sql bcp export error - sql

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.)

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?

SQL Server BCP Empty File

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.

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>

exporting query result to excel

I am trying to execute the below sql but I am getting "Invalid object name '.Sheet1$'."
INSERT INTO OPENDATASOURCE
('Microsoft.Jet.OLEDB.4.0',
'Database=c:\test.xls;Extended Properties=Excel 8.0')..[Sheet1$])
SELECT col1 FROM table;
its in mssql 2005.
any help is appreciated.
If you have xp_cmdshell enabled you can do this to export to delimited text file, which will open perfect in Excel.
EXEC xp_cmdshell 'SQLCMD -S [SERVERNAME] -d [DBNAME] -o "C:\Output.txt" -s "," -U "[USERNAME]" -P "[PASWORD]" -Q "SELECT TOP 10 * FROM table"';
According to this posting (and a few other samples Google found for me), you need three dots before the table: 8.0)...[Sheet1$]. (Don't ask me why).
Added: The German translation of this provides a complete example of Excel access:
SELECT * FROM OPENDATASOURCE('Microsoft.Jet.OLEDB.4.0',
'Data Source=C:\DataFolder\Documents\TestExcel.xls;Extended Properties=EXCEL 5.0')...[Sheet1$] ;