SQL Server:如何打印变量以打破每一行并超过varchar(MAX)

时间:2019-05-22 18:31:42

标签: sql-server sql-server-2008

我有以下查询:

DECLARE @TableName VARCHAR(MAX)
DECLARE @Script VARCHAR(MAX) = ''
DECLARE @sql NVARCHAR(MAX)
DECLARE @fillfactor INT

DECLARE TableCursor CURSOR FOR
    SELECT '[' + OBJECT_SCHEMA_NAME([object_id]) + '].[' + name + ']' AS TableName
    FROM sys.tables

OPEN TableCursor
FETCH NEXT FROM TableCursor INTO @TableName

WHILE @@FETCH_STATUS = 0
BEGIN
    SET @sql = 'ALTER INDEX ALL ON ' + @TableName + ' REBUILD' + ')' 

    SELECT @Script = CAST(@Script + @sql AS VARCHAR(MAX))

    FETCH NEXT FROM TableCursor INTO @TableName
END

CLOSE TableCursor
DEALLOCATE TableCursor

PRINT @Script
GO

SQL Server的新功能。现在限制为8000个字符,我想知道如何打印所有表中的@Script,并且同时@sql上的每一行都换成新行。

0 个答案:

没有答案