Wednesday, May 19, 2010

SQLCMD

Here is a handy cheat sheet for Microsoft's SQLCMD, when you don't have the luxury of SQL Server Management Studio.

To run some sql and have results sent to "output.txt",on local server:

>SQLCMD -S(local) -U sa -P changeme -q "use WONDERBEAN;select * from orders;" -o output.txt


To run some sql stored in "myScript.sql" and have results logged to "output.txt",
on local server,with instance called "instanceSmedly":

>SQLCMD -S(local)\instanceSmedly -U sa -P changeme -i myScript.sql -o output.txt


Typical use would be to do a restore. Here is a handy example of some restore sql syntax:

alter database WONDERBEAN SET single_user with rollback immediate;
restore database WONDERBEAN from disk = 'C:\Users\Howard\Documents\WONDERBEAN_1_1_2010.bak' with replace,recovery;
alter database WONDERBEAN set multi_user

No comments: