Solved

Outputfile to csv from sql

Posted on 2007-03-30
6
717 Views
Last Modified: 2008-01-09
I am getting the error Server: Msg 170, Level 15, State 1, Line 4
Line 4: Incorrect syntax near 'English'.  This query runs find with just the select statement but I am trying to output it to a csv file daily.   I  would appreicate any help.  Thanks


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
Comment
Question by:running32
  • 3
  • 3
6 Comments
 
LVL 13

Expert Comment

by:Atlanta_Mike
ID: 18823138
You need to use two quotes around English because it is inside a string. Something like this:

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
 

Author Comment

by:running32
ID: 18823195
I received the error below

Server: Msg 170, Level 15, State 1, Line 4
Line 4: Incorrect syntax near 'when bytlanguage = 1 THEN 'English'              
                        when bytlanguage = 4 THEN 'English'  end as 'Languag'.
0
 

Author Comment

by:running32
ID: 18823207
I found that problem but I still get the error when I use the new code you gave me.  

Msg 170, Level 15, State 1, Server BC-BIZTALK, Line 1
Line 1: Incorrect syntax near 'English'.
NULL
0
[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

 
LVL 13

Accepted Solution

by:
Atlanta_Mike earned 500 total points
ID: 18823441
Make sure you have two quotes where there should be one (inside the string) ... look at Spanish...
0
 
LVL 13

Expert Comment

by:Atlanta_Mike
ID: 18823447
See the problem?

''Spanish' 'when
0
 

Author Comment

by:running32
ID: 18823974
Below is the code I tried to run.  Thanks

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

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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.
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.

896 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

11 Experts available now in Live!

Get 1:1 Help Now