I've read the documentation (sqlcmd Utility), but I can't get sqlcmd's -v parameter to work as expected.
Assuming this SQL script (echo.sql):
:setvar THE_PATH C:\Users\Craig\Desktop
PRINT 'THE_PATH: $(THE_PATH)'
:setvar THE_TOP 10
PRINT 'THE_TOP: $(THE_TOP)'
When run at the PowerShell prompt without the -v argument set:
PS> sqlcmd -E -S 'server' -d 'database' -i '.\echo.sql'
THE_PATH: C:\Users\Craig\Desktop
THE_TOP: 10
Setting the numeric variable (THE_TOP) are ignored:
PS> sqlcmd -E -S 'server' -d 'database' -i '.\echo.sql' -v THE_TOP=5
PS> sqlcmd -E -S 'server' -d 'database' -i '.\echo.sql'
THE_PATH: C:\Users\Craig\Desktop
THE_TOP: 10
If I eliminate the default value for THE_TOP in echo.sql, it reinforced the assertion that the parameter is being ignored:
:setvar THE_TOP
PRINT 'THE_TOP: $(THE_TOP)'
PS> sqlcmd -E -S 'server' -d 'database' -i '.\echo.sql' -v THE_TOP=5
THE_PATH: C:\Users\Craig\Desktop
THE_TOP: $(THE_TOP)
If I attempt to set the THE_PATH parameter, I get:
PS> sqlcmd -E -S 'server' -d 'database' -i '.\echo.sql' -v THE_PATH="C:\path"
Sqlcmd: ':\path': Invalid argument. Enter '-?' for help.
What is the correct -v syntax?
OK, this is retarded.
If :setvar is used in the script:
:setvar THE_TOP
PRINT 'THE_TOP: $(THE_TOP)'
then you attempt to set it at the PowerShell prompt, you'll get an error:
PS> sqlcmd -E -S server -d database -i .\echo.sql -v THE_TOP=5
'THE_TOP' scripting variable not defined.
However, if you DO NOT set it in the script:
-- variable disabled
-- :setvar THE_PATH
PRINT 'THE_TOP: $(THE_TOP)'
then you attempt to set it at the PowerShell prompt, then it will work as (not really) expected:
PS> sqlcmd -E -S server -d database -i .\echo.sql -v THE_TOP=5
THE_TOP: 5
** edit **
To supply a dynamically-generated path as a parameter to a script file (extract.sql):
:out $(THE_PATH)\extract.csv
SELECT *
FROM the_table
WHERE the_key = $(THE_ID)
...
Execute in PowerShell session:
PS> $cd = Get-Location
PS> sqlcmd -E -S 'server' -d 'database' -i '.\extract.sql' -v THE_ID=5 THE_PATH=`"$cd\build`"
Related
#!/bin/sh
today=$(date +"%Y%m%d")
echo $today
#20220720
psql -h myhost -U sqladmin -d trades 8787 -c "select * from recalculate_value("$today")";
When executing psql, the $today is not getting interpreted correctly.
What is the proper way to pass the $today in the psql command line?
select * from recalculate_value('20220720') ;
I'm trying to run a parameterized query from shell.
But when I run:
p='some stuff'
psql -d "dbname" -v v1="$p" -c "SELECT * FROM table WHERE name=:'v1'"
I'm getting the following error:
ERROR: syntax error at or near ":"
Meanwhile:
psql -d "dbname" -v v1="$p" -c "\echo :'v1'"
works normally. (returns as expected: 'some stuff')
You cannot use the variable defined in -v in -c command (see below). Try passing the command into the standard input:
psql -d "dbname" -v v1="$p" <<< "SELECT * FROM table WHERE name=:'v1'"
From the document:
-c command
--command command
...
command must be either a command string that is completely parsable by
the server (i.e., it contains no psql-specific features), or a single
backslash command.
...
-v does set the psql's internal variable, which is psql-specific features. That's why you got the syntax error.
I use the following PowerShell command to run a Perl command (pp):
pp --% -u -x -g --link openssl.exe --link libpng16-16_.dll -o D:\Dati\file.exe -F Bleach="^(AT_|DB_)" "G:\Scripts work\script.pl"
Copy and Paste it in PowerShell, and it works like a charme. Now I want to run the same from a bat file. I tried:
set CMD_LINE_ARGS="%*"
powershell -Command "{pp --% -u -x -g --link openssl.exe --link libpng16-16_.dll -o D:\Dati\file.exe -F Bleach="^(AT_^|DB_)" "G:\Scripts work\script.pl"}"
I tried to escape the pipe "|" with `.
My bat is run, no errors, but my script is not lunched. Any idea?
You need to escape special CMD characters in batch in order for them to parsed by PowerShell.exe. Some characters include ",^. Inner double quotes can be double quoted or escaped with a backslash. Command characters need to be escaped with ^.
powershell.exe -Command "{pp --% -u -x -g --link openssl.exe --link libpng16-16_.dll -o D:\Dati\file.exe -F Bleach=\"^^(AT_^|DB_)\" \"G:\Scripts work\script.pl\"}"
I have a function block as below which is getting called from somewhere. The call is happening fine but when it's executing any of the below psql check commands eg:
local db_availability_check_88=`psql -h 10.95.187.88 -p 5444 -c "\pset tuples_only" -c "select pg_is_in_recovery;"|grep -v "Tuples"`
The script terminates immediately if the script is unable to communicate to the database via the above psql command. I want thee script to continue its execution. If the connectivity fails it should continue with the next statements.
check_db_status_function () {
echo "entered function block db_status"
local db_availability_check_67=`psql -h 10.95.167.87 -p 5444 -c "\pset tuples_only" -c "select pg_is_in_recovery;"|grep -v "Tuples"`
local db_availability_check_68=`psql -h 10.95.167.88 -p 5444 -c "\pset tuples_only" -c "select pg_is_in_recovery;"|grep -v "Tuples"`
local db_availability_check_69=`psql -h 10.95.167.89 -p 5444 -c "\pset tuples_only" -c "select pg_is_in_recovery;"|grep -v "Tuples"`
if [[ ( $a1 = "down" ) && ( $a3 != "primary" ) && ( $db_availability_check_67 = 't' ) ]];
then
echo "pgPool services are down on the node:$a2"
echo "Promoting..."
/u01/edb/pgpool3.6/bin/pcp_attach_node -w -U pcpuser -h localhost -p 9898 $a0
fi
The script o/p is as :
Nested block 1
check_db_stat
entered function block db_status
psql.bin: could not connect to server: Connection refused
Is the server running on host "10.95.167.88" and accepting
TCP/IP connections on port 5444?
Thanks,
Sandeep
I am running psql from command line and sending output to a file. It is a simple select statement on a view, but I am getting a syntax error when I have a column alias that starts with a number.
I ran the query in PgAdmin and it works (which makes me believe that this is some sort of issue with psql). I also tried adding a '_' to the beginning of the alias and that allows it to go through.
works: 'abc as "_1abc"'
doesn't work: 'abc as "1abc"'
psql -u <username> -h <host> -p <port> -d <DB> -o <outputfile> -A -c
"SELECT abc as "1abc" From example.view
This is the error I get:
ERROR: syntax error at or near "1"
It is a problem with nested double quotes. You need to escape the inner ones.
psql -u <username> -h <host> -p <port> -d <DB> -o <outputfile> -A -c "SELECT abc as \"1abc\" From example.view"