Solved

Why won't my results save to the xml path I specified in my stored procedure?

Posted on 2012-03-10
1
376 Views
Last Modified: 2012-04-12
Here is my code:

 EXEC master..xp_cmdshell
               'bcp "SELECT title
                                   ,author
                                   ,location
                                   ,ISBN
 FROM    libraryInventory
 WHERE  inventory_level > 2            
 for xml auto, elements, root(''location'')
               "
               queryout \\main\libraries\locations\inventories.xml -x'

Instead of outputting my result to the above path, this is what I get as a result:
usage: bcp {dbtable | query} {in | out | queryout | format} datafile
  [-m maxerrors]            [-f formatfile]          [-e errfile]
  [-F firstrow]             [-L lastrow]             [-b batchsize]
  [-n native type]          [-c character type]      [-w wide character type]
  [-N keep non-text native] [-V file format version] [-q quoted identifier]
  [-C code page specifier]  [-t field terminator]    [-r row terminator]
  [-i inputfile]            [-o outfile]             [-a packetsize]
  [-S server name]          [-U username]            [-P password]
  [-T trusted connection]   [-v version]             [-R regional enable]
  [-k keep null values]     [-E keep identity values]
  [-h "load hints"]         [-x generate xml format file]
NULL
What am I doing wrong?
0
Comment
Question by:programmher
1 Comment
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 125 total points
ID: 37705006
remove the newlines from your sql...

EXEC master..xp_cmdshell  'bcp "SELECT title ,author,location,ISBN FROM    libraryInventory WHERE  inventory_level > 2   for xml auto, elements, root(''location'') " queryout "\\main\libraries\locations\inventories.xml" -x' 

Open in new window


you might consider moving the query into a view, and just use the view name (as table name) instead, this will shorten this code.

you could build the "sql" into a variable, if it's about readability

declare @cmd varchar(1000)
set @cmd = 'bcp "SELECT title ,author,location,ISBN '
set @cmd = @cmd + ' FROM    libraryInventory WHERE  inventory_level > 2 '
set @cmd = @cmd + ' for xml auto, elements, root(''location'') " '
set @cmd = @cmd + ' queryout "\\main\libraries\locations\inventories.xml" -x' 
exec(@cmd)

Open in new window


hope this helps
0

Featured Post

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.

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how the fundamental information of how to create a table.

809 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