Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Output sql script to file

Posted on 2007-03-29
8
Medium Priority
?
389 Views
Last Modified: 2011-10-03
I would like to output a file to a text file.   I am getting a couple of errors on the script below.  I would be greatful for some input.  Thanks

Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near '/'.
Server: Msg 103, Level 15, State 1, Line 1
The identifier that starts with 'SELECT  lastname, firstname,case when bytlanguage = 2 then 'English' when bytlanguage = 3 THEN 'Spanish' when bytlanguage = 1 TH' is too long. Maximum length is 128.

********script **************


EXEC ('isql /U username /P pw /q "SELECT  lastname, firstname,case when bytlanguage = 2 then ''English'' when bytlanguage = 3 THEN ''Spanish'' when bytlanguage = 1 THEN ''English''              
                        when bytlanguage = 4 THEN ''English''  end as ''Language'', appt.phone, CONVERT(VARCHAR,APPTDATE, 101) as apptdate, time_hour, time_minute,apptreason,  name_last, name_first,appt.prprovid  FROM DDB_APPT_BASE as appt inner join ddb_pat_base as pat on appt.patid = pat.patid inner join ddb_rsc_base as rsc on appt.prprovid = rsc.urscid inner join tblpatient as p on appt.chart = p.strpatientid
WHERE CONVERT(VARCHAR,APPTDATE, 101) = CONVERT(VARCHAR,DateAdd(dd,
   CASE WHEN DATEPART(weekday, getdate()) IN ( 5,6) THEN  4 ELSE 2 END
  , GetDate()), 101) and rsc.defaultclinic = 30
ORDER BY APPTDATE DESC" /o C:\OutputFileName.txt')
0
Comment
Question by:running32
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 16

Expert Comment

by:rboyd56
ID: 18817320
You cannot use EXEC to execute a command line utility. You have to use xp_cmdshell to execute the ISQL command. Also I would recommend using the /Q parameter instead of /q. ?Q will close the ISQL connection after the query runs. Also with the double quotes involved I would create a view or a stored procedure with your select statemtent and then use that as the value for the /Q parameter.
0
 
LVL 16

Expert Comment

by:rboyd56
ID: 18817326
This statemnt:

?Q will close the ISQL connection after the query runs.

should have been:

/Q will close the ISQL connection after the query runs.
0
 

Author Comment

by:running32
ID: 18817350
If I try and run the script below I get the error Incorrect syntax near English.   I have tried ''  and "" 


declare @filename varchar(100)
declare @cmd varchar (1000)
set @filename='C:\testsqloutput.txt'
Set @cmd = 'osql -h -E -d healthdistrictQnA /Q "SELECT  lastname, firstname,case when bytlanguage = 2 then English  when bytlanguage = 3 THEN Spanish when bytlanguage = 1 THEN English              
                        when bytlanguage = 4 THEN English  end as Language, appt.phone, CONVERT(VARCHAR,APPTDATE, 101) as apptdate, time_hour, time_minute,apptreason,  name_last, name_first,appt.prprovid  FROM DDB_APPT_BASE as appt inner join ddb_pat_base as pat on appt.patid = pat.patid inner join ddb_rsc_base as rsc on appt.prprovid = rsc.urscid inner join tblpatient as p on appt.chart = p.strpatientid
WHERE CONVERT(VARCHAR,APPTDATE, 101) = CONVERT(VARCHAR,DateAdd(dd,
   CASE WHEN DATEPART(weekday, getdate()) IN ( 5,6) THEN  4 ELSE 2 END
  , GetDate()), 101) and rsc.defaultclinic = 30
ORDER BY APPTDATE DESC"  /o ' + @filename
exec master.dbo.xp_cmdshell @cmd
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 16

Expert Comment

by:rboyd56
ID: 18817402
What error?
0
 

Author Comment

by:running32
ID: 18817420
Msg 170, Level 15, State 1, Server BC-BIZTALK, Line 1
Line 1: Incorrect syntax near 'English'.
NULL
0
 
LVL 10

Expert Comment

by:lahousden
ID: 18817582
Are English and Spanish columns in one of the tables?  If not then you are probable forgetting to put them in quotes as literals...

e.g.:

SELECT  lastname, firstname,case when bytlanguage = 2 then 'English'  when bytlanguage = 3 THEN ...
0
 

Author Comment

by:running32
ID: 18818486
if I add ' I get the error Server: Msg 170, Level 15, State 1, Line 4
Line 4: Incorrect syntax near 'English'.
0
 
LVL 16

Accepted Solution

by:
rboyd56 earned 2000 total points
ID: 18818534
Try putting this query in a view and pass the view as the parameter to /Q to see if it works.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question