Solved

Cannot save XML query to a file using xp_cmdshell

Posted on 2013-06-24
1
361 Views
Last Modified: 2013-06-24
I have a query that exports resumes to an XML file. The query has been tested and runs without error and produces the XML file as requested by my client. I need to be able to send the results to a file, though, as the final run will have over 500,000 resumes. However, when I put the query in a string and send it to the xp_cmdshell command line, it does not do anything. It does not even send an error. I get a return with parameter information. However, I know the parameters are correct, since when I run the command on a simpler query, it works and the file is created. Also, selecting the string for the XML query and running that, returns the proper results. I do not know, therefore, what is wrong.

Here is the complete sql:

DECLARE @cmd nvarchar(4000), @FileOut nvarchar(200)
SELECT  @FileOut = 'G:\Resumes\XML\Binary5.xml'
SELECT @cmd = 'bcp ' +
'"DECLARE @soapHeader VARCHAR(1000)
DECLARE @soapFooter VARCHAR(1000)
DECLARE @JobSeeker VARCHAR(1000)
DECLARE @Resumes xml
SET @soapHeader=
''<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
  <SOAP-ENV:Header>
    <mh:MonsterHeader xmlns:mh="http://schemas.monster.com/MonsterHeader">
      <mh:MessageData>
        <mh:MessageId>RCI AAS Resumes</mh:MessageId>
        <mh:Timestamp>'+CONVERT(varchar(20),getdate())+'</mh:Timestamp>
      </mh:MessageData>
      <mh:ProcessingReceiptRequest>
        <mh:Address transportType="http">http</mh:Address>
      </mh:ProcessingReceiptRequest>
    </mh:MonsterHeader>
    <wsse:Security xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/04/secext">
      <wsse:UsernameToken>
        <wsse:Username>miteparallon</wsse:Username>
        <wsse:Password>mEzNFl@0</wsse:Password>
      </wsse:UsernameToken>
    </wsse:Security>
  </SOAP-ENV:Header>
  <SOAP-ENV:Body>''
SET @JobSeeker=            ''<JobSeekers xsi:schemaLocation="http://schemas.monster.com/Monster http://schemas.monster.com/current/xsd/JobSeeker.xsd" xmlns="http://schemas.monster.com/Monster" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">''
SET @soapFooter=
''      </JobSeekers>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>'''+
' SELECT CAST(@soapHeader+@JobSeeker+
(SELECT
''addOrUpdate'' AS ''@seekerAction'',
a.Resume_ID AS ''@seekerRefCode'',
''active'' AS ''@status'',
''6722'' AS ''Channel/@monsterId'',
a.FName AS ''PersonalData/Contact/StructuredName/GivenName'',
a.LName AS ''PersonalData/Contact/StructuredName/FamilyName'',
a.Address AS ''PersonalData/Contact/Address/StreetAddress'',
a.Address2 AS ''PersonalData/Contact/Address/StreetAddress2'',
a.City AS ''PersonalData/Contact/Address/City'',  
REPLACE(a.State,''US-'','''') AS ''PersonalData/Contact/Address/State'',
a.Country AS ''PersonalData/Contact/Address/CountryCode'',
a.Zip AS ''PersonalData/Contact/Address/PostalCode'',
''home'' AS ''PersonalData/Contact/Phones/Phone/@phoneType'',
a.Phone AS ''PersonalData/Contact/Phones/Phone'',
a.Email AS ''PersonalData/Contact/E-mail'',
''addOrUpdate'' AS ''Resumes/Resume/@resumeAction'',
SUBSTRING(RTRIM(LTRIM(a.Resume_ID)),1,200) AS ''Resumes/Resume/@resumeRefCode'',
''7790'' AS ''Resumes/Resume/BoardName/@monsterId'',
a.DateModified AS ''Resumes/Resume/ResumeModDate'',
''add'' AS ''Resumes/Resume/ResumeDocument/@documentAction'',
d.OrigDoc AS ''Resumes/Resume/ResumeDocument/File'',
d.FileName AS ''Resumes/Resume/ResumeDocument/FileName''
FROM Resumes a
LEFT OUTER JOIN ResReferrer r on r.ResID = a.Resume_ID
LEFT OUTER JOIN _Referrers b on b.RefID = r.RefID
INNER JOIN (
SELECT DocID, ResID, OrigDoc, FileName, ROW_NUMBER() OVER (ORDER BY ResID DESC) AS RowNumber
FROM ResDocuments
) d on d.ResID = a.Resume_ID
WHERE LEN(a.Email) > 0 AND a.State LIKE ''US-%''
AND a.Zip IN (SELECT ZipCode FROM ats_Master.dbo.USZipCodes)
AND a.Email LIKE ''%@%.%''
AND DATEADD(yyyy,-3,getdate()) < DateModified
AND d.RowNumber BETWEEN 1001 AND 1500
FOR XML PATH (''JobSeeker''),
ELEMENTS )+@soapFooter
AS XML)"' +' queryout '+@FileOut+' -w -T -S ' + @@SERVERNAME

--SELECT @cmd
EXECUTE master..xp_cmdshell @cmd

Again, if I uncomment the SELECT statement, I get a valid SQL query that runs perfectly. If I change @cmd to a simpler query, cmdshell creates the file.
0
Comment
Question by:mfkaminski
[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
1 Comment
 

Accepted Solution

by:
mfkaminski earned 0 total points
ID: 39273180
OK, I got this working. You need to put the entire XML query into a stored procedure and then run that as the bcp command.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
I have a large data set and a SSIS package. How can I load this file in multi threading?
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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.

622 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