I am trying to create a flat text file using a query that concatenates multiple values from multiple tables. This result set is being inserted into a table variable which I would like to use in the xp_cmdshell call to create the text file. Here's a sample of my code.
DECLARE #tablevar table (string nvarchar(200))
INSERT INTO #tablevar
SELECT 'test' + column1 + column2
FROM SampleTable
EXEC xp_cmdshell 'bcp "SELECT * FROM #tablevar" queryout "C:\temp\output.txt" -T -c '
I get the following error when I make the xp_cmdshell call:
SQLState = 42000, NativeError = 1087
Error = [Microsoft][SQL Native Client][SQL Server]Must declare the table variable "#outputtable".
SQLState = 42000, NativeError = 8180
Error = [Microsoft][SQL Native Client][SQL Server]Statement(s) could not be prepared.
NULL
When attempting to use a temp table use this code
EXEC xp_cmdshell 'bcp "Select * From #temp" queryout "C:\temp\outputtable.txt" -T -c '
I get this error message
SQLState = 42S02, NativeError = 208
Error = [Microsoft][SQL Native Client][SQL Server]Invalid object name 'tempdb.temp'.
SQLState = 42000, NativeError = 8180
Error = [Microsoft][SQL Native Client][SQL Server]Statement(s) could not be prepared.
Is using a temp table or table variable with xp_cmdshell just not possible?
Your #table variable and #temp table are both out of scope to other connections, but global temp tables like ##my_global_temp should work.
Related
I want to rename a table. But I get an error when running the code. What is the correct code?
DECLARE #TABLE_NAME VARCHAR(100)
SET #TABLE_NAME = 'Employee_Destination'
IF EXISTS (SELECT NAME FROM SYS.tables WHERE NAME = #TABLE_NAME)
BEGIN
EXEC sp_rename #TABLE_NAME, #TABLE_NAME += '_2'
END
This error message (SSMS):
Incorrect syntax near #TABLE_NAME
This error message (SSIS Project) :
[Execute SQL Task] Error: Executing the query "DECLARE #TABLE_NAME
VARCHAR(100) DECLARE #TABLE_TE..." failed with the following error:
"An error occurred while extracting the result into a variable of type
(DBTYPE_I4)". Possible failure reasons: Problems with the query,
"ResultSet" property not set correctly, parameters not set correctly,
or connection not established correctly.
Note: I run this code in a SSIS project
Thank you.
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
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
I'm using the following query to write the data in a external file:
declare #sql varchar(8000);
select #sql = 'bcp tempdb.##hmscript out
F:\HMS\test.txt -c -t, -T -S GIT2B-01\MON'
select #sql
exec master..xp_cmdshell #sql;
When testing the bcp in cmd window I'm getting the following error:
SQLState = 37000, NativeError = 11525
Error = [Microsoft][SQL Server Native Client 11.0][SQL Server]The metadata could
not be determined because statement 'select * from ##hmscript' uses a temp table.
Somebody has a solution for this?
Thanks in advance,
David
If the table doesn't exist, you'll get this error.
Create the table and try again, and you should get what you want.
I am using SQL Server 2005 SP4. I have a stored procedure which gets data from many tables and converts it into XML and then send this XML data to output variable of the stored procedure.
Now I want to load this XML data to file in the filesystem with specific location; for this I am using the BCP utility with the following command:
BCP "exec master.[dbo].[SPFileCreationInstanceOnlyXML]" QUERYOUT "C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\PerformanceAnalyticsXML\LEOSQL2K5Apr112014128PM.xml" -c -t -T -SLEO\SQL2K5
Now this command gives me an error message when I am trying to execute from command prompt.
SQLState = S0002, NativeError = 208
Error = [Microsoft][SQL Native Client][SQL Server]Invalid object name '##traceinfo'.
I am unable to figure it out why it is reference to the temporary table. While my code for the stored procedure which I am executing from BCP is as following
ALTER procedure [dbo].[SPFileCreationInstanceOnlyXML]
as
begin
DECLARE #Result XML
exec [dbo].[BCPTableLineXML] #Result OUT
SELECT #Result AS FinalOutput
end
The most interesting thing about this I have same stored procedures on SQL Server 2008 R2 which works like a charm in other server only this one with SQL Server 2005 is giving me this error message.
The code for BCPTabelineXML is as following
the code inside BCPTableXML is as following.
ALTER procedure [dbo].[BCPPerformanceBaseLineXML]
#ALLRet xml OUTPUT
as
begin
DECLARE #ALL xml
--SQL Server Version and Server Name
DECLARE #SQLTraceFlag NVARCHAR(MAX)
create table ##traceinfo(flag varchar(20),Status varchar(10),Global varchar(10),Session varchar(10))
INSERT INTO ##traceinfo EXECUTE ('DBCC TRACESTATUS(-1)')
--select * from ##traceinfo
SET #SQLTraceFlag =
(
select * from
(
select * from ##traceinfo
) as SQLTraceFlag
for xml auto
);
--select #SQLTraceFlag
drop table ##traceinfo
set #ALL = CONVERT(XML
ISNULL(#SQLTraceFlag,''))
set #ALLRet=#ALL;
end
Thanks in advance for your help and valuable time.
Cheers,
Nic
Problem is solved using table variable instead of Temporary table. –