SQL Server BCP Empty File - sql

I'm trying to use bcp to query out a comma-separated-value file but each time I get an empty file.
Here's my bcp command:
bcp "SELECT * FROM ##OutAK " QUERYOUT D:\Outbound\raw\li14090413.raw -c -T -t -S DB1
I have verified that ##OutAK is NOT empty because select count (*) from ##OutAK is not 0. When open file using HEX editor, I see the following:
0D 0A

I found the problem. It seems BCP is "allergic" with NULL. So, I just put ISNULL() to all the null-able fields and the output file is back to normal now.

Related

SQL Server bcp utility export/write incomplete data

I am using SQL Server bcp utility to export data in csv format from a table.
I've noticed that sometimes the exported data is incomplete, I mean the bcp process exit without errors but the resulting csv file shows incomplete data at the end.
For example this is the bcp command and log of a recent export:
bcp "SELECT IdSap, CodSap, replace(replace(convert(nvarchar(max), Dati_XML), char(13), ''), char(10), '') as Dati_XML FROM MYDB.dbo.MyTable WHERE DataAggiornamento > '2019-06-07'" queryout "C:\temp\bcp_output.csv" -c -t 0x1f -U "myuser" -P "mypassword" -S "10.180.188.53"
As you can see there are no errors. The output file shows 100885 rows. The problem here is that the query should return 292887 rows, not 100884. It's like the bcp process has not finished to properly import/write data before exit.
Does anyone know why is this happening and any possible fix?

Exporting SQL Table into a CSV file using Windows Batch Script

I am trying to create a windows batch file to export data from an SQL file to a CSV file.
I have an SQL file in %MYHOME%\database\NET-DB.sql which contains data that is:
NET-DB.sql
insert into net_network (id, a_id, alias, address, domain, mask) values('NET_10.10.1.0_10', 1, 'Local Network', '10.10.1.0', '', '255.255.252.0');
What I have tried so far in exporting the data from net_network table into a CSV file in my .bat file is with this command:
export.bat
if not exist "%MYHOME%\net\NUL" mkdir "%MYHOME%\net"
COPY net_network TO '%MYHOME%\net\CSV-EXPORT_FILE.csv' DELIMITER ',' CSV HEADER;
pause
Since that does not work for me, what should be the correct approach for this implementation? Any help will be much appreciated.
Use SQLCMD
You need to modify the code to make it work in your environment, but here goes.
if not exist "%MYHOME%\net\NUL" mkdir "%MYHOME%\net"
cd "C:\your path\to\sqlcmd"
sqlcmd -S YourDBServer -d DB_NAME -E -Q "select id, a_id, alias, address, domain, mask from net_network"
-o "CSV-EXPORT-FILE.csv" -s"," -w 255
Some explanations:
-S The database server to connect to.
-d Name of the database to connect to.
-Q Query to run, can also be insert, delete, update etc.
-o select the output file
-s"," separated by comma
-w column width, this has to be as big as your largest columns characters.

sql bcp export error

My query is
EXEC XP_CMDSHELL bcp "select * from dbo.table1" queryout "Z:\test.csv" -T -c -S ".\SQL2012EXPRESS"
I got error saying: Incorrect syntax near 'select * from dbo.table1'.
I wonder what's the problem?
Thanks for advice!
Include the database name as well in your select query like below
bcp "select * from db_name.dbo.table1" queryout "Z:\test.csv" -T -c -S ".\SQL2012EXPRESS"
Also, since you are getting entire table data select * from ... you can just do
bcp db_name.dbo.table1 out "Z:\test.csv" -T -c
EDIT:
I have a DB named test in which have a table named test1. If I try below it complain nothing. Just open a command prompt and run the bcp command
C:\Users\blah>bcp test.dbo.test1 out "D:\test.csv" -T -c
Starting copy...
1 rows copied.
Network packet size (bytes): 4096
Clock Time (ms.) Total : 1 Average : (1000.00 rows per sec.)

bcp utility asking me to enter different parameters i do not undertand

I am using BCP To export data from sqlserver 2008R2 Database Name Health,and a table name patient.The out of the query should be save in a textfile:ApplicantsName.txt located at:
C:\Users\meuser\Desktop ApplicantsName.txt -C -T
After running the following query on the command prompt:
bcp "Select FirstName,LastName,PatientNumber from Health.dbo.Patient order by FirstName" queryout "C:\Users\meuser\Desktop ApplicantsName.txt" -C -T
It prompted me this:
Enter the file storage type of fiedl FirstName [char]:varchar
and then this:
Enter prefix-length of field FirstName[2]:FirstName
I have been entering some values but i think the best is to know how it works.After some time of research on the internet, know using bcp utility is one fastest way to export or import data between instance to a file.I follow exactly the samples provided by MS here but i think i need some practical explanation. Can some guide me how to go about this and a little bit of explanation or relevant ref. will be appreciated too.
#one angry researcher's solution of adding '-C RAW' did not work in my particular case but adding lower-case '-c' did. It performs the operation using a character data type
For instance:
bcp mydb.mytable out c:/temp/data.txt -T -c
You need to add a value for the -C parameter (capital C!). If you do not know what you're using it for, you probably won't be needing it and can omitt it.
Refer to the official documentation: http://msdn.microsoft.com/en-us/library/ms162802.aspx
edit: you could, for example, use
bcp "Select FirstName,LastName,PatientNumber from Health.dbo.Patient order by FirstName" queryout "C:\Users\meuser\Desktop\ApplicantsName.txt" -C RAW -T
You will need to fix your output directory too (seems you forgot a backslash there).
heres the sample bcp command with query and credentials (param)
bcp "SELECT * from yourtable" queryout c:\StockItemTransactionID_c.txt -c -Uusername -Pdbpassword -Sinstance -dYourDBName
Note: -U -P -S are case sensitive.

My bcp hangs after creating an empty file

I am trying to drop a file into a directory on the local machine (same machine running SQL Instance). The content of the table I am trying to drop out is in xml format.
ie. table=xmlOutFiles, fieldName = xmlContent; fieldName contains essentially varchar(max) data that is to become the xml file we need.
When the bcp command is executed it seems to create the file in the #dest location, size = 0 bytes and then the process running from within SMSS just sits there waiting for something!
I cannot do anything with that empty file, like delete it, unless I use task manager to kill the process "bcp.exe".
I have tried multiple combinations of the bcp flags, etc.
Running the bcp command from a system prompt, replacing the "#vars" seems to work but I really need it to be part of my SQL Trigger script and function.
Assistance appreciated!!!!
Select #dest = (Select filename from xmlOutfiles)
Select #cmd = 'bcp "Select xmlContent from ProcureToPay.dbo.XmlOutFiles" queryout '+#dest+' -x -w -T -S' + ##servername
Exec xp_cmdshell #cmd
I have tried executing with -T and -Uusername -Ppassword parameters, etc.
This command works from the DOS prompt:
bcp "Select xmlContent from Procure.To.Pay.dbo.XmlOutFiles" queryout c:\temp\test.xml -x -w -T S<myservernameHere>