Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 SqlCmd :connect with variables and backslash

Author  Topic 

mattt
Posting Yak Master

194 Posts

Posted - 2011-09-14 : 10:14:18
Hi,

When using SqlCmd to try and connect to an active server I have discovered that these both work:


:connect myServer\myInstanceName -U myLogin -P myPassword

:setvar INSTANCE myInstanceName
:connect $(INSTANCE) -U myLogin -P myPassword


This does not:


:setvar serv myServer
:setvar inst myInstance
:connect $(serv)\$(inst) -U myLogin -P myPassword


It throws this as a syntax error, I suspect because it thinks the back-slash is an escape character.

Doing this doesn't help:

:connect '$(serv)\$(inst)' -U myLogin -P myPassword


And neither does this:

:connect $(serv)\\\\$(inst) -U myLogin -P myPassword


And I can't figure out how sqlCmd is supposed to concatenate variables, so I can't pass the whole thing in as a variable.

Anyone help?

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2011-09-14 : 10:27:13
how about this

:setvar SERVER foo\inst
:connect $(SERVER) -U myLogin -P myPassword



elsasoft.org
Go to Top of Page

mattt
Posting Yak Master

194 Posts

Posted - 2011-09-14 : 10:29:24
quote:
Originally posted by jezemine

how about this

:setvar SERVER foo\inst
:connect $(SERVER) -U myLogin -P myPassword



I tried this, but the trouble is that (in your example) foo and inst are environment variables, not literals. So it's

:setvar SERVER $(foo)\$(inst)

Which results in SERVER being set to the specific value "$(foo)\$(inst)". If I could figure out how to build a variable from existing variable values, it might work. But I can't see how to do it.

Cheers,
Matt

Go to Top of Page
   

- Advertisement -