BCP on global ##temp table error - sql

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.

Related

Rename table in SQL Server

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.

BCP with WHERE clause Fails

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

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.

Using Table Variables with xp_cmdshell

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.