How do I execute an sql script via the command prompt?

glit
glit used Ask the Experts™
on
Hi All,

I am trying to execute an sql script (which consists of a simple select query) via the command prompt using -

osql -E -i c:\query1.sql

When attempting this, I get an error -

1> 2> Msg 102, Level 15, State 1, Server PADSVR038, Line 1
Incorrect syntax near '''.

Any ideas?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
SQL Server DBA & Architect, EE Solution Guide
Awarded 2009
Distinguished Expert 2018
Commented:
Since you use SQL Server 2005, Would recommend using SQLCMD instead of the deprecated osql..

http://msdn.microsoft.com/en-us/library/ms162773.aspx

>> Incorrect syntax near '''.

Seems like an syntax error in your query1.sql file..
Posting the contents would help us to fix it out..

Author

Commented:
Thanks, I'll give the sqlcmd a go.

As for the sql script, I ran it within the sql server management studio and works fine.  I have included the code.
SELECT        STPIJCURNT_CNT.ReceiverName1, STPIJCURNT_CNT.ReceiverName2, STPIJCURNT_CNT.ReceiverAddress1, STPIJCURNT_CNT.ReceiverAddress2, 
                         STPIJCURNT_CNT.ReceiverLocation, STPIJCURNT_CNT.ReceiverState, STPIJCURNT_CNT.ReceiverPostcode, STPIJCURNT_CNT.ReceiverAccountNumber, 
                         STPIJCURNT_CNT.CarrierAbbreviation, STPIJCURNT_CNT.DespatchDate, STPIJCURNT_DST.UnitCode, STPIJCURNT_DST.Quantity, STPIJCURNT_DST.TotalCubic, 
                         STPIJCURNT_DST.Weight, STPIJCURNT_CDK.ClientDefinedKey, STPIJCURNT_DST.DespatchNumber, STPIJCURNT_CNT.ConnoteNumber
FROM            STPIJCURNT_CDK INNER JOIN
                         STPIJCURNT_DST ON STPIJCURNT_CDK.DespatchNumber = STPIJCURNT_DST.DespatchNumber INNER JOIN
                         STPIJCURNT_CNT ON STPIJCURNT_DST.ConnoteNumber = STPIJCURNT_CNT.ConnoteNumber
WHERE         (STPIJCURNT_CNT.CarrierAbbreviation = 'PL1') AND (STPIJCURNT_CNT.DespatchDate = CONVERT(VARCHAR(10), GETDATE(), 111))

Open in new window

Author

Commented:
Thanks rrjegan17 !

The sqlcmd worked :)
Raja Jegan RSQL Server DBA & Architect, EE Solution Guide
Awarded 2009
Distinguished Expert 2018

Commented:
Welcome..

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial