Solved

Output sql script to file

Posted on 2007-03-29
8
387 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

688 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