I am trying to use sqlcmd to output the results from a stored procedure to a text file. However, it is putting a space between the resulting columns (a default delimiter) and I need to remove this. I am using the below code and have tried specifying nothing as the column separator see code below (-s "") but this does not work. It gives me a missing argument error.
sqlcmd -S Server name -U Username-P Password -d Db name-Q
"exec Stored procedure" -h -1 -s "" -o c:\newtest.txt
How can I specify nothing as the delimiter?
It can be done with bcp, using -t 0x90
-t is the field terminator in bcp. you can use it on stored procs as well as tables so it should do everything you need.
I just verified this works:
bcp dbname.dbo.tablename out "c:\test.txt" -S serveraddress -U username -P password -t 0x90 -c
Using a stored proc would be something like:
bcp "EXEC YourDatabaseNameGoesHere.dbo.sp_myproc ''0123,0234,0345''"
Related
I am exporting sql view using .bat script:
sqlcmd -U HPH_COM -P HPH_COM -S vTF-DB\LEVEL2 -d HPH_COM -Q " SELECT TOP (1000) * FROM HPH_COM.dbo.REP_TEMP_STOCKS_LASTDAY " -o "C:\sajtovi\TF1_PyroMeasuresLastDay.csv"
And its working. However, exported csv file is not organized as when you export using SSMS: using batch one column in sql is not copied to one column in excel, but more columns from sql to one column in excel. so i cannot play with exported data. Any idea how to write script which will give same results as exported with ssms?
Thanks
you can use these options.
-h rows_per_header
-s col_separator
-W (remove trailing spaces)
and also specify set nocount on to prevent the completion message from appearing.
It looks like this.
sqlcmd -U HPH_COM -P HPH_COM -S vTF-DB\LEVEL2 -d HPH_COM -Q "set nocount on; SELECT TOP (1000) * FROM HPH_COM.dbo.REP_TEMP_STOCKS_LASTDAY " -o "C:\sajtovi\TF1_PyroMeasuresLastDay.csv" -W -h -1 -s,
I'm trying to export a dataset from SQL with SQLCMD to a .csv file.
The field I'm filtering has the Format e.g. "490658" (Yes, with quotation marks)
In my .cmd file the SqlString looks like
set SqlString=Set NOCOUNT ON;SELECT * FROM A_DPD_Print_Manuell WHERE (mpscref1 = '"%1%"')
this
And the sqlcmd like this
sqlcmd -S XXX -U XXX -P OXXX -d XXX -s "," -Q "%SqlString%" -W -o "%Datei1Pfad%"
But it returns the error: No closing quote after the string
(Google translator im sorry if its very wrong)
I don't know how to handle this error.
Thanks in advance
So i figured it out.
In the SQL String I needed to add more " because the cmd thought the command stops at the first ".
So the sqlcmd string was like
sqlcmd -S XXX -U XXX -P OXXX -d XXX -s "," -Q "SqlString=Set NOCOUNT ON;SELECT * FROM A_DPD_Print_Manuell WHERE (mpscref1 = '" [...]
I just added mpscref1 = '""%1%""'
To the sql string and it worked.
I am trying to create a windows batch file to export data from an SQL table to a .txt file with <~> as field separator.
I tried to escape with ^ still i am unable to export with <~> as field separator, below is the command
sqlcmd -S YourDBServer -d DB_NAME -E -Q "select mcc_code, 1, name, address, domain, mask from table" -o "tableName_date.txt" -s"^<~^>" -W -h -
how should i escape? Please help.
Im am currently using this command to try to write to a text file the output that I PRINTED in the sql console(messages) using a stored procedure that uses the 'print' command to print stuff. I want to get that printed stuff into a text file using bat executable.
sqlcmd -Q "exec myprocedure" -S servername-d dbname-o C:\yourOutput.txt
But whenever I run this batch file, it just opens the yourOutput.txt and its blank...
C:\filepath\sqlcmd -Q "exec procedureName" -S servername-d dbname-o
Sqlcmd: 'dbname-o': Unexpected argument. Enter '-?' for help.
SQL auth :
sqlcmd -Q "exec schema.myprocedure;" -S servername -d dbname -u sqluser -p "sqluserpassword" -o C:\yourOutput.txt
Windows auth
sqlcmd -Q "exec schema.myprocedure;" -S servername -d dbname -o C:\yourOutput.txt
Should work with apropriate servername, schema, sqluser and sqluserpassword
Please note that the expected servername is : -S [protocol:]server[instance_name][,port]
Exemples :
-S tcp:localhost\instanceXXXXX,1433]
-S tcp:192.168.4.9\instanceYYYYY,1333]
Type : SELECT ##servername + '\' + ##servicename to get servername\instancename. For exemple it can gives you : "CO-DESQL01\MSSQLSERVER"
I am using bcp command in sql server to export data generated from a query to .csv file with the help of following command
*xp_cmdshell bcp EXEC .DBO. QUERYOUT -U -P /c /t, -T -S *
It is working fine and exporting data as expected but now we have a column which contain multilingual data and during exporting with above command data in csv file shows as "????????".
After doing some googling I found some other switch like -w to be used for unicode character but this option is creating unicode file and doesnot open in excel properly(columns are not separted by comma(,))
Can anybody help me if I am missing anything?
/t, is not correct. It should read -t, to set the field delimeter. And -w is the correct flag for unicode. Also I don't think -U/-P is used with -T.
bcp AdventureWorks2012..myTestUniCharData out C:\myTestUniCharData-w.Dat -w -t, -T
See the examples area in the following tech note:
http://technet.microsoft.com/en-us/library/ms188289.aspx
try using -w -T only.
don't add a field delimeter
i dont think "/c" or "/t" are the correct switches. see the documentation here: it should be -c or in your case -w for unicode. try from the cmd line using:
bcp AdventureWorks2012.dbo.myTestUniCharData IN C:\temp\logs\ex170112.log -w -S
localhost\SQLEXPRESS -T
you could also use a format file (.fmt) to tell it to format certain columns. see here.
Using -w switch produces a file with UTF16 format, which is not easy to work with.
If your special characters are covered by ISO 8859-1 characters, then use the switches -C -c in your bcp command. -C makes a ISO 8859-1 file.
The following worked for me to import unicode from text file:
bcp dbo.MyTable in unicode-input.txt -S localhost -T -d EWBKonfig -C 65001 -c -t, -r '0x0A'