Dynamic file creation with BCP Utility - sql-server

I'm using BCP Utility to copy records out of table before deleting the records.
The function is working just fine, however, I need to copy the records to a new file for the every time I delete, instead of override the same file (as it is now).
It could be creating a new file with timestamp as prefix or something similar.
Any ideas?
My code
Declare #cmd varchar(1000) = 'bcp "select * from ##DeletedRecords" queryout
"C:\Delete\DeletedRecord.txt" -t, -c -T'
print #cmd
EXEC master..XP_CMDSHELL #cmd

just change the filename in the BCP command accordingly by appending date & time to the filename
example :
Declare #cmd varchar(1000);
select #cmd = 'bcp "select * from ##DeletedRecords" queryout '
+ '"C:\Delete\DeletedRecord'
+ convert(varchar(10), getdate(), 112) -- YYYYMMDD
+ replace(convert(varchar(10), getdate(), 108), ':', '') -- HHMMSS
+ '.txt" -t, -c -T'
print #cmd
EXEC master..XP_CMDSHELL #cmd

Related

BCP - Export CSV with header

I have run the following query to export my Ms SQL table as CSV. It working good. Now I want to add the field name as the first row. How is it possible?
declare #sql varchar(8000)
select #sql = 'bcp "select * from test_table" queryout C:\Test_SP\Tom.csv -c -t, -T -S' + ##servername
exec master..xp_cmdshell #sql
I know that I can specify the names #Red Devil answered. But the table is dynamic, Its fields are not fixed, It will change. I am trying to find a method to fetch the field names from the table definition and prepend it into the result CSV
Try this:
declare #sql varchar(8000)
select #sql = 'bcp "select 'col1', 'col2',... union all select * from test_table" queryout C:\Test_SP\Tom.csv -c -t, -T -S' + ##servername
exec master..xp_cmdshell #sql

SQL Server 2008 R2: Export data using bcp command and store bcp output into variable

I want to export data using bcp command and the output of the bcp command should be stored in the variable.
My try:
DECLARE #Result varchar(max)
DECLARE #SQL nvarchar(max)
SET #SQL = N'Execute xp_cmdshell ''bcp "SELECT * FROM EMP" QueryOut "E:\BCP\Result.pec" -T -t#_# -c -o "E:\BCP\LogReport.txt"'''
EXEC sp_executesql #SQL, N'#Result nvarchar(75) OUTPUT', #Result =#Result output
PRINT(#Result)
But getting an error in the output:
output
------------------------------------------------------------------------------
bcp: Unable to open output file E:\BCP\LogReport.txt: No such file or directory
NULL
Questions:
1. How to store the above output result into the variable?
2. Given permission to the file and folder too, but still getting this error.
---------------------------------------- 1 -------------------------------------------
DECLARE #Result TABLE
(error_msg VARCHAR(800))
DECLARE
#SQL varchar(8000),
#Result_var VARCHAR(MAX)=''
SET #SQL = N'bcp "SELECT * FROM <MY_DATABASE>.<MY_SCHEMA>.<MY_TABLE>" QueryOut "E:\BCP\Result.pec" -T -t#_# -c -o "E:\BCP\LogReport.txt"'''
-- Insert the output from xp_cmdshell into the table #Result
INSERT INTO #Result (error_msg)
EXEC xp_cmdshell #SQL
-- Merge the rows
SELECT #Result_var = #Result_var + ' ' + ISNULL(error_msg,'') FROM #Result
-- Output in variable
PRINT #Result_var
--------------------------------------------------------------------------------------
2. For me, your command works, check access to folders and subfolders (for database user), check permissions on the sql server.

Why do I get an error in bcp query out where clause?

I'm new to SQL Server and write this query for save select result into csv file:
declare #Cycle_ID as int
set #Cycle_ID = 0
EXECUTE master.dbo.xp_cmdshell 'bcp "select [Telno],[Cycle],[Price] FROM [ClubEatc].[dbo].[CycleAnalysisTable] where cast([Price] as float)>'+ #Cycle_ID +' " queryout d:\download\behi.csv -t"|" -c -S VM_TAZMINDARAMA -U behzad -P beh1368421'
In where clause I write simple variable, but I get this error:
Incorrect syntax near '+'.
Please don't decrease my question! I'm new! Thanks
SQL Server doesn't recognize expressions in exec statements. So, try setting up the query first in a variable and using that:
declare #Cycle_ID as int;
set #Cycle_ID = 0;
declare #sql nvarchar(max);
set #sql = '
bcp "select [Telno],[Cycle],[Price] FROM [ClubEatc].[dbo].[CycleAnalysisTable] where cast([Price] as float)>'+ cast(#Cycle_ID as varchar(255)) +' ";
EXECUTE master.dbo.xp_cmdshell #sql queryout d:\download\behi.csv -t"|" -c -S VM_TAZMINDARAMA -U behzad -P beh1368421';
It seems curious to me that you are comparing a column called Price to a variable called #Cycle_ID, but that has nothing to do with the syntax issue.

Stored Procedure results into a file only if not null

I am trying to execute Stored Procedure on a regular basis using a SQL Job and store the results of this into a .txt file in a folder location.
I have used the BCP command which is like:
DECLARE #command VARCHAR(1000)
SET #command = 'BCP "Exec [DatabaseName].[dbo].[StoredProcedureName] " queryout "D:\In\ErrorDetails'+ '.txt" -c -T -t -k'
EXEC xp_cmdshell #command
I need this to execute only if results of SP are not null.
You can use temp table to store data generated by SP, if there are any rows - write it to file
USE tempdb
IF OBJECT_ID(N'#MyTempTable') IS NOT NULL
BEGIN
DROP TABLE #MyTempTable
END
SELECT * INTO #MyTempTable
FROM OPENROWSET('SQLNCLI', 'Server=HOME\SQLEXPRESS;Trusted_Connection=yes;',
'EXEC Test.dbo.StoredProcedureName');
IF (SELECT COUNT(*) FROM #MyTempTable) > 0
BEGIN
DECLARE #command VARCHAR(1000)
SET #command = 'BCP "USE tempdb SELECT * FROM #MyTempTable " queryout "D:\In\ErrorDetails'+ '.txt" -c -T -t -k'
EXEC xp_cmdshell #command
END

SQL Server bcp utility does not create the txt file,

I'm new to this feature in SQL Server and could use some help. I'm experimenting with the BCP utility and the AdventureWorks2012 database.
I'm attempting to export data to a text file with the BCP utility and the code executes but a file is not created. Can you please look at my code and tell me where the problem(s) is/are.
I'm working out of a local copy of SQL Server Express. Thank you.
Declare #sql Varchar(8000)
Select #sql = 'bcp
+ SELECT FirstName, LastName
FROM AdventureWorks2012.Person.Person ORDER BY LastName, Firstname
+ queryout C:\Users\David\Desktop\yes.txt + -c -t, -T -S'
+ ##SERVERNAME
EXEC master..xp_cmdshell #sql
Here is my output when I run the query:
output
usage: bcp {dbtable | query} {in | out | queryout | format} datafile
[-m maxerrors] [-f formatfile] [-e errfile]
[-F firstrow] [-L lastrow] [-b batchsize]
[-n native type] [-c character type] [-w wide character type]
[-N keep non-text native] [-V file format version] [-q quoted identifier]
[-C code page specifier] [-t field terminator] [-r row terminator]
[-i inputfile] [-o outfile] [-a packetsize]
[-S server name] [-U username] [-P password]
[-T trusted connection] [-v version] [-R regional enable]
[-k keep null values] [-E keep identity values]
[-h "load hints"] [-x generate xml format file]
[-d database name] [-K application intent] [-l login timeout]
NULL
Here is the PRINT output:
bcp
+ "SELECT FirstName, LastName
FROM AdventureWorks2012.Person.Person ORDER BY LastName, Firstname"
+ queryout C:\Users\David\Desktop\yes.txt -c -t, -T -SHOMEPC\SQLINST01
TT's code worked. Here it is:
DECLARE #stmt_e VARCHAR(8000);
SET #stmt_e=
'BCP '+
'"SELECT FirstName,LastName FROM AdventureWorks2012.Person.Person ORDER BY LastName,Firstname" '+
'QUERYOUT "C:\Users\David\Desktop\yes.csv" '+
'-c -t, -T -S ' + ##SERVERNAME;
EXEC master.sys.xp_cmdshell #stmt_e;
The instructions for adding system permissions for database engine access can be found at the link below. I had to do this because my SQL Server Instance did not have permission to write to the path I was specifying.
https://msdn.microsoft.com/en-us/library/jj219062.aspx
The following snippet should run without problem on any SQL Server. It outputs all table information in INFORMATION_SCHEMA.TABLES as a comma separated file in C:\Temp\information_schema.csv.
Run this as a sanity check; it works without problem on my system, and it should on your system too. Run this from the AdventureWorks2012 database. If it doesn't work we'll have to delve deeper.
DECLARE #stmt_c VARCHAR(8000);
SET #stmt_c=
'BCP '+
'"SELECT*FROM '+QUOTENAME(DB_NAME())+'.INFORMATION_SCHEMA.TABLES" '+
'QUERYOUT "C:\Temp\information_schema.csv" '+
'-c -t, -T -S ' + ##SERVERNAME;
EXEC master.sys.xp_cmdshell #stmt_c;
Now if this works, adapt this to your query:
DECLARE #stmt_e VARCHAR(8000);
SET #stmt_e=
'BCP '+
'"SELECT FirstName,LastName FROM AdventureWorks2012.Person.Person ORDER BY LastName,Firstname" '+
'QUERYOUT "C:\Users\David\Desktop\yes.txt" '+
'-c -t, -T -S ' + ##SERVERNAME;
EXEC master.sys.xp_cmdshell #stmt_e;
This Should work
DECLARE #sql VARCHAR(8000);
SELECT #sql = 'bcp "SELECT FirstName, LastName FROM'+
' AdventureWorks2008.Person.Person ORDER BY FirstName, LastName" queryout'+
' C:\Users\David\Desktop\yes.txt -c -t, -r \r\n -S '+##servername+' -T';
EXEC master..xp_cmdshell #sql;
Should warpped the query in double quotes. I have removed an extra + before the -c.
You can test out the BCP on command prompt first. make sure it is working before using xp_cmdshell to execute it.
And lastly, i have added a PRINT statement to print out the command for verification
Declare #sql Varchar(8000)
Select #sql = 'bcp "SELECT FirstName, LastName '
+ 'FROM AdventureWorks2012.Person.Person '
+ 'ORDER BY LastName, Firstname" '
+ 'queryout C:\Users\David\Desktop\yes.txt -c -t, -T -S'
+ ##SERVERNAME
PRINT #sql -- Print out for verification
EXEC master..xp_cmdshell #sql
I have struggled with that problem almost whole yesterday and finally it comes out, that the "select" query is too complex (probably) to be processed by the xp_cmdshell command directly.
I have a query joining and aggregating many tables from different databases.
Trying to save its results to txt file directly via xp_cmdshell always resulted in the output presented by BrownEyeBoy, eventhought the select itself was working correctly.
I've bypassed this simply by inserting the results of the complex query into temporary table and then execute the xp_cmdshell on the temporary table like:
DECLARE
#SQL varchar(max)
, #path varchar(max) = 'D:\TMP\TMP_' + convert(varchar(10), convert(date, getdate(), 21)) + '.txt'
, #BCP varchar(8000)
;
INSERT INTO ##TMP
{COMPLEX SELECT}
;
SET #SQL = 'SELECT * FROM ##TMP;
SET #BCP = 'bcp "' + #sql + '" queryout "' + #path + '" -c -T -S.'
EXEC xp_cmdshell #bcp;
Not nice, but easy and working.
Put the complete bcp query in one not to change the line while writing bcp query.
You can write your query as:
Declare #sql Varchar(8000)
Select #sql = 'bcp "SELECT FirstName, LastName FROM AdventureWorks2012.Person.Person ORDER BY LastName, Firstname " queryout "C:\Users\David\Desktop\yes.txt" -Usa -Ppassw0rd -c -t, -T -S'
EXEC master..xp_cmdshell #sql

Resources