Solved

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

Posted on 2012-03-10
1
373 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 142

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

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…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how the fundamental information of how to create a table.

808 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