I'm using MS SQL Server 2016 SP2.
I have a table that contains two columns like this:
Filename XML
--------- --------------------
c:myfoldertest.xml <?xml version blah blah blah
c:myfoldertest2.xml <?xml version blah blah blah
c:myfoldertest3.xml <?xml version blah blah blah
Using SQL I want to export the XML in the XML column and create the file from the Filename column. I have tried using a cursor and BCP as shown below, but I keep getting the message:
Copy direction must be either 'in', 'out' or 'format'. usage: bcp {dbtable | query} {in | out | queryout | format} datafile
This is my SQL - where am I going wrong?
Thanks for any help.
DECLARE @name VARCHAR(MAX)
DECLARE db_cursor CURSOR FOR
SELECT [Filename] FROM Instances
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @NAME
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC xp_cmdshell 'bcp -S xxxxxxx -d xxxxxxxxx "SELECT [XML] FROM Instances WHERE [Filename] = @NAME" -o @NAME -T -c -t -x'
FETCH NEXT FROM db_cursor INTO @NAME
END
CLOSE db_cursor
DEALLOCATE db_cursor