Exporting stored procedure result to excel with column name
I looked at the link above and created stored procedure. I am not getting data into ExcelSheet I have created in D:\testing.xls and D:\data_file.xls
Errors List
1.usage: bcp {dbtable | query} {in | out | queryout | format} datafile
[-m maxerrors] [-f formatfile] [-e errfile]
[-F firstrow] [-L lastrow] [-b batchsize]
[-n native type] [-c character type] [-w wide character type]
[-N keep non-text native] [-V file format version] [-q quoted identifier]
[-C code page specifier] [-t field terminator] [-r row terminator]
[-i inputfile] [-o outfile] [-a packetsize]
[-S server name] [-U username] [-P password]
[-T trusted connection] [-v version] [-R regional enable]
[-k keep null values] [-E keep identity values]
[-h "load hints"]
NULL
2.The system cannot find the file specified.
NULL
3.Could Not Find D:\data_file.xls
NULL
Well may be following script will be useful to you.
INSERT INTO OPENDATASOURCE('Microsoft.Jet.OLEDB.4.0','Data Source={\\Operations\Output\CR_20071130.xls};Extended Properties=Excel 8.0')..
[Sheet1$A1:H1] exec YourStoredProcedure
Related
I need to export the Json data into a text file, but it is getting error out.
EXEC xp_cmdshell 'bcp "SELECT Formdata fldr.FormInstanceDataMap WHERE
FormInstanceID=127432" queryout "D:\Import\Test.txt" -
STMGVM021\deBenefitSync_Web_DEV -T -c -Usa -Psa#123 '
Getting error Out. Output is
Copy direction must be either 'in' or 'out'. Syntax Error in
'queryout'. usage: bcp [[db_name.]owner.]table_name[:slice_num]
[partition pname] {in | out} [filename] [-m maxerrors] [-f
formatfile] [-e errfile] [-d discardfileprefix] [-F firstrow] [-L
lastrow] [-b batchsize] [-n] [-c] [-t field_terminator] [-r
row_terminator] [-U username] [-P password] [-I interfaces_file] [-S
server] [-a display_charset] [-z language] [-v] [-i input_file] [-o
output_file] [-A packet size] [-J client character set] [-T text or
image size] [-E] [-g id_start_value] [-N] [-W] [-X] [-M LabelName
LabelValue] [-labeled] [-K keytab_file] [-R remote_server_principal]
[-C] [-V [security_options]] [-Z security_mechanism] [-Q] [-Y] [-y
sybase directory] [-x trusted.txt_file] [--clienterr errfile]
[--maxconn maximum_connections] [--show-fi] [--hide-vcc] [--colpasswd
[[[db_name.[owner].]table_name.]column_name [password]]] [--keypasswd
[[db_name.[owner].]key_name [password]]] [--initstring ASE
initialization string] [--quoted-fname] NULL
I could find two changes in your query.
When -T (windows authentication) option is added, no need to specify the SQL credentials.
You did not specify the DB name. Unless the needed DB is specified as default in your login, then probably you might end up with error.
Thanks,
Ananda Kumar J.
Thanks to domenicr now I see, that bcp keys are case sensetive. But another problem appears - bcp asks me about type of the field to write in file each time i run script - is it possible to automate that?
Enter the file storage type of field Employee_ID [char]:
I need to export data from view to csv file. The easiest way, as it seems to me is BCP. I tried to read MSDN https://msdn.microsoft.com/en-us/library/ms162802.aspx for syntax help, but I can't makes bcp to create file cause of error (running bcp from cmd). Thanks in advance for any help with syntacsis.
bcp [base_name].[dbo].[all_users_for_ad] out 1.csv -t -c -u mylogin -p mypassword -s mysever
bcp: unknown option u
usage: bcp {dbtable | query} {in | out | queryout | format} datafile
[-m maxerrors] [-f formatfile] [-e errfile]
[-F firstrow] [-L lastrow] [-b batchsize]
[-n native type] [-c character type] [-w wide character type]
[-N keep non-text native] [-V file format version] [-q quoted identifier]
[-C code page specifier] [-t field terminator] [-r row terminator]
[-i inputfile] [-o outfile] [-a packetsize]
[-S server name] [-U username] [-P password]
[-T trusted connection] [-v version] [-R regional enable]
[-k keep null values] [-E keep identity values]
[-h "load hints"] [-x generate xml format file]
BCP command arguments are case sensitive. Should be -T and -S. For trusted connections, no need to pass user and password.
Use -c to specify character data type or supply a format file with -f argument
Refer to this on how to create a format file. https://msdn.microsoft.com/en-CA/library/ms191516.aspx
I Hope I'm not re-asking.. but..
here's the pertinent section of my SQL. The view in question (vw_TempALlItems) is created in prior steps, and yes its created using column names, and paying attention to types & etc.. The view works 100%.. But the BCP, not so much..
---------------------------------------------------------------
DECLARE #bcpCommand varchar(2000)
SET #bcpCommand = 'bcp vw_TempAllItems out
"c:\FEPData.csv" -c -t -U USER -P PWD'
EXEC master..xp_cmdshell #bcpCommand
GO
Drop View vw_TempAllItems
GO
----------------------------------------------------------------
The only thing I get is the results pane shows the BCP command parameters
usage: bcp {dbtable | query} {in | out | queryout | format} datafile
[-m maxerrors] [-f formatfile] [-e errfile]
[-F firstrow] [-L lastrow] [-b batchsize]
[-n native type] [-c character type] [-w wide character type]
[-N keep non-text native] [-V file format version] [-q quoted identifier]
[-C code page specifier] [-t field terminator] [-r row terminator]
[-i inputfile] [-o outfile] [-a packetsize]
[-S server name] [-U username] [-P password]
[-T trusted connection] [-v version] [-R regional enable]
[-k keep null values] [-E keep identity values]
[-h "load hints"] [-x generate xml format file]
[-d database name]
NULL
and the CSV file is NOT created..
Anyone?
For one, try making it a query:
SET #bcpCommand = 'bcp "SELECT * FROM vw_TempAllItems" out
"c:\FEPData.csv" -c -t -U USER -P PWD'
Also, to answer your question, check out: How to pass parameter to a bcp command in sql server on stackoverlow.
For anyone who may care here's the end result which works well
DECLARE #bcpCommand varchar(2000)
SET #bcpCommand = 'bcp ' + DB_NAME() + '..vw_TempAllItems out c:\FEPData.csv -c -t, -U User -P Pwd -S' + ##SERVERNAME
EXEC master..xp_cmdshell #bcpCommand
GO
I am trying to write an output text file from table. I am not getting any errors but the file is not created.
EXEC master..xp_cmdshell 'bcp "SELECT * FROM DB.dbo.table1" queryout "C:\employee.txt" -T -Sservername -U user111 -P pwd -c -t,'
No file is created, I am not sure why. Can anyone pls help?
I am not getting errors instead I get following messages:
usage: bcp {dbtable | query} {in | out | queryout | format} datafile
[-m maxerrors] [-f formatfile] [-e errfile]
[-n native type] [-c character type] [-w wide character type]
[-N keep non-text native] [-V file format version] [-q quoted identifier]
[-C code page specifier] [-t field terminator] [-r row terminator]
[-i inputfile] [-o outfile] [-a packetsize]
[-S server name] [-U username] [-P password]
[-T trusted connection] [-v version] [-R regional enable]
[-k keep null values] [-E keep identity values]
NULL
I am running it on server A but the database is on server B. so on server A I used SQL SERVER management studio and gave the server B details to log on. After that I wrote query as mentioned above. so the file is going to be formed on which server?
Worked for me even with or without the spaces after the username and pass. Do you have write access to C:\ on the server box? Do you need username or pass if you are also using -T?
From MSDN:
Security Note
When the bcp utility is connecting to SQL Server with a trusted connection using integrated security, use the -T option (trusted connection) instead of the user name and password combination.
the query was correct -
EXEC master..xp_cmdshell 'bcp "SELECT * FROM DB.dbo.table1" queryout "C:\employee.txt"
-Sservername -U user111 -P pwd -c -t,'
while typing in SQL SERVER MANAGEMENT studio I pressed enter after queryout so statement "C:\employee.txt" -Sservername -U user111 -P pwd -c -t,' was coming in next line.
because of this I was not getting any error or
I have tried many things, but I'm still not getting this to work. Here's the code:
bcp RFID.dbo.ResultsDump format nul -t, -c -x -f C:\RFID.xml -S"ALBERTLAST-PC\SQLEXPRESS" -UAlbert Last -P -T
All I want to do is create an XML format file from ResultsDump table, and the above statement doesn't even work. My username is "Albert Last" where Last is my last name, yet this is what the above statement returns:
bcp: unknown option A
usage: bcp {dbtable | query} {in | out | queryout | format} datafile
[-m maxerrors] [-f formatfile] [-e errfile]
[-F firstrow] [-L lastrow] [-b batchsize]
[-n native type] [-c character type] [-w wide character type]
[-N keep non-text native] [-V file format version] [-q quoted identifier]
[-C code page specifier] [-t field terminator] [-r row terminator]
[-i inputfile] [-o outfile] [-a packetsize]
[-S server name] [-U username] [-P password]
[-T trusted connection] [-v version] [-R regional enable]
[-k keep null values] [-E keep identity values]
[-h "load hints"] [-x generate xml format file]
NULL
It's thinking that I am asking for option A, because when I put square brackets around Albert Last, here's what I get:
bcp: unknown option [
I've also tried this WITHOUT specifying server name, username, or password, with just -T. This is what I get:
SQLState = 37000, NativeError = 4060
Error = [Microsoft][SQL Native Client][SQL Server]Cannot open database "RFID" requested by the login. The login failed.
SQLState = 28000, NativeError = 18456
Error = [Microsoft][SQL Native Client][SQL Server]Login failed for user 'NT AUTHORITY\NETWORK SERVICE'.
NULL
I've looked everywhere online and have not seen a similar problem before, and I'm beginning to think that this is just a simple mistake that I'm not catching.
Can somebody help?? Or if there is a way to generate format xml file RFID.xml from table ResultsDump without bcp, that would be greatly appreciated! Thanks in advance!
Can you try if the following works:
-U"Albert Last"
I can vaguely remember that I have done it this way a long time ago.
The error you see in your comment has something to do with your format file (c:\RFID.xml), not with your username. Possibly you do not have write access to c:\? Can you try it with a relative path, just RFID.xml? And make sure you then run the bcp command from a location where you have write access.