Solved

Outputfile to csv from sql

Posted on 2007-03-30
6
714 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
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

 
LVL 13

Accepted Solution

by:
Atlanta_Mike earned 500 total points
Comment Utility
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
Comment Utility
See the problem?

''Spanish' 'when
0
 

Author Comment

by:running32
Comment Utility
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

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

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 …
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

771 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