How could I execute a set of .SQL files (each does some data transformations) from within SQL Server Management Studio?
What other alternative are there for executing .SQL files in batch?
How could I execute a set of .SQL files (each does some data transformations) from within SQL Server Management Studio?
What other alternative are there for executing .SQL files in batch?
While SQLCMD.exe is the best way, SSMS also has a SQLCMD mode where you can execute a SQLCMD script. To enable this mode click Query in menu bar then select SQLCMD Mode.
The ":r filename.sql" command is the SQLCMD script command to import and execute a sql script file. You know you are in SQLCMD mode because any lines that are SQLCMD script commands will appear with colored (gray I think) background.
:setvar path "c:\Path_to_scripts\"
:r $(path)\file1.sql
:r $(path)\file2.sql
Use SqlCmd.exe.
For example:
sqlcmd -S myServer\instanceName -i C:\myScript.sql
or to save output to a file:
sqlcmd -S myServer\instanceName -i C:\myScript.sql -o C:\EmpAdds.txt
You can execute T-SQL files without using SQLCMD Mode or using SQLCMD outside SSMS, like this:
exec sp_configure 'show advanced options',1;reconfigure with override;
exec sp_configure 'xp_cmdshell',1;reconfigure with override;
declare @sqlfile nvarchar(100),@sqlcmd varchar(4000)
exec xp_cmdshell 'del c:\SQLscript.sql && echo select ''This Is a script running on SQLCMD from SSMS'' >> c:\SQLscript.sql',no_output --test script
set @sqlfile = 'c:\SQLscript.sql' --script location
set @sqlcmd = 'sqlcmd -E -i '+@sqlfile
exec xp_cmdshell @sqlcmd --executing script
exec sp_configure 'xp_cmdshell',0;reconfigure with override;
exec sp_configure 'show advanced options',0;reconfigure with override;