-2

I have a scalar value function that returns a VarChar(MAX) In my stored procedure I do this

declare @p_emailAddr varchar(MAX) = (select db.dbo.GetEmails(10))

If I do print @p_emailAddr it shows me it was populated with the correct information but the rest of the code doesn't work correctly using it. (I have no clue why, it doesn't make sense!)

Now if I change it like this

declare @p_emailAddr varchar(MAX) = '[email protected];'

The rest of my code works perfect as it should!

What is the difference between the two methods of setting @p_emailAddr that is breaking it?

This is get emails code

ALTER FUNCTION [dbo].[GetEmails](@p_SubID int)
RETURNS varchar(max)
AS
BEGIN
    DECLARE @p_Emails varchar(max)

    SELECT @p_Emails = COALESCE(@p_Emails + ';', '') + E.EmailAddress
    FROM    db.dbo.UserEmailAddr E JOIN
            db.dbo.EmailSubscriptionUsers S on e.ClockNumber = s.Clock AND S.SubID = @p_SubID

    SET @p_Emails = @p_Emails + ';'
    RETURN @p_Emails
END
Timmy
  • 543
  • 1
  • 7
  • 19

1 Answers1

0

What's coming back from GetEmails(10)? varchar(max) is a string value and is expecting a single value. you could have a table variable or if dbo.getemails(10) is a table just join it where you're expecting to use @p_emailaddr

best

select *
from table1 t1
join dbo.GetEmails(10) e
on e.email = t1.email

alternative

create table #GetEmails (emails varchar(max))
insert into #GetEmails values ('[email protected]'), ('[email protected]')

declare @p_emailAddr table (emails varchar(max))
insert into @p_emailAddr(emails)
select * 
from #GetEmails


select * 
from @p_emailAddr
Migo
  • 153
  • 10
  • The problem was found. Check my last comment in OP, it was permission issues between the stored procedure and the getEmails() functions. – Timmy Dec 11 '15 at 19:32