I have a really big script (about 1GB) with UPDATE statements. Something like
UPDATE Table1 SET Col1 = 'Some text 1' WHERE ID = 1
UPDATE Table1 SET Col1 = 'Some text 2' WHERE ID = 2
-- and so on
What is the best way to execute this script? I suppose that I can't simply open it at text editor and run query...
Thanks in additional.
UPDATED
I need to run this script on remote SQL Server instance.
execute it wih the osql utility Reference
Example:
osql -E -i C:\MyFolder\MyScript.sql -o C:\MyFolder\MyOutput.rpt
you can also type for reference:
osql -?
usage: osql [-U login id] [-P password]
[-S server] [-H hostname] [-E trusted connection]
[-d use database name] [-l login timeout] [-t query timeout]
[-h headers] [-s colseparator] [-w columnwidth]
[-a packetsize] [-e echo input] [-I Enable Quoted Identifiers]
[-L list servers] [-c cmdend] [-D ODBC DSN name]
[-q "cmdline query"] [-Q "cmdline query" and exit]
[-n remove numbering] [-m errorlevel]
[-r msgs to stderr] [-V severitylevel]
[-i inputfile] [-o outputfile]
[-p print statistics] [-b On error batch abort]
[-X[1] disable commands [and exit with warning]]
[-O use Old ISQL behavior disables the following]
<EOF> batch processing
Auto console width scaling
Wide messages
default errorlevel is -1 vs 1
[-? show syntax summary]
For remote server but you need the sql utility installed on the machine you run this.
osql -S <instance name> -U <username> -P <password> -i C:\MyFolder\MyScript.sql -o C:\MyFolder\MyOutput.rpt
this is will help you, it's worked very good for me
Bulk Update Data
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 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 the following command to write a text file from a table:
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
Try creating a temp directory and add this to your path. It could well be Windows protecting the root of the drive.
EXEC master..xp_cmdshell 'bcp "SELECT * FROM DB.dbo.table1" queryout "C:\TempDir\employee.txt" -T -Sservername -U user111 -P pwd -c -t,'
If the above statement works, then it is a permissions issue.
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 output file
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.