Solved

Cannot save XML query to a file using xp_cmdshell

Posted on 2013-06-24
1
342 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
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
This query failed in sql 2014 5 29
sql server computed columns 11 29
SSRS 2013 - Overlapping reports 2 19
Get the latest status 8 29
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

813 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

20 Experts available now in Live!

Get 1:1 Help Now