TSQL 在游标期间设置连接 varchar

TSQL set concatenating varchar during 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. 当您声明 @buildEvents 时为其分配一个空字符串:

    declare @buildEvents varchar(max) = ''

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

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