Internet Handholding posted on March 22, 2010 10:57
Author's note: updated the below article and have posted a new article on the same subject on May 3, 2011, for Windows 7, Sql Server Express 2008 R2.
www.programmer.bz/Articles/tabid/159/asp_net_sql/266/Understanding-SqlCmd-Variables-and-the-Use-Command.aspx
Sqlcmd utility reference with description of setvar is here.
http://msdn.microsoft.com/en-us/library/ms162773.aspx
There is a good article on variables in sqlcmd using setvar.
http://blogs.msdn.com/gertd/archive/2007/01/08/variables-to-the-rescue.aspx
In Sql Server Management Studio, script filenames end with .sql. For sqlcmd scripts, I use .sqlcmd.sql to distinguish from a transact only .sql script. That way they still open in SSMS.
www.programmer.bz/Forum/tabid/259/forumid/8/postid/47/scope/posts/Default.aspx
Unfortunately, it seems you cannot use sqlcmd script variables within the :setvar statement. This does not work.
:setvar pathscripts c:\sql\scripts\
:setvar pathmyscripts $(pathscripts)my\
I am using Windows 7 with Sql Server Express R2. You can use variables in the :r command, provided you do not quote the values. This works.
:setvar pathscripts c:/sql/scripts/
:r $(pathscripts)myscript.sqlcmd.sql
To print the values of the SqlCmd variables that are set to object references, or whatever they are other than strings, you need to include the variable in quotes.
print 'pathscripts=$(pathscripts)'
By Andrew Weitzen, Bronze Inc. (c) 2010-present
Bronze is the publisher of several online Internet journals including: InternetHandholding.com, DomainNames.gs, DotNetNuke.bz, Programmer.bz, Software.vg, WebHosting.vg