Solved

Cannot save XML query to a file using xp_cmdshell

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

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

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

705 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

15 Experts available now in Live!

Get 1:1 Help Now