Concatinate SP always return NULL?

  • Hi All,

    Any ideas why this always returns NULL? Help would be greatly appreciated!

    DECLARE @Count int

    DECLARE @sql NVARCHAR(2000)

    SET @Count = 1

    WHILE @Count < 20

    BEGIN

    SET @sql = @sql + ' Test ' + CAST(@Count AS NVARCHAR)

    SET @Count = @Count + 1

    END

    SELECT @sql

  • @sql starts off as null, and you keep appending to it - which will always give null. You need to set @sql...

    DECLARE @Count int

    DECLARE @sql NVARCHAR(2000)

    SET @sql = '' -- You need this

    SET @Count = 1

    WHILE @Count < 20

    BEGIN

    SET @sql = @sql + ' Test ' + CAST(@Count AS NVARCHAR)

    SET @Count = @Count + 1

    END

    SELECT @sql

    Ryan Randall

    Solutions are easy. Understanding the problem, now, that's the hard part.

  • Great. That works. Thanks for the help!

  • Or you can modify your code little.

    Hi All,

    Any ideas why this always returns NULL? Help would be greatly appreciated!

    DECLARE @Count int

    DECLARE @sql NVARCHAR(2000)

    SET @Count = 1

    WHILE @Count < 20

    BEGIN

    SET @sql = isnull(@sql,'') + ' Test ' + CAST(@Count AS NVARCHAR)

    SET @Count = @Count + 1

    END

    SELECT @sql

    karthik

  • Or use SET CONCAT_NULL_YIELDS_NULL OFF in your SP...

Viewing 5 posts - 1 through 4 (of 4 total)

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