Solved

Output sql script to file

Posted on 2007-03-29
8
383 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
  • 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
 
LVL 16

Expert Comment

by:rboyd56
ID: 18817402
What error?
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

932 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now