1

I am trying to export the results of my query to a csv file but for some reason, I get this error from SQL Server:

Msg 156, Level 15, State 1, Line 32
Incorrect syntax near the keyword 'INTO'

I can't figure out why or how to troubleshoot this.

WITH q AS
(
    SELECT 
        A.ExternalID,
        Sheet = A.UserDefinedXml.value('(//SkinSheet/@Label)[1]', 'varchar(3)'),
        LOA_Request_Sent = A.UserDefinedXml.value('(//SentDate/@Value)[1]', 'varchar(12)'),
        LOA_Received = A.UserDefinedXml.value('(//ReceivedDate/@Value)[1]', 'varchar(12)'),
        LOA_Discontinued_Date = A.UserDefinedXml.value('(//DicontinuedDate/@Value)[1]', 'varchar(12)'),
        LOA_Disc_Reason = A.UserDefinedXml.value('(//ReasonDiscontinued/@Value)[1]', 'VARCHAR(MAX)'),
        LOA_InsuranceStatus = A.UserDefinedXml.value('(//InsuranceStatus/@Label)[1]', 'VARCHAR(MAX)'),
        LOA_NoOONIns = A.UserDefinedXml.value('(//NoOONBenefitsInsCompany/@Value)[1]', 'VARCHAR(MAX)'),
        LOA_NotMedNecIns = A.UserDefinedXml.value('(//NotMedNecessaryInsCompany/@Value)[1]', 'VARCHAR(MAX)'),
        LOA_FacilityRefused = A.UserDefinedXml.value('(//FacilityRefused/@Value)[1]', 'varchar(5)'),
        LOA_OtherDenial = A.UserDefinedXml.value('(//ReasonDenied/@Value)[1]', 'varchar(5)'),
        LOA_OtherDenialReason = A.UserDefinedXml.value('(//OtherReasonDenied/@Value)[1]', 'varchar(max)'),
        AuthorizationReceived = A.UserDefinedXml.value('(//AuthorizationToTreatReceivedDate/@Value)[1]','varchar(12)'),
        POReceived = A.UserDefinedXml.value('(//POReceivedDate/@Value)[1]', 'varchar(12)'),
        ICRecieved = A.UserDefinedXml.value('(//InformedConsentReceivedDate/@Value)[1]', 'varchar(12)'),
        A.ServiceSiteUid, 
        A.LastModifiedDate, 
        PersonUid_A = A.PersonUid,
        B.FirstName, B.LastName, 
        PersonUid_B = B.PersonUid,
        C.Name
    FROM
        Patient A
    INNER JOIN 
        Person B ON B.PersonUid = A.PersonUid
    INNER JOIN  
        ListServiceSite C ON C.ServiceSiteUid = A.ServiceSiteUid 
)
SELECT 
    Name, ExternalID, FirstName, LastName, POReceived, 
    ICRecieved, AuthorizationReceived, LOA_Request_Sent, LOA_Received, 
    LOA_FacilityRefused, LOA_OtherDenial, Sheet 
FROM 
    q
WHERE 
    Sheet = 'Yes'
INTO OUTFILE 'C:\Users\Intern\Desktop\Sheet.csv'
  FIELDS TERMINATED BY ','
  LINES TERMINATED BY '\n';
regents
  • 600
  • 6
  • 15

1 Answers1

0

Into comes before the from clause:

  SELECT Name, ExternalID, FirstName, LastName, POReceived, ICRecieved, AuthorizationReceived, LOA_Request_Sent, LOA_Received, LOA_FacilityRefused, LOA_OtherDenial, Sheet 
  INTO OUTFILE 'C:\Users\Intern\Desktop\Sheet.csv'  
  FIELDS TERMINATED BY ','
  LINES TERMINATED BY '\n';
  FROM q
  WHERE Sheet = 'Yes'
SQL_M
  • 2,455
  • 2
  • 16
  • 30
  • As a sidenote: I have never used this kind of output function. Why not use the simpler import export wizard? – SQL_M Feb 08 '19 at 14:07
  • Because it doesn't exist in SQL Server. – Jacob H Feb 08 '19 at 14:08
  • 1
    Uhh, -- task - import data - and so on. Are we talking about the same thing here? – SQL_M Feb 08 '19 at 14:12
  • I mean, you've basically answered it yourself. The post is tagged SQL Server. There is no such "OUTFILE" command in SQL Server/T-SQL. Hence why you've never used it, and in your own comments on your answer you suggest using a Import/Export utility. Right? – Jacob H Feb 08 '19 at 14:15
  • I just figured it was an option that I never used before. So the OP mis-tagged it as SQL Server? – SQL_M Feb 08 '19 at 14:16
  • I'm not sure, that's why I asked in the comments :) Your answer is correct, if OP uses MySQL. If not, they need to go [here](https://stackoverflow.com/questions/20270674/how-to-use-sql-outfile-command-correctly) or [here](https://stackoverflow.com/questions/6354130/is-there-a-select-into-outfile-equivalent-in-sql-server-management-studio) – Jacob H Feb 08 '19 at 14:16