Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / DB Engine / SQL Server / February 2010

Tip: Looking for answers? Try searching our database.

SQL 2005 Express SQLCMD

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tom Nowak - 08 Feb 2010 21:26 GMT
I am trying to use SQLCMD with a -v to run a SQL file using the variables I
am trying to pass to it.  I want to pass a date, but every time I try, the
SQL trys to put the wrong data in the wrong fields:

Command

c:\web\sqltest.cmd server database table '02/09/2010 09:00:00' 'ABCD1234'
'SYS'

CMD File

sqlcmd -S %1 -i c:\web\mytest.sql -v dbname = %2 tablename = %3 jstart = %4
id = %5 sys= %6

SQL file

use $(dbname)
update $(tablename)
set jstart= $(jstart), jend = ' ', status = 'OK'
where id= $(id) AND sys= $(sys) AND (status = 'ID')

I guess I am not sure how to specify date data in the SQLCMD -v command.  

Please help.
Erland Sommarskog - 08 Feb 2010 23:02 GMT
> I am trying to use SQLCMD with a -v to run a SQL file using the
> variables I am trying to pass to it.  I want to pass a date, but every
[quoted text clipped - 4 lines]
> c:\web\sqltest.cmd server database table '02/09/2010 09:00:00' 'ABCD1234'
> 'SYS'

On the command line, you use "" to delimit strings, not ''. (And this has
nothing to do with SQLCMD, that is just the syntax of the DOS BOX.)

Signature

Erland Sommarskog, SQL Server MVP, esquel@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

jbellnewsposts@hotmail.com - 08 Feb 2010 23:07 GMT
>I am trying to use SQLCMD with a -v to run a SQL file using the variables I
>am trying to pass to it.  I want to pass a date, but every time I try, the
[quoted text clipped - 20 lines]
>
>Please help.

Hi

For the command file:

sqlcmd -S %1 -i c:\temp\SQLFile.sql -v dbname=%2 tablename=%3
jstart=%4 id=%5 sys=%6

and the command:

sqltest "(local)" "Tempdb" "test" "20090209" "2" ""

Without en-quoting the jstart parameter it will assume a number

USE $(dbname);
CREATE TABLE $(tablename) ( id int );
INSERT $(tablename) (id ) values ( 1) , (2) ;
SELECT id, CAST('$(jstart)' AS DATE ) AS jstart
FROM $(tablename)
WHERE id = $(id) ;
DROP TABLE $(tablename) ;
Go

If you don't have quotes in the SQL Script

USE $(dbname);
CREATE TABLE $(tablename) ( id int );
INSERT $(tablename) (id ) values ( 1) , (2) ;
SELECT id, CAST($(jstart) AS DATE ) AS jstart
FROM $(tablename)
WHERE id = $(id) ;
DROP TABLE $(tablename) ;
Go

and single quotes in the cmd file

sqlcmd -S %1 -i c:\temp\SQLFile.sql -v dbname=%2 tablename=%3
jstart='%4' id=%5 sys=%6

You can't use double quotes in the command.

John
Tom Nowak - 09 Feb 2010 15:33 GMT
I thought I understood your comments, but still am getting errors:

I tried this:

CMD File

sqlcmd -S %1 -i c:\web\mytest.sql -v dbname = %2 tablename = %3 jstart =
%4 id = %5 syst = %6 stat = %7

SQL File

use $(dbname)
update $(tablename)
set jstart = CAST('$(jstart)' AS DATE), jend = ' ', stat =
'RUN'
where id = $(id) AND syst = $(syst) AND (stat = 'SCHED')

Command

c:\web\sqltest.cmd "server" "database" "table" "02/09/2010
09:00:00" "ABCD1234" "ABC"

I receive invalid column names ABCD1234 and ABC.

> >I am trying to use SQLCMD with a -v to run a SQL file using the variables I
> >am trying to pass to it.  I want to pass a date, but every time I try, the
[quoted text clipped - 64 lines]
>
> .
Erland Sommarskog - 09 Feb 2010 23:00 GMT
> I thought I understood your comments, but still am getting errors:
>
[quoted text clipped - 19 lines]
>
> I receive invalid column names ABCD1234 and ABC.

The WHERE clause after expansion will become:

 where id = ABCD1234 AND syst = ABC AND (stat = 'SCHED')

which will not work, unless you have columns with these names. You should
probably have:

  where id = '$(id)' AND syst = '$(syst)' AND (stat = 'SCHED')

in the SQL file.

Signature

Erland Sommarskog, SQL Server MVP, esquel@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

Tom Nowak - 10 Feb 2010 02:02 GMT
It is now working!  My new question is before I was having problems, I was
storing a date in a nvarchar field and a time in another nvarchar field, but
I converted those fields to one datetime field to make searching between
dates easier.  When I had the two nvarchar fields, I did not have to enclose
parameters in the WHERE in quotes.  Why do I have to do that now?

Thank you so much for correcting my issue.

> > I thought I understood your comments, but still am getting errors:
> >
[quoted text clipped - 30 lines]
>
> in the SQL file.
jbellnewsposts@hotmail.com - 10 Feb 2010 08:02 GMT
>It is now working!  My new question is before I was having problems, I was
>storing a date in a nvarchar field and a time in another nvarchar field, but
[quoted text clipped - 38 lines]
>>
>> in the SQL file.

SQL Server is making assumptions as to what it is. I don't know who
how it  decides but there are rules for regular identifiers and it may
be related to type precedence.

Using quotes in your SQLCMD script makes sure it knows that it is a
string. If you used '[' and ']' it would be an identifier.

HTH

John
Erland Sommarskog - 10 Feb 2010 08:37 GMT
> It is now working!  My new question is before I was having problems, I
> was storing a date in a nvarchar field and a time in another nvarchar
> field, but I converted those fields to one datetime field to make
> searching between dates easier.  When I had the two nvarchar fields, I
> did not have to enclose parameters in the WHERE in quotes.  Why do I
> have to do that now?

Since I don't see the code, I will have to guess. But if you have

 datecol = 20100210

and datecol is nvarchar, there will be an implicit conversion from integer
to nvarchar. And if you use delimited format like:

 datecol = 2010/02/10

there will still be an implicit conversion, but it is unlikely that you
will hit any rows, since 2010/02/10 evaluates to 100.

Signature

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

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2010 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.