Bcp command is getting error Out - sql

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.

Related

ssh using # without a username

Im doing some setup via the following guide,
https://www.ovirt.org/develop/dev-process/working-with-gerrit.html
Theres a step to verify the ssh config but it involves doing
ssh #gerrit.ovirt.org
This looks like a typo, don't we need a user for the # part. If no user, no # is my understanding?
[root#dub-svrfarm27 ~]# ssh #gerrit.ovirt.org
usage: ssh [-1246AaCfGgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]
[-D [bind_address:]port] [-E log_file] [-e escape_char]
[-F configfile] [-I pkcs11] [-i identity_file]
[-J [user#]host[:port]] [-L address] [-l login_name] [-m mac_spec]
[-O ctl_cmd] [-o option] [-p port] [-Q query_option] [-R address]
[-S ctl_path] [-W host:port] [-w local_tun[:remote_tun]]
[user#]hostname [command]
This being said...the final bit of my id_rsa.pub has this
71MgP8w== root#
So maybe there is method to this, im not sure.
It's a typo. Look again at the usage message:
[user#]hostname
The # is legal if, and only if, a user name is supplied in addition to the required host name.

Can't makes BCP to export data from view to csv

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

Exporting stored procedure result to excel sheet

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

writing a text from a table using tsql

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

Can't create XML format file in SQL, bcp: unknown option A

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.