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

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

Related

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]

How to execute multiple queries using psql command from bash shell?

I need to execute postgresql queries from command line using psql -c command.
For every psql command, it opens a new tcp connection to connect to the database server and execute query which is a overhead for large number of queries.
Currently I can execute single query like this:
psql -U postgres -h <ip_addr> -c "SELECT * FROM xyz_table;"
When I tried to execute multiple queries as below, but only the last query got executed.
psql -U postgres -h <ip_addr> -c "SELECT * FROM xyz_table; SELECT * FROM abc_table;"
Can anyone help me and tell me the proper way to do it?
-c processes only one command. Without it however psql expects commands to be passed into standard input, e.g.:
psql -U postgres -h <ip_addr> <database_name> << EOF
SELECT * FROM xyz_table;
SELECT * FROM abc_table;
EOF
Or by using echo and pipes.
at least from 9.6.2 this approach works as well:
psql -c "select now()" -c "select version()" -U postgres -h 127.0.0.1
now
2017-12-26 20:25:45.874935+01
(1 row)
version
PostgreSQL 9.6.2 on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 5.3.1-14ubuntu2) 5.3.1 20160413, 64-bit
(1 row)
Using echo and a pipe to fit it on a single line:
echo 'SELECT * FROM xyz_table; \n SELECT * FROM abc_table' | psql -U postgres
The --file parameter executes a file's content
psql -U postgres -h <ip_addr> -f "my_file.psql"
All the output will be sent to standard output
http://www.postgresql.org/docs/current/static/app-psql.html

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

BCP utility:"Copy direction must be either 'in', 'out' or 'format'"

I get below mentioned error when I run BCP command for trusted connection:
Copy direction must be either 'in', 'out' or 'format'.
I tried searching MSDN, where it specifies that servername passed could be incorrect.
The command I am trying is:
bcp %SQL_database%..TABLE1 in \FileSERVER\file.dat -f\fileserver\Formats\file.fmt -eERR.txt -m1000000 -C -T RAW -S%SQL_server%
When I pass a username and password instead of using the -T option, it works. The command is executed from command prompt by passing parameters from command line.
Your -C and -T options are flip-flopped - -C -T RAW instead of -C RAW -T.
Check the bcp utility's online documentation for confirmation that -C rather than -T should precede RAW.
Try this instead:
bcp %SQL_database%..TABLE1 in \FileSERVER\file.dat -f\fileserver\Formats\file.fmt -eERR.txt -m1000000 -C RAW -T -S%SQL_server%
My guess is that you probably misplaced the -T option when switching to a trusted connection (with the -T option) from integrated security (with the -U and -P options).

Resources