BCP with WHERE clause Fails - sql

When using BCP to export data from a table to an XML file this works perfectly:
declare #cmd nvarchar(255);
select #cmd = '
bcp "select AGENT,TERMCD ,MYAMOUNT,CAMPNAME,LCDATE,CAMPAIGNID from [MYDATABASE].[dbo].[CC1] row for xml auto, root(''rows''), elements" '
+ 'queryout "d:\temp\sample.xml" -S DESKTOP-2C2OAE9 -T -w -r -t';
exec xp_cmdshell #cmd;
go
However if I add a simple WHERE clause like below it fails.
declare #cmd nvarchar(255);
select #cmd = '
bcp "select AGENT,TERMCD ,MYAMOUNT,CAMPNAME,LCDATE,CAMPAIGNID from [MYDATABASE].[dbo].[CC1] WHERE AGENT=''Kelly Martens'' row for xml auto, root(''rows''), elements" '
+ 'queryout "d:\temp\sample.xml" -S DESKTOP-2C2OAE9 -T -w -r -t';
exec xp_cmdshell #cmd;
go
There are the errors:
NULL
Starting copy...
SQLState = 37000, NativeError = 102
Error = [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Incorrect syntax near 'row'.
SQLState = S1000, NativeError = 0
Error = [Microsoft][ODBC Driver 13 for SQL Server]Unable to resolve column level collations
NULL
BCP copy out failed
NULL
I am actually going to have to use LCDATE which is varchar to compare to the current date but I wanted to do something simple initially since I am having problems but if you have thoughts on that too it would be welcome.

Your 'row' table alias moved to after the where, it should be after the table name
declare #cmd nvarchar(255);
select #cmd = '
bcp "select AGENT,TERMCD ,MYAMOUNT,CAMPNAME,LCDATE,CAMPAIGNID from [MYDATABASE].[dbo].[CC1] row WHERE AGENT=''Kelly Martens'' for xml auto, root(''rows''), elements" '
+ 'queryout "d:\temp\sample.xml" -S DESKTOP-2C2OAE9 -T -w -r -t';
exec xp_cmdshell #cmd;
go

Related

Error in exec master xp_cmdshell BCP queryout

I need to create some XML from a SQL query. I need to increment a name file but with my code the system return the error
Messaggio 102, livello 15, stato 1, riga 89
Incorrect syntax near '+'.
This error refers to the code part in
exec master.. `'+#num'+`
Below the code; the variable #num is of type nvarchar
SET #num = (SELECT CAST([incr_num] AS NVARCHAR(10)) FROM [DB01].[dbo].[NUM_XML])
EXEC master..xp_cmdshell 'bcp "SELECT #header,#inland FOR XML RAW(''''),ROOT(''root''), ELEMENTS, TYPE" queryout "\\server01\TEMP_SW\XML_TMS\test_'+#num+'.xml" -U xx -P xxxxx -c -C ANSI -t;'
I try to insert all path in another variable and attach this new variable in exec function, but the result not change
Can you help me?
Create command text first
declare #cmd varchar(2000) = 'bcp "SELECT #header,#inland FOR XML RAW(''''),ROOT(''root''), ELEMENTS, TYPE" queryout "\\server01\TEMP_SW\XML_TMS\test_'+#num+'.xml" -U xx -P xxxxx -c -C ANSI -t;' ;
exec master..xp_cmdshell #cmd;
Now if I execute the single query
SELECT #header,#inland FOR XML RAW(''),ROOT('root'), ELEMENTS, TYPE
I receive the correct XML data.
But if i insert the query into exec master..xp_cmdshell
declare #cmd varchar(2000) = 'bcp "SELECT #header,#inland FOR XML RAW(''''),ROOT(''root''), ELEMENTS, TYPE" queryout "\\server01\TEMP_SW\XML_TMS\test_'+#num+'.xml" -U xx -P xxxxx -c -C ANSI -t;' ;
exec master..xp_cmdshell #cmd;
The system return the error
SQLState = 37000, NativeError = 137
Error = [Microsoft][SQL Native Client][SQL Server]Must declare the scalar variable "#header".
SQLState = 37000, NativeError = 8180
Error = [Microsoft][SQL Native Client][SQL Server]Statement(s) could not be prepared.
NULL
#header is declared as XML
Let me know thanks and regards

Problems with bcp utility

I want to extract a SELCET query with the bcp utility. But it doesn't work. MS SQL underlines "bcp" marks it.
DECLARE #DBName NVARCHAR(MAX);
SET #DBName = (SELECT name FROM master.dbo.sysdatabases where name LIKE '%NAV%');
EXECUTE ('USE [' + #DBName+']');
PRINT (' DATABASE SELECTED: '+ #DBName)
bcp "SELECT [Role ID],[Name] FROM [dbo].[Permission Set]" out "C:\Users\Public\Documents\test.txt" -c -T
The error message is:
Msg 102, Level 15, State 1, Line 19
Incorrect syntax near 'bcp'.
Does anybody know the correct syntax? If I compare it with the examples on the docs.microsoft website (https://learn.microsoft.com/en-us/sql/tools/bcp-utility#o) I do not see my mistake.
You need to use xp_cmdshell procedure to execute the bcp command in T-SQL script
Declare #sql varchar(8000)
Select #sql = 'bcp "SELECT [Role ID],[Name] FROM [dbo].[Permission Set]" out "C:\Users\Public\Documents\test.txt" -c -T'
Exec master..xp_cmdshell #sql
bcp is a command line utility. It runs from a shell script (typically), not from T-SQL.
Within SQL code, you would normally use bulk insert. See the documentation here.

Return BCP Output as a message from SQL Server 2012 (read by MATLAB)

I create a table in my stored procedure, export its content using BCP and then drop this table. BCP command looks like:
SET #sqlop = 'BCP "SELECT blah FROM MYTABLE" queryout "' + #txt4print*
+ '" -b 50000 -c -t"," -S"server" -U"uname" -P"pswd"'
exec master..xp_cmdshell #sqlop --, NO_OUTPUT
Typically, if there is an error in the store procedure, it is printed as a Message in SQL Server. However, the output from BCP is not printed as a message. So I'm not able to capture the error instance in MATLAB. Is there a way to catch the string error as shown below? Can this output be returned as a Message from SQL Server?
I'm not an advanced SQL programmer. Thanks!
Well you have output here so you should be able to capture that output and then manually force an error from it.
DECLARE #Output TABLE (OutputMessage NVARCHAR(4000));
SET #sqlop = 'BCP "SELECT blah FROM MYTABLE" queryout "' + #txt4print*
+ '" -b 50000 -c -t"," -S"server" -U"uname" -P"pswd"'
INSERT INTO #Output
exec master..xp_cmdshell #sqlop --, NO_OUTPUT
DELETE FROM #Output WHERE OutputMessage IS NULL
DECLARE #Statement NVARCHAR(MAX)
WHILE (SELECT COUNT(*) FROM #Output) > 0
BEGIN
SELECT TOP 1 #Statement = OutputMessage FROM #Output
IF #Statement LIKE '%Error%'
BEGIN
SET #Statement = 'Unexpected error in procedure: ' + #Statement
RAISERROR(#Statement, 16, 1)
END
END
Alternatively if you just want a message and not an error, you should be able to use PRINT for similar effect.
http://technet.microsoft.com/en-us/library/ms176047.aspx

Using bcp utility to export SQL queries to a text file

I debug a stored procedure (SQL Server 2005) and I need to find out some values in a datatable.
The procedure is run by an event of the application and I watch just the debugging output.
I do the following my stored procedure (SQL Server 2005), I took a system table (master.dbo.spt_values) as example:
set #logtext = 'select name, type from master.dbo.spt_values where number=6'
--set #logtext = 'master.dbo.spt_values'
SET #cmd = 'bcp ' + #logtext + ' out "c:\spt_values.dat" -U uId -P uPass -c'
EXEC master..XP_CMDSHELL #cmd
So, when I uncomment the second like everything works, a file apprears on the C:\ drive... but if I coment it back leaving only the first line, any output is generated.
How to fix this problem?
bcp out exports tables.
To export a query use queryout instead - you'll need to wrap your query in "double quotes"
set #logtext = '"select name, type from master.dbo.spt_values where number=6"'
--set #logtext = 'master.dbo.spt_values'
SET #cmd = 'bcp ' + #logtext + ' queryout "c:\spt_values.dat" -U uId -P uPass -c'
EXEC master..XP_CMDSHELL #cmd
http://msdn.microsoft.com/en-us/library/ms162802.aspx

TSQL QUERY to dump table into a file?

I want to dump a tale into a text file I am using following command -
bcp tablename out myTable.dat -T -C
But got error it says incorrect symbol near bcp.
I am not sure why I am getting this.
From #Code Monkey's Link : http://www.simple-talk.com/sql/database-administration/creating-csv-files-using-bcp-and-stored-procedures/
try this
declare #sql varchar(8000)
select #sql = 'bcp [dbname].[dbo].[tablename] out myTable.dat -c -t, -T -S'+ ##servernameexec
master..xp_cmdshell #sql
where dbname is the name of your database, and replace dbo with the name of the schema if not dbo