Concatenated Variable Problem

  • I'm having a problem concatenating a variable to itself.  I don't receive an error, I just don't receive any results.  Below is the code I'm using.

    --First I tried setting the value in a select statement and didn't receive any results

    DECLARE @To nvarchar(1000)

    SELECT @To = @To + email + ';'

    FROM tblNotifications

    WHERE NotifyType = 1 AND EmailNotify = 1

    PRINT @To

    --So I tried it with a cursor and received the same result

    DECLARE @email nvarchar(20), @To nvarchar(1000)

    DECLARE email_notify CURSOR FOR

     SELECT email

     FROM tblNotifications

     WHERE NotifyType = 1 AND EmailNotify = 1

    OPEN email_notify

    FETCH NEXT FROM email_notify INTO @email

    WHILE @@FETCH_STATUS = 0

     BEGIN

      SET @To = @To + @email + ';'

      FETCH NEXT FROM email_notify INTO @email

     END

    CLOSE email_notify

    DEALLOCATE email_notify

    PRINT @To

  • You aren't initializing @To.

    That means it is NULL.

    NULL concatenated with anything is always NULL

     

    DECLARE @To nvarchar(1000)

    SELECT @To = ''  -- Initialize to empty string

  • You really are a veteran.  I've never run into that problem before but it makes sense.  Thanks for the help!

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply