Export SQL Record into .csv using sqlcmd - sql-server

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.

Related

How to export data from sql to excel using batch - exporting one column in sql per one column in excel?

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,

Export SQL table into text file using SQLCmd with <~> as field separator

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.

TSQL - Trying to write printed output to a text file using batch file

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"

-X option not disable sql commands in sqlcmd

I use sqlcmd to execute large sql file which insert multiple of records into database. So when sqlcmd run it display a error message like "syntax error near 'Ed'..." . I known Ed is sql commands and i found -X will disable that kind of commands but it is not work.
My command like this:
sqlcmd -S "tcp:ip,1433" -U "sa" -P "pass" -d "dbname" -c "GO" -k 1 -f 65001 -i "C:\sql.sql" -X -x
My sql file content:
Go
insert into tablename (title) values (N'title
Ed some more data
some more data
some more data')
Go
insert into tablename (title) values (N'title
Ed some more data
some more data
some more data')
Go
Could you give me some help? please.
Thanks you.
It use -X[1] instead of -X.
sqlcmd -S "tcp:ip,1433" -U "sa" -P "pass" -d "dbname" -c "GO" -k 1 -f 65001 -i "C:\sql.sql" -X[1]

Remove column space from sqlcmd output file

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''"

Resources