All,
Here I have a simple query that can be run on anyone's machine with SQL Server where a simple table is created and then I try to export the contents of this to an XML file via BCP. All you have to do is replace @DatabaseName, @filename, @query with values pertaining to your machine.
When I run query 1 the error message is
SQLState = S1000, NativeError = 0 Error = [Microsoft][SQL Server Native Client 11.0]Unable to open BCP host data-file.
When I run query 2 or 3 the error message is
bcp.exe: unknown option A usage: bcp.exe {dbtable | query} {in | out | queryout | format} datafile.
I am running this from the SQL Server Management Studio. I really need a solution to this, please help.
DECLARE @cmd As VARCHAR(8000)
use CP
IF EXISTS(SELECT * FROM dbo.Dummy) DROP TABLE dbo.Dummy
Declare @DatabaseName VARCHAR(200)
Declare @filename VARCHAR(200)
Declare @query As varchar(max)
set @DatabaseName = 'ALSCG-JPATHIL\SQLEXPRESS'
set @filename = 'C:\Users\jpathil\Desktop\sample.xml'
set @query = 'SELECT * FROM [CP].[dbo].[Dummy]'
CREATE TABLE Dummy (BCP_Sucks VARCHAR(200),);
INSERT INTO Dummy VALUES ('Jean');
INSERT INTO Dummy VALUES ('Derrick')
Declare @teststring VARCHAR(2000)
-- Query 1
--set @teststring = 'bcp.exe ' + '"' + @query + '" ' + 'queryout' + ' "' + @filename + '" ' + '-c -T -x -S ' + @DatabaseName
--Query 2
--set @teststring = 'bcp.exe ' + '"' + @query + '" ' + 'queryout' + ' "' + @filename + '" ' + '-c -T -x' + @DatabaseName
--Query 3
set @teststring = 'bcp.exe ' + '"' + @query + '" ' + 'queryout' + ' "' + @filename + '" ' + '-c -x' + @DatabaseName
SET @cmd = @teststring
EXEC xp_cmdshell @cmd;
PRINT @teststringto see if the format of the command is correct?