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.
Related
I'm having a hard time working out how to write XML to my hard drive in SQL Server. I gather I could do something like this:
EXEC xp_cmdshell 'bcp "###INSERT SELECT/FOR XML QUERY###" queryout "C:\test.xml" -T -c -t'
But my SELECT/FOR XML query runs to about 40 lines so I'd rather not. I tried storing the XML as #storedXML, saving it in a table and then querying that table with bcp:
CREATE TABLE xmlOutput(outXML xml)
INSERT INTO xmlOutput(outXML)
VALUES (#storedXML)
DECLARE #sqlCmd VARCHAR(1000)
SET #sqlCmd = 'bcp "SELECT * FROM xmlOutput" queryout C:\test.xml -c -T -t'
EXEC xp_cmdshell #sqlCmd
But all that gets me is "Unable to open BCP host data-file".
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
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
In the EXAMPLE below, it works creating a file with all rows and columns in FTP_export_BAC table. But if I specify specific columns only to be exported, i.e. SELECT Col1, Col2 FROM Asce.... it fails. Any ideas?
DECLARE #Command varchar(512)
set #Command = 'bcp "SELECT * FROM myDatabase.dbo.[FTP_export_BAC]" queryout "C:\bcp\bcptest.txt" -T -c -S' + ##SERVERNAME
print #Command
EXEC xp_cmdshell #Command
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