Using bcp utility to export SQL queries to a text file - sql

I debug a stored procedure (SQL Server 2005) and I need to find out some values in a datatable.
The procedure is run by an event of the application and I watch just the debugging output.
I do the following my stored procedure (SQL Server 2005), I took a system table (master.dbo.spt_values) as example:
set #logtext = 'select name, type from master.dbo.spt_values where number=6'
--set #logtext = 'master.dbo.spt_values'
SET #cmd = 'bcp ' + #logtext + ' out "c:\spt_values.dat" -U uId -P uPass -c'
EXEC master..XP_CMDSHELL #cmd
So, when I uncomment the second like everything works, a file apprears on the C:\ drive... but if I coment it back leaving only the first line, any output is generated.
How to fix this problem?

bcp out exports tables.
To export a query use queryout instead - you'll need to wrap your query in "double quotes"
set #logtext = '"select name, type from master.dbo.spt_values where number=6"'
--set #logtext = 'master.dbo.spt_values'
SET #cmd = 'bcp ' + #logtext + ' queryout "c:\spt_values.dat" -U uId -P uPass -c'
EXEC master..XP_CMDSHELL #cmd
http://msdn.microsoft.com/en-us/library/ms162802.aspx

Related

SQL Server BCP export file incremental

I'm trying to use BCP to export my data to a .TXT file, but I needed to do an incremental in the same file, since I will need to export data from two views.
Example:
SET #cmd = '"SELECT * FROM Site"'
SELECT #sql = 'bcp '+#cmd+' queryout D:\mytest.txt -c -t; -T -S SERVER-PC\SQLEXPRESS';
EXEC xp_cmdshell #sql;
SET #cmd = '"SELECT * FROM Customers"'
SELECT #sql = 'bcp '+#cmd+' queryout D:\mytest.txt -c -t; -T -S SERVER-PC\SQLEXPRESS';
EXEC xp_cmdshell #sql;
Data out:
H;04399024100427;20160620
V;04399024100427;CUSTOMER I;STATE;CITY;NAME;75123390;A
I thought of UNION ALL but the structure of the tables are different and I have cases where I will need to export data from up to 5 tables.

Strange XML-Export behaviour

declare #cmd nvarchar(255)
SET #cmd = 'bcp "select * from Testdb.dbo.mytable WHERE nr LIKE ''%102065336''' + '" queryout C:\temp\sample.xml -c -t, -S' + ##servername + ' -T'
exec xp_cmdshell #cmd
That Code returns a correct .xml file with correct format, but when i use this #cmd so with "=" instead of "LIKE" the xml file looks broken(only cryptic chars in it) :
SET #cmd = 'bcp "select * from Testdb.dbo.mytable WHERE nr = ''102065336''' + '" queryout C:\temp\sample.xml -c -t, -S' + ##servername + ' -T'
So How is this possible? The queries return the same data if i execute the sql statement...
I cannot reproduce this. Check it out:
USE master;
GO
CREATE DATABASE tstDB;
GO
USE tstDB;
CREATE TABLE mytable(nr VARCHAR(100));
GO
INSERT INTO mytable VALUES('11'),('12'),('22');
GO
declare #cmd nvarchar(255);
SET #cmd = 'bcp "select * from tstDB.dbo.mytable WHERE nr LIKE ''%11'' FOR XML AUTO' + '" queryout C:\temp\sample1.xml -c -t, -S' + ##servername + ' -T';
exec xp_cmdshell #cmd;
SET #cmd = 'bcp "select * from tstDB.dbo.mytable WHERE nr = ''11'' FOR XML AUTO' + '" queryout C:\temp\sample2.xml -c -t, -S' + ##servername + ' -T';
exec xp_cmdshell #cmd;
GO
USE master;
GO
--careful with real data!
DROP DATABASE tstDB;
GO
Some ideas:
You speak about XML export, but the code you show does not create any XML? So maybe the issue is in an area we cannot see...
You declare your command with a size of 255. This is pretty small... Might be, that something is truncated
generall hint: Use -w instead of -c when you export XML. Find details here

gpg encryption in xp_cmdshell

I am trying to encrypt a .csv file using gpg in xp_cmdshell command in sql server 2012.
When I use gpg through xp_cmdshell, it says gpg is not recognized as an internal or external command.
But this works perfectly on windows cmd.
How should I configure my sql server to accept this command. Please advice.
select #encrypt = 'gpg -e -r ' + #sEncryptionKey+ ' '+ #sPath + #tempdataFolder+'\'+ #sFileName
exec master..xp_cmdshell #encrypt
To remedy the error you are indicating above, simply add the full path to gpg.exe in your code.
select #encrypt = 'c:\gpg\gpg.exe -e -r ' + #sEncryptionKey+ ' '+ #sPath + #tempdataFolder+'\'+ #sFileName
exec master..xp_cmdshell #encrypt
For comparison, this is how I have done this in the past via SSIS; be sure you provide the exact full path to your executable... we are using gpg4win (gpg2.exe) in this example.
You may also want to wrap each parameter in " " to handle file names/paths with spaces better.
DECLARE #sKeyEmail VARCHAR(100), #sInFile VARCHAR(100), #sOutFile VARCHAR(100), #SQL VARCHAR(1000)
SET #sInFile = 'c:\temp\somefile.xls'
SET #sOutFile = 'c:\temp\somefile.xls.gpg'
SET #sKeyEmail = 'user#domain.com'
SET #SQL = 'EXEC master.dbo.xp_cmdshell ''C:\Progra~1\GNU\GnuPG\gpg2 -o ' +#sOutFile+ ' -e -r ' +#sKeyEmail + ' ' +#sInFile+ ''''
PRINT #SQL
-- EXEC sp_execute #SQL

How to export SQL Server table data into text file as Insert command?

I have a table with bulk data (say 2 Million rows). I need to export this data into a text file.
I've approached Generate Scripts method by which it throws
System.OutOfMemoryExeception.
I need to some how convert the data into text file. Can sqlcmd approach be helpful? If so please suggest the steps.
Turn on xp_cmdshell in Facets and run this -
DECLARE #sql NVARCHAR(4000) = 'bcp "SELECT * FROM sys.schemas" queryout "D:\sample.txt" -S ' + ##servername + ' -T -w -r -t'
EXEC sys.xp_cmdshell #sql

bcp field terminator empty

https://technet.microsoft.com/en-us/library/aa196735(v=sql.80).aspx
I'm trying to create a file from bcp, the file was successfully created, but I want to make the field terminator -t empty. The closest way was with the null terminator, but it makes a space between fields.
This is my code.
select #cmd = 'bcp '+ #SP + ' queryout '+ #ruta+' -w -t\0 -S '+#Servidor+ ' -U'+#user +' -P'+ #password
exec master..xp_cmdshell #cmd
I've also tried to leave it without any symbol after t, but it puts more spaces.
select #cmd = 'bcp '+ #SP + ' queryout '+ #ruta+' -w -t -S '+#Servidor+ ' -U'+#user +' -P'+ #password
exec master..xp_cmdshell #cmd
use -t"" This basically gives you a fixed width export file especially when used to source fields defined as char()
for example:
bcp "select * from TEST.dbo.tExportTst" queryout FixedWidth.txt -c -t"" -T -S Svr\instance