BCP utility in SQL Server - how to exclude tabs in the output - sql-server

I use a view to get financial data and in the view it pads the columns into a positional file. Then I use BCP to create the file. All gr8 but I do not know how to stop it adding TABS to the file. Any idea how to stop / exclude tabs?
set #Command = 'bcp "SELECT * FROM AscendancyCF.dbo.[BACS_EXPORT]" queryout "C:\bcp\edge_bacs_pay_' + #sDate + '.dat" -T -c -S' + ##SERVERNAME

By TABS, I'm guessing you mean it's using tab spacing as the delimiter. You need to specify the delimiter switch (-t,) in your query:
set #Command = 'bcp "SELECT * FROM AscendancyCF.dbo.[BACS_EXPORT]" queryout "C:\bcp\edge_bacs_pay_' + #sDate + '.dat" -T -c -t, -S' + ##SERVERNAME

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

Dynamic file creation with BCP Utility

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

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

How to escape double quotes in bcp sql query in stored procedure sql server

The following is my stored procedure code:
Alter PROCEDURE [dbo].[ConvertToFile]
#TempFileName varchar(8000)
AS
BEGIN
SET NOCOUNT OFF;
DECLARE #bcpFileCmd varchar(8000)
SET #bcpFileCmd= 'bcp "SELECT id,full_Name,message from NotesTable " queryout '+#TempFileName+' -t, -c -T'
EXEC master..xp_cmdshell #bcpFileCmd
END
BCP dumps all the data from NotesTable in file. But I need to enclose the message results with double quotes. The datatype for message field is text. I'm not able to append it in select query in BCP.
You will need to concatenate double quotes to the beginning and end of your message results, and escape both the single quotes and double quotes:
SET #bcpFileCmd= 'bcp "SELECT id, full_Name, ''""'' + message + ''""'' from NotesTable " queryout '+#TempFileName+' -t, -c -T'
If you need to retain the column name, you should also alias it like so (might not be necessary if you're just outputting to a file):
SET #bcpFileCmd= 'bcp "SELECT id, full_Name, ''""'' + message + ''""'' ''message'' from NotesTable " queryout '+#TempFileName+' -t, -c -T'

Export xml data using BCP Command in SQL Server

I am trying to export datable data in xml format but,Problem is like i can able to create xml file but data is not get writing in to the file following is my code I am trying.
DECLARE #cmd VARCHAR(2000);
SET #cmd = 'bcp.exe "select * from emp FOR XML AUTO" queryout E:\rahul_1.xml -x -T';
EXEC xp_cmdshell #cmd ;
And following is the output message I am getting after executing above code
NULL
Enter the file storage type of field XML_F52E2B61-18A1-11d1-B105-00805F49916B [ntext]:
can any body please suggest me on this
Dan you answer works, except one last thing. BCP needs additional information about the source query. Best idea to fully qualify the source of the data.
SET #cmd = 'bcp.exe "select * from [Database].[Schema].[Table] FOR XML AUTO"
queryout E:\rahul_1.xml -c -T';
Instead of the -x parameter (generate xml format file), specify -c (character file):
SET #cmd = 'bcp.exe "select * from emp FOR XML AUTO" queryout E:\rahul_1.xml -c -T';
Try to use -w switch for exporting XML file in correct format
DECLARE #cmd VARCHAR(2000);
SET #cmd = 'bcp.exe "select * from [Database].[Schema].[Table] FOR XML AUTO" queryout E:\filename.xml -S MyServer\MyInstance -c -T -w';
EXEC xp_cmdshell #cmd;

Resources