TSQL在游标期间设置连接varchar

时间:2015-06-05 12:48:00

标签: sql tsql cursor

调试时,@ buildEvents在第一次进入循环时会被填充,但下次再也不会被连接起来。这可能在游标内吗?

OPEN @BusinessCursor;
FETCH NEXT FROM @BusinessCursor INTO  @BusinessName;

WHILE @@FETCH_STATUS = 0
BEGIN

set @buildEvents = @buildEvents + @BusinessName

FETCH NEXT FROM @BusinessCursor INTO   @BusinessName;
END

CLOSE @BusinessCursor;
DEALLOCATE @BusinessCursor;     


      select @buildEvents
      return

关于如何让@buildEvents与@BusinessName连接的任何想法?通过的数据是电子邮件的html数据。

1 个答案:

答案 0 :(得分:0)

你可能不会做两件事之一:

  1. 当您声明@buildEvents为其分配一个空字符串时:

    declare @buildEvents varchar(max) = ''

  2. 在循环中检查null中的@BusinessName

    set @buildEvents = @buildEvents + ISNULL(@BusinessName, '')