Solved

Output sql script to file

Posted on 2007-03-29
8
384 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
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.

 
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

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…
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.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

775 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