In such cases I follow this logic:
DECLARE @cmdTbl TABLE(id INT IDENTITY,cmd NVARCHAR(MAX));
INSERT INTO @cmdTbl(cmd) VALUES(N'SELECT TOP 3 * FROM INFORMATION_SCHEMA.TABLES');
INSERT INTO @cmdTbl(cmd) VALUES(N'SELECT TOP 3 * FROM INFORMATION_SCHEMA.COLUMNS');
DECLARE @cmd VARCHAR(MAX);
DECLARE cur CURSOR FOR SELECT cmd FROM @cmdTbl ORDER BY id;
OPEN cur;
FETCH NEXT FROM cur INTO @cmd;
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT @cmd;
EXEC(@cmd);
FETCH NEXT FROM cur INTO @cmd;
END
CLOSE cur;
DEALLOCATE cur;
First there is a declared table variable. You insert all your statements separately.
No GO
!
Everywhere you want to set a GO
just insert a new command.
The CURSOR
will execute all statements in the order you inserted them.
In the first attempt comment-out the EXEC
and copy all the PRINT
-output into a new query window to check for syntax errors. Then execute the whole lot in one go...
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…