Hier ist etwas zum Nachdenken ... Es verwendet die Funktion BULK INSERT, um die SQL-Anweisungen in eine temporäre Tabelle zu laden, und verwendet dann dynamisches SQL, um sie auszuführen.
Und ja ... alle die übliche Warnung von SQL-Injektion anwenden, etc, etc,
--Assume this is your main sql file.....
--Execute some SQL commands....
--Now load the second SQL file and execute it
DECLARE @SQL NVARCHAR(MAX);
CREATE TABLE #SQL (value nvarchar(max));
BULK INSERT #SQL
FROM 'C:\TEMP\select2.sql'
WITH
(
DATAFILETYPE = 'char',
FIELDTERMINATOR = '|', --<Make sure these characters
ROWTERMINATOR = '|' --<don't appear in your SQL statements
);
SELECT @SQL = Value FROM #SQL
EXEC (@SQL);
TRUNCATE TABLE #SQL;
-- Do some other sql commands in your main query
--Now load the third SQL file and execute it
BULK INSERT #SQL
FROM 'C:\TEMP\select3.sql'
WITH
(
DATAFILETYPE = 'char',
FIELDTERMINATOR = '|', --<Make sure these characters
ROWTERMINATOR = '|' --<don't appear in your SQL statements
);
SELECT @SQL = Value FROM #SQL
EXEC (@SQL);
--Do some more sql commands
--etc...
--Clean up the temp table
DROP TABLE #SQL
Dieser Ansatz wird ohne SSMS arbeiten, und kann in jeder Umgebung
Mögliche Duplikat (http://stackoverflow.com/questions/5237198/transactsql-to-run-another-transactsql-script) –
@ PM77-1 [TransactSQL andere TransactSQL-Skript ausführen] Es funktioniert nicht. Keine Ahnung warum ? Ich habe versucht, das: r/Skript Path/in meinem .sql fil, aber es unterstrichen von sql. Nicht als Befehl erkannt –