-X option not disable sql commands in sqlcmd - sql-server

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]

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,

Update Local SQL Server with a Bash Script

Question:
What is the correct format to use in my bash script to be able to run the -Q option?
Case: Update local database from S3 every night to run reports on our on-premise server
Code:
#!/bin/bash
#get latest file from S3
BACKUP_MARKETING=`aws s3 ls [some_folder]/[some_subfolder]/ --recursive | sort | tail -n 1 | awk '{print $4}'`
#download the file locally
aws s3 cp s3://[some_folder]/$BACKUP_MARKETING /var/opt/mssql/backup/marketing
#get the file name
BAK_MARKETING=`find [folder]/ -type f -name "*.bak"`
#drop the database to avoid conflicts from not backing it up
/opt/mssql-tools/bin/sqlcmd -S localhost -U [username] -P '[password]' -Q 'DROP DATABASE [db_name]'
#restore the database
/opt/mssql-tools/bin/sqlcmd -S localhost -U [username] -P '[password]' -Q RESTORE DATABASE "[db_name]" FROM DISK = "/var/opt/mssql/backup/$BAK_MARKETING" WITH MOVE "[db_name]" TO "/var/opt/mssql/data/[db_name].MDF", MOVE "[db_name]_log" TO "/var/opt/mssql/data/[db_name].LDF"
Error
Sqlcmd: 'DATABASE" "[db_name]" "FROM" "DISK" "=" "/var/opt/mssql/backup/marketing/[db_name].bak" "WITH" "MOVE" "[db_name]" "TO" "/var/opt/mssql/data/[db_name].MDF," "MOVE" "[db_name]_log" "TO" "/var/opt/mssql/data/[db_name].LDF': Unexpected argument. Enter '-?' for help.
Apparently I had to concatenate my variables on the SQL command. Here is the working version plus I added the REPLACE option to it
/opt/mssql-tools/bin/sqlcmd -S localhost -U [username] -P '[password]' -Q 'RESTORE DATABASE [db_name] FROM DISK = "/var/opt/mssql/backup/'**$BAK_FILE**'" WITH REPLACE, MOVE "[db_name]" TO "/var/opt/mssql/data/[db_name].MDF", MOVE "[db_name]_Log" TO "/var/opt/mssql/data/[db_name].LDF"'
Could you not use the -i Option instead?
I had some problems as well using Q, so i replaced it with -i and placed the code within a .sql file instead.
I ended up with;
SET SQLusername=sa
SET SQLpassword=password
SET SQLserver=dnsnameorIp
SET SQLdatabase=databasename
sqlcmd -U %SQLusername% -P %SQLpassword% -S %SQLserver% -d %SQLdatabase% -i mycode.sql -o outputResult.txt

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"

How to execute a script through sqlcmd in MS Sql server

I got a .sql file in which {0} kind of things used. I would like to know how can i pass values in the sqlcmd for {0} variable.
Sample script (test.sql):
GO
PRINT '{0}'
GO
And I am trying to executing through this command line
sqlcmd -S . -d master -E -i test.sql
But, where should i pass the parameter?
Just Try as below:
test.sql
GO
Print $var
GO
sqlcmd -S . -d master -E -i test.sql -v var="helloWorld"
You can use the -v switch to pass in variables:
sqlcmd -S . -d master -E -i test.sql -v varPrintMe="{0}"
Then you'll need to put the variables in $() in your sql script:
GO
PRINT $('varPrintMe')
GO

how do i pass in a command to sqsh and get the output to a file in one go?

I'm trying to set up a simple loop to periodically query a database table in bash. Normally I seem to have to do:
sqsh -s SERV -U user -P passwd -D db -L bcp_colsep=','
then within sqsh I have to type:
select * from some_table where foo=bar
\go -m bcp > /path/to/output.out
I was trying to use the -C option to sqsh to pass in the command like this:
sqsh -s SERV -U user -P passwd -D db -L bcp_colsep=',' -C 'select * from some_table where foo=bar \go -m bcp > /path/to/output.out'
but I keep getting:
Incorrect syntax near '\'.
How can I get the desired effect?
When you use the -C option to pass on a SQL statement to sqsh, the \go command will be implicitly executed. To get the output in 'bcp' result style you need to set the variable 'style=bcp' using the -L parameter or use -mbcp as a commandline parameter and just redirect the output to a file, or use the sqsh -o parameter to specify a filename for output. So basically your command would look like:
sqsh -S SERV -U user -P passwd -D db -L bcp_colsep=',' -m bcp \
-C 'select * from some_table where foo=bar' > /path/to/output.out
HTH, Martin

Resources