Solved

Output sql script to file

Posted on 2007-03-29
8
382 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
What error?
0
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 

Author Comment

by:running32
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Try putting this query in a view and pass the view as the parameter to /Q to see if it works.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

743 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

17 Experts available now in Live!

Get 1:1 Help Now