sqlcmd.exe -v Rocks

CraigBlog

Syndication

I'm the first to admit that I'm no database guru, but I occasionally have need to do something beyond just a simple SELECT, and I can usually manage to fumble my way through it.
 
The other day, the thing I was fumbling my way through was restoring a database from a backup a client had sent me. While I was doing it, I ran across a neat little option to sqlcmd.exe that I hadn't seen before, but which I definitely want to remember. It's the -v option, and it lets you pass parameters to your SQL script. In my case, I wanted to pass the current directory, so I could restore the database to files in whatever directory I happened to be running. Well, putting this in a .cmd file does the trick:
 
sqlcmd -E -i restoredb.sql -v root="%CD%"
 
Then I can use the root variable in my SQL script. All I have to do is reference it with the $(root) syntax, like this:
 
RESTORE DATABASE MyDB 
    FROM DISK = '$(root)\mydb.bak'
    WITH REPLACE,
    MOVE 'mydb_data' to '$(root)\mydb.mdf',
GO
 
Nifty, eh? I know I'm far from the first one to "discover" this, but maybe it'll help someone who hasn't seen it before.

Posted Dec 16 2005, 03:15 PM by craig-andera

Comments

Christopher Steen wrote Link Listing - December 16, 2005
on 12-16-2005 7:00 PM
ASP.NET Podcast Show #30 - Minimizing the ASP.NET ViewState
Part #2 [Via: Wallym ]
Complex data binding...
Anshuk Jain wrote re: sqlcmd.exe -v Rocks
on 09-14-2006 10:58 AM
Is there any way I can get a SP to output a string to a dos variable?
Craig wrote re: sqlcmd.exe -v Rocks
on 09-17-2006 3:22 AM
I can't think of an easy one. You could write a very simple C# program to do it, or you could use a more advanced scripting environment, like PowerShell.
Aaron wrote re: sqlcmd.exe -v Rocks
on 02-07-2007 4:03 PM
I used SQLCMD.exe to print the required data to a file. If you want the result only and not table name etc.. Use the Print statement in your SQL file to print only what you want. Then you just have to read the file to get what you want.
db042188 wrote re: sqlcmd.exe -v Rocks
on 05-04-2007 4:48 AM
what if you want to pass more than 1 user variable?
Craig wrote re: sqlcmd.exe -v Rocks
on 05-04-2007 5:28 AM
I haven't tried it, but my guess is that you simply use more than variable. E.g.

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

Would you mind trying it and posting back here with your results?
Nishanth Marathe wrote re: sqlcmd.exe -v Rocks
on 05-29-2007 7:35 PM
Thanks tonnes.
I was searching exactly for this (desperately). You really rock man. In my case i wanted to attach a DB and run a lot of scripts from command prompt, but did not know how to pass current directory to script file.
"%CD%" helped.

sqlcmd -S localhost\SQLExpress -E /i db_attach.sql -v Path="%CD%"

Thanks again!!!
Nishanth
(nishanth_marathe@rediffmail.com)
Craig wrote re: sqlcmd.exe -v Rocks
on 05-30-2007 3:43 AM
No charge. :)
Kiran wrote re: sqlcmd.exe -v Rocks
on 09-25-2007 11:20 PM
Hi,

Iam using MSDE2000. iam want to use osql. can u please help how do i restore database using the osql.exe.


Thanks
Kiran
kumarkiranc@yahoo.com
Sean wrote re: sqlcmd.exe -v Rocks
on 12-18-2007 1:57 AM
I know it's a while since you asked someone else, but I've tried more than one variable as you suggested and it works just fine. Thanks for the tip off.

Sean Kerr
seankerr06@yahoo.co.uk

Add a Comment

(required)  
(optional)
(required)  
Remember Me?