I need to find a T-SQL way to remove GO command from scripts which I read from .sql files. I'm using similar to this approach Execute SQL scripts from file, which suits my needs very well, however some of the files contains GO command and breaks execution as sp_executesql
doesn't handle non T-SQL commands.
How to create a REPLACE
which would remove GO, which mostly sits alone in the row? Or any other method I could apply here? Please keep in mind, that in the script there could be other GOs, which are actually not a command.
DECLARE @sql NVARCHAR(1000) =
'DECLARE @table AS TABLE(
[Id] INT,
[Info] NVARCHAR(100)
);
INSERT INTO @table
([Id],[Info])
VALUES
(1,''Info''),
(2,''Show must go on''),
(3,''GO'');
SELECT * FROM @table;
GO';
PRINT @sql;
EXEC sp_executesql @sql;
Using xp_cmdshell
to execute scripts is not an option due to server security restrictions. SQLCMD
is not an option too this time.
CodePudding user response:
Well, I would NOT claim that this is the way this should be done, but it was some fun to ticker it down:
Disclaimer: This is a demonstration why TSQL is the wrong tool for this