Hi everyone,
I am looking to call a function in my select statement that will grab information from each row, and return an appropriate email address based off of this information. I need to do this because I have some email addresses stored in the view that I am selecting from, and some inside of a table that came from a different source. Both tables contain the field 'ID_NBR', so that is how I am joining them. The logic needs to be that if a given 'ID_NBR' has an email address in the table, that will win out & replace whatever is in the email field already. If not I want to keep the email that is already in the email field of view I am selecting from. And if there isn't an email in either place I'd like to put NULL in the field.
This is what I have so far:
The select statement looks like this:
SELECT DATE, ID_NBR, NAME, get_email_addr(ID_NBR, 'a') AS EMAIL FROM view_a
The function looks like this:
BEGIN DECLARE @email varchar(25); If @type = 'a' set @email = (SELECT CASE WHEN email_table.email LIKE '%@%' THEN email_table.email WHEN view_a.EMAIL like '%@%' THEN view_a.EMAIL ELSE NULL END AS email FROM email_table RIGHT OUTER JOIN view_a ON email_table.ID_NBR = view_a.ID_NBR WHERE ID_NBR = @id_nbr) RETURN @email; END
I am getting an error that says this:
"Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression."
Also, I am not sure if this is the most efficient way to go about solving this problem.
Does anyone know what I am doing wrong, or a better way to do this? Thanks!!