BCP Parameter issues - sql

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

Related

Bcp command is getting error Out

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.

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

Executing of large script in SQL Server 2008

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

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

TSQL QUERY to dump table into a file

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