New & Cool SQL Server commands

Run an external program from inside SQL Server:
  exec xp_cmdShell ‘path\of\program’ ;

Run a script file from inside another script file:
  exec xp_cmdShell ‘sqlCmd -i "path\of\script" -d "databaseName"’ ;

Example:

exec spClearDB null ;
go

go
declare @cmd nvarchar(4000);
declare @db nvarchar(4000);
declare @in nvarchar(4000);
set @cmd = ”;
set @db = ‘ -d "’ + DB_NAME()+’"’;
set @in = ‘ -i "d:\desktop-folder\giang day\QTCSDL.united\bai nop gia lap RB khoa chinh\build database\db\build database.sql"’;
set @cmd = ‘sqlcmd’ + @db + @in;
exec xp_cmdshell @cmd, no_output;

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: