Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

sqlcmd mode and r command

 
   Database Help (Home) -> Programming RSS
Next:  XML in a Stored Procedure with routing  
Author Message
Dan Holmes

External


Since: May 23, 2008
Posts: 47



(Msg. 1) Posted: Mon Aug 18, 2008 4:38 pm
Post subject: sqlcmd mode and r command
Archived from groups: microsoft>public>sqlserver>programming (more info?)

I have this small piece of SQL running in sqlcmd mode.

DECLARE @v VARCHAR (MAX)
SET @v = 'c:\asdf.sql'
:setvar filename @v
PRINT $(filename)
:r $(filename)

I get this error:
A fatal scripting error occurred.
The file specified for :r command was not found.

The filename does print in the print command and the file exists. I have seen examples of this syntax here
http://www.simple-talk.com/sql/sql-tools/the-sqlcmd-workbench/ but can't make it work for me.

Anyone know what i am doing wrong?

 >> Stay informed about: sqlcmd mode and r command 
Back to top
Login to vote
Erland Sommarskog2

External


Since: May 30, 2004
Posts: 2061



(Msg. 2) Posted: Mon Aug 18, 2008 4:38 pm
Post subject: Re: sqlcmd mode and r command [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Dan Holmes (dan.holmes@routematch.com) writes:
> I have this small piece of SQL running in sqlcmd mode.
>
> DECLARE @v VARCHAR (MAX)
> SET @v = 'c:\asdf.sql'
>:setvar filename @v
> PRINT $(filename)
>:r $(filename)
>
> I get this error:
> A fatal scripting error occurred.
> The file specified for :r command was not found.
>
> The filename does print in the print command and the file exists. I
> have seen examples of this syntax here
> http://www.simple-talk.com/sql/sql-tools/the-sqlcmd-workbench/ but can't
> make it work for me.
>
> Anyone know what i am doing wrong?

The value of $(filename) is "@v", not whatever value the T-SQL variable
that @v might have. Keep in mind that SQLCMD is a client tool, and SQL
Server is a server application. Furthermore, T-SQL variables are local
to a batch, so there is no way that SQLCMD can retrieve the value of @v
to assign it to one of its own variables.


--
Erland Sommarskog, SQL Server MVP, esquel DeleteThis @sommarskog.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

 >> Stay informed about: sqlcmd mode and r command 
Back to top
Login to vote
Bob

External


Since: Feb 08, 2005
Posts: 182



(Msg. 3) Posted: Mon Aug 18, 2008 7:37 pm
Post subject: RE: sqlcmd mode and r command [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

You can't really mix sqlcmd variables and T-SQL variables in this way. What
you could do, is script the script out to another file and run it, eg

-- Option1: sqlcmd variables only
:setvar filename "c:\temp\temp.sql"
PRINT '$(filename)'
:r $(filename)


-- Option 2: script the script
SET NOCOUNT ON
:out c:\temp\sqlcmd.sql
DECLARE @v VARCHAR (MAX)
SET @v = 'c:\temp\temp.sql'
SELECT ':setvar filename ' + @v + '
PRINT ''$' + '(filename)''
:r $' + '(filename)'
GO
:out STDOUT
GO
:r c:\temp\sqlcmd.sql



-- Option 3: like option 2, but from a table
SET NOCOUNT ON
:out c:\temp\sqlcmd.sql
DECLARE @files TABLE ( filename VARCHAR(MAX) )
INSERT INTO @files SELECT 'c:\temp\temp.sql' UNION SELECT 'c:\temp\temp2.sql'

SELECT ':setvar filename ' + filename + '
PRINT ''$' + '(filename)''
:r $' + '(filename)
GO'
FROM @files
GO
:out STDOUT
GO
:r c:\temp\sqlcmd.sql


SQLCMD mode is wicked, I use it all the time.
HTH
wBob
Rate the post.
"Dan Holmes" wrote:

> I have this small piece of SQL running in sqlcmd mode.
>
> DECLARE @v VARCHAR (MAX)
> SET @v = 'c:\asdf.sql'
> :setvar filename @v
> PRINT $(filename)
> :r $(filename)
>
> I get this error:
> A fatal scripting error occurred.
> The file specified for :r command was not found.
>
> The filename does print in the print command and the file exists. I have seen examples of this syntax here
> http://www.simple-talk.com/sql/sql-tools/the-sqlcmd-workbench/ but can't make it work for me.
>
> Anyone know what i am doing wrong?
>
 >> Stay informed about: sqlcmd mode and r command 
Back to top
Login to vote
Dan Holmes

External


Since: May 23, 2008
Posts: 47



(Msg. 4) Posted: Tue Aug 19, 2008 12:29 pm
Post subject: Re: sqlcmd mode and r command [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Erland Sommarskog wrote:
> Dan Holmes (dan.holmes@routematch.com) writes:
>> I have this small piece of SQL running in sqlcmd mode.
>>
>> DECLARE @v VARCHAR (MAX)
>> SET @v = 'c:\asdf.sql'
>> :setvar filename @v
>> PRINT $(filename)
>> :r $(filename)
>>
>> I get this error:
>> A fatal scripting error occurred.
>> The file specified for :r command was not found.
>>
>> The filename does print in the print command and the file exists. I
>> have seen examples of this syntax here
>> http://www.simple-talk.com/sql/sql-tools/the-sqlcmd-workbench/ but can't
>> make it work for me.
>>
>> Anyone know what i am doing wrong?
>
> The value of $(filename) is "@v", not whatever value the T-SQL variable
> that @v might have. Keep in mind that SQLCMD is a client tool, and SQL
> Server is a server application. Furthermore, T-SQL variables are local
> to a batch, so there is no way that SQLCMD can retrieve the value of @v
> to assign it to one of its own variables.
>
>
I very much want to agree with you but running this:

DECLARE @v VARCHAR (MAX)
SET @v = 'c:\asdf.sql'
:setvar filename @v
PRINT $(filename)

on my system will print 'c:\asdf.sql'

would you try that on yours? I have tried with SSMS 2005 and 2008. Both print the value in @v from the sqlcmd variable
$(filename)

danny
 >> Stay informed about: sqlcmd mode and r command 
Back to top
Login to vote
Erland Sommarskog2

External


Since: May 30, 2004
Posts: 2061



(Msg. 5) Posted: Tue Aug 19, 2008 2:52 pm
Post subject: Re: sqlcmd mode and r command [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Dan Holmes (dan.holmes@routematch.com) writes:
> I very much want to agree with you but running this:
>
> DECLARE @v VARCHAR (MAX)
> SET @v = 'c:\asdf.sql'
>:setvar filename @v
> PRINT $(filename)
>
> on my system will print 'c:\asdf.sql'

Of course it does. That's the value of @v. But try this:

DECLARE @v VARCHAR (MAX)
SET @v = 'c:\asdf.sql'
:setvar filename @v
PRINT '$(filename)'

or

DECLARE @v VARCHAR (MAX)
SET @v = 'c:\asdf.sql'
:setvar filename @v
go
PRINT '$(filename)'


--
Erland Sommarskog, SQL Server MVP, esquel.RemoveThis@sommarskog.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
 >> Stay informed about: sqlcmd mode and r command 
Back to top
Login to vote
Dan Holmes

External


Since: May 23, 2008
Posts: 47



(Msg. 6) Posted: Wed Aug 20, 2008 8:50 am
Post subject: Re: sqlcmd mode and r command [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Erland Sommarskog wrote:
> Dan Holmes (dan.holmes@routematch.com) writes:
>> I very much want to agree with you but running this:
>>
>> DECLARE @v VARCHAR (MAX)
>> SET @v = 'c:\asdf.sql'
>> :setvar filename @v
>> PRINT $(filename)
>>
>> on my system will print 'c:\asdf.sql'
>
> Of course it does. That's the value of @v. But try this:
>
> DECLARE @v VARCHAR (MAX)
> SET @v = 'c:\asdf.sql'
> :setvar filename @v
> PRINT '$(filename)'
>
> or
>
> DECLARE @v VARCHAR (MAX)
> SET @v = 'c:\asdf.sql'
> :setvar filename @v
> go
> PRINT '$(filename)'
>
>
Now i am even more confused. Why does PRINT '$(filename)' and PRINT $(filename) produce different outputs? I get the
value of @v in one and '@v' in the other? And if i can get $(filename) to print the value of @v why won't :r use that
value when running?

Are there better resources than BOL for this?

danny
 >> Stay informed about: sqlcmd mode and r command 
Back to top
Login to vote
Erland Sommarskog2

External


Since: May 30, 2004
Posts: 2061



(Msg. 7) Posted: Wed Aug 20, 2008 3:22 pm
Post subject: Re: sqlcmd mode and r command [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Dan Holmes (dan.holmes@routematch.com) writes:
> Now i am even more confused. Why does PRINT '$(filename)' and PRINT
> $(filename) produce different outputs? I get the value of @v in one and
> '@v' in the other? And if i can get $(filename) to print the value of
> @v why won't :r use that value when running?

It's nothing strange at all.

PRINT @v

prints the value of @v

PRINT '@v'

prints the string '@v'.

In SQLCMD you have defined the variable filename to have the value '@v'.
Then you say:

PRINT $(filename)

SQLCMD reads this line, and recognizes that $(filename) is an SQLCMD
variable, and replace it with its value before it sends the string to
SQL Server. SQL Server sees this:

DECLARE @v VARCHAR (MAX)
SET @v = 'c:\asdf.sql'
PRINT @v

It's important to understand that the order of execution here does not
agree with the text you see. You had:

DECLARE @v VARCHAR (MAX)
SET @v = 'c:\asdf.sql'
:setvar filename @v
PRINT $(filename)

But the actual execution order is:

:setvar filename @v

DECLARE @v VARCHAR (MAX)
SET @v = 'c:\asdf.sql'
PRINT $(filename)

And more in detail this happens:

1) SQLCMD parses the command text, looking for
a) go
b) it's own commands, those starting with :
c) Variables in $() to expand.
2) SQL performs commands and expansions as it finds them. When it reaches
go it sends the text to SQL Server.
3) SQL Server parses the batch, to check syntax and that.
4) SQL Server executes the batch.
5) SQLCMD gets the result back and prints the result.

--
Erland Sommarskog, SQL Server MVP, esquel.TakeThisOut@sommarskog.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
 >> Stay informed about: sqlcmd mode and r command 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
sqlcmd in silent mode - Is it possible to run sqlcmd in silent, or quiet mode where the console window does not appear? I am setting up an automatic backup system using calls to sqlcmd -Q with the Windows Task Scheduler. The transaction log backups, especially, will likely....

sqlcmd -v - Hello, SQL Server 2005 STD edition SP1 Trying to call the following from a stored Proc. Yes XP_CmdShell is enabled Declare @Server_Instance sysname ,@cmd varchar(1000) set @Server_Instance = 'Server\Instance' set @cmd = 'SQLCMD -S<Host serve...

SQLCMD Output - Hello, The documentation says that sqlcmd shows column headers above an output resultset. That is what I want. I use the -o parameter (tried the >> operator also) to put the output in a file. There are no column headers in the file. Am I missing....

SQLCMD -L doesn't find all my servers - I have one domain with many SQL servers. Of them all, 4 (that i know of) aren't being found. Can someone point me to some good documentation on how SQLCMD -L works. Also, has anyone else seen this, and have they solved it? Thanks. -- The Spirit....

sqlcmd output is UTF-8 - If I create a simple sqlcmd script: :out c:\temp\temp2.sql SELECT 1 GO :out STDOUT and run it, the output appears to be UTF-8. This causes problems not least because SSMS saves code as ANSI. I can open the output files in Notepad and save them a...
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]