SQL Server bcp utility does not create the txt file, - sql-server

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

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

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.

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

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

Exporting SQL Query results to Excel

On executing the below mentioned statement:
EXEC proc_generate_excel_with_columns
'your dbname', 'your table name','your file path'
I'm getting the following error.Can anyone help?
User name not provided, either use -U to provide the user name or use
-T for Trusted Connection 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] NULL
My procedure is this:
create procedure proc_generate_excel_with_columns
(
#db_name varchar(100),
#table_name varchar(100),
#file_name varchar(100)
)
as
--Generate column names as a recordset
declare #columns varchar(8000), #sql varchar(8000), #data_file varchar(100)
select
#columns=coalesce(#columns+',','')+column_name+' as '+column_name
from
information_schema.columns
where
table_name='dbo.vcuriosoftronic.tblPayrollGroups'
select #columns=''''''+replace(replace(#columns,' as ',''''' as '),',',',''''')
--Create a dummy file to have actual data
select #data_file=substring('D:\TestFile.xls',1,len('D:\TestFile.xls')
-charindex('\',reverse('D:\TestFile.xls')))
+'D:\TestFile.xls'
--Generate column names in the passed EXCEL file
set #sql='exec master..xp_cmdshell ''bcp
" select * from (select '+#columns+') as t"
queryout "'+#file_name+'" -c'''
exec(#sql)
--Generate data in the dummy file
set #sql='exec master..xp_cmdshell ''bcp
"select * from [myserver]..'+#table_name+'"
queryout "'+#data_file+'" -c'''
exec(#sql)
--Copy dummy file to passed EXCEL file
set #sql= 'exec master..xp_cmdshell ''type '+#data_file+' >> "'+#file_name+'"'''
exec(#sql)
--Delete dummy file
set #sql= 'exec master..xp_cmdshell ''del '+#data_file+''''
exec(#sql)
Check out my blog article on how to use BCP to export data.
http://craftydba.com/?p=1690
The below snippet uses the -T, trusted connection. If you are running a job under the agent, it will run under that security account.
Please either pass the standard security credentials, -U -P or make sure the account has the ability to run the command.
-- BCP - Export query, pipe delimited format, trusted security, character format
DECLARE #bcp_cmd4 VARCHAR(1000);
DECLARE #exe_path4 VARCHAR(200) =
' cd C:\Program Files\Microsoft SQL Server\100\Tools\Binn\ & ';
SET #bcp_cmd4 = #exe_path4 +
' BCP.EXE "SELECT FirstName, LastName FROM AdventureWorks2008R2.Sales.vSalesPerson" queryout ' +
' "C:\TEST\PEOPLE.TXT" -T -c -q -t0x7c -r\n';
PRINT #bcp_cmd4;
EXEC master..xp_cmdshell #bcp_cmd4;
GO
Updated assuming the BCP path is in search list. Below is a screen shot with the path removed and the query changed for SQL Server 2012.
Look at the message window, it has the BCP command from the print statement. You can put the command into a batch file to test from the DOS prompt. It is a debugging exercise for you.

Resources