Monday, November 14, 2011

Few Useful SQL Commands

1.) Executing a sql file with SQLCMD command and passing a variable value to file variable.

Let's say restoredb.sql is a file contains following sql.

RESTORE
DATABASE MyDB FROMDISK = '$(root)\mydb.bak'WITH REPLACE,MOVE 'mydb_data' to '$(root)\mydb.mdf',GO

Now I can execute the content of restoredb.sql file with any value of root variable.

sqlcmd -E -i restoredb.sql -v root="%CD%"


Site Reference: