Export xml data using BCP Command in SQL Server - 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;

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

AWS RDS SQL Server : export large data to CSV file from stored procedure

Migrating from a hosted SQL Server to AWS RDS which is a managed service (SaaS), we couldn't enable xp_cmdshell.
Our java app currently use a stored procedure that use xp_cmdshell to execute BCP.exe (Bulk Copy Program is a command-line tool used to import or export data against a Microsoft SQL Server) to export large data to CSV files.
And as xp_cmdshell cannot be enabled on RDS, we cannot use it to execute BCP.
Stored procedure code:
declare #bcp_header varchar(8000)
declare #bcp varchar(8000)
declare #bcpCopy varchar(8000)
declare #deleteFile varchar(300)
select #bcp_header = 'BCP "' + #header_select + '" queryout ' + #pathAndFileName +'.csv -c -C 1252 -t; -T '
select #bcp = 'BCP "' + #sql_export + '" queryout '+ #pathAndFileName +'_data.csv -c -C 1252 -t; -T '
select #bcpCopy = 'TYPE '+ #pathAndFileName +'_data.csv >> '+ #pathAndFileName +'.csv'
select #deleteFile = 'DEL '+#pathAndFileName +'_data.csv'
exec master..xp_cmdshell #bcp_header
exec master..xp_cmdshell #bcp
exec master..xp_cmdshell #bcpCopy
exec master..xp_cmdshell #deleteFile
Can we use a T-SQL command to export the query result to a CSV file and host it on S3 ?
Can we use a T-SQL command to export the query result to a CSV file and host it on S3 ?
Nope. There are TSQL alternatives to the BCP command, but ONLY for IMPORT.
Your best bet is to write a program that returns the data from SQL via some sort of SELECT statement and writes out the file yourself - bascailly a homebrewn replacement to BCP.
There is nothing out of the box that can act as a BCP replacement as all non BCP bulk functionality is either for programmatic use OR import only.
The answer is use BCP.exe inside an SSIS package using the SQL Server that the SSIS instance uses.

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.

Using BCP to create XML files produces files with invalid encoding for non-ASCII characters

Reproducing issue, SQL code:
CREATE TABLE dbo.FooTable(SomeString NVARCHAR(100));
INSERT INTO dbo.FooTable(SomeString)
VALUES('Degree symbol is °');
DECLARE #Code NVARCHAR(4000) = N'BCP "SELECT (SELECT SomeString FROM dbo.FooTable FOR XML PATH(''Foo''), ROOT(''BAR''), TYPE )" QUERYOUT "F:\Output\File.XML" -c -C RAW -T ';
EXEC xp_cmdshell #Code;
DROP TABLE dbo.FooTable;
It creates file with following content:
<BAR><Foo><SomeString>Degree symbol is °</SomeString></Foo></BAR>
Such files are not recognized as valid XML files by Internet Explorer and Firefox (Chrome yields error error on line 1 at column 23: Encoding error). If I open them with Notepad and save as Unicode (little endian) - it opens and validates.
Update:
changing bcp options to -T -w -r -t seems to make XML valid for most XML consumers, but not Internet Explorer.
Try this one -
IF OBJECT_ID('tempdb.dbo.##t') IS NOT NULL
DROP TABLE ##t
SELECT x = (
SELECT x
FROM (
VALUES (N'Degree symbol is °')
) t(x)
FOR XML PATH('Foo'), ROOT('BAR'), TYPE
)
INTO ##t
DECLARE #sql NVARCHAR(4000) = 'bcp "SELECT * FROM ##t" queryout "D:\sample.xml" -S ' + ##servername + ' -T -w -r -t'
EXEC sys.xp_cmdshell #sql
Just interesting where the root of your issue...

convert database table into XML schema file

I am using SQL Server 2005. Is there any command or GUI tool (e.g. any menu/function from SQL Server management studio) to convert database table into XML schema file (.xsd)?
thanks in advance,
George
I've found this. Give it a try
Select CourseID, Name, CoursePrice
FROM CouseMaster.Course Course
FOR XML AUTO, XMLSCHEMA
You can write to file like this:
bcp.exe "select top 0 * from (select 1 as iCol) as t for xml auto, xmlschema" queryout outfile.xsd -T -c
I'm Using the TOP 0 to exclude the xml of the actual query data since you only want the schema. The -c causes it to be plain character data in the output, use -w instead if you want utf-16 (unicode) output.
EDIT - and if you want to change the xml structure, look at PATH with FOR XML.
Declare #SQL nvarchar(1000)
SET #SQL= 'bcp.exe '+ '"select * from yourdbname.yourschema.yourtablename for xml path (''record''), ROOT (''tabel'')"' +' queryout '+ 'c:\yourfilename.xsd' +' -w -r -t -SyourServerName -T'
print #SQL
EXEC Master..xp_CmdShell #SQL
Replace allvalues starts with 'your', accordingly

Resources