Using variables when executing single command in PSQL - sql

When using PSQL's variables, I can run it as follows:
psql -d database -v var="'123'"
And I will then have access to the variable var when I type the following in the PSQL terminal:
select * from table where column = :var;
This variable feature also works when the SQL is read from a file:
psql -d database -v var="'123'" -f file.sql
But when I try to run the SQL as a single command:
psql -d database -v var="'123'" -c "select * from table where column = :var;"
I can't access the variable and get the following error:
ERROR: syntax error at or near ":"
Is it possible to pass variables to single SQL commands in PSQL?

It turns out that, as man psql explains, the -c command is limited to SQL that "contains no psql-specific features":
-c command, --command=command
Specifies that psql is to execute one command string, command, and then exit. This is useful in shell
scripts. Start-up files (psqlrc and ~/.psqlrc) are ignored with this option.
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. Thus you cannot mix SQL and psql meta-commands
with this option. To achieve that, you could pipe the string into psql, for example: echo '\x \\ SELECT
* FROM foo;' | psql. (\\ is the separator meta-command.)
It looks like I can do what I want by passing in the SQL using stdin:
echo "select * from table where column = :var;" | psql -d database -v var="'123'"

Related

Execute HQL file with beeline command

i'm trying to execute a query in a test.hql with beeline and i'm not getting results
here the command :
beeline -u "jdbc:hive2://master01:2181,master02:2181,master03:2181/;serviceDiscoveryMode=zooKeeper;zooKeeperNamespace=hiveserver2" -f test.hql
in test.hql file :
select * from db1.tab1 limit 20;
But if i execute the command with the parameter -e which execute directly the query,it gives me the results
beeline -u "jdbc:hive2://master01:2181,master02:2181,master03:2181/;serviceDiscoveryMode=zooKeeper;zooKeeperNamespace=hiveserver2" -e "select * from db1.tab1 limit 20;"
Am i missing something ?
You're right. -e is used to execute one or multiple queries and see the result.
-f is mostly used to run scripts like insert into mytab select * from ourtab; where you dont need to see all the rows.
For more options you can check-
https://cwiki.apache.org/confluence/display/hive/hiveserver2+clients#HiveServer2Clients-BeelineHiveCommands

Using for loop bat file windows for multiple command calls

I want to export all data from sql server table to a csv, I know I can get the desired result by:
sqlcmd -S . -d database -E -s, -W -Q "SELECT * FROM TABLENAME" > file.csv
I have many tables, so I want to create a .bat file that do the work for me, I have this:
set "list = A B C D"
for %%x in (%list%) do (
sqlcmd -S . -d database -E -s, -W -Q "SELECT * FROM %%x" > %%x.csv
)
But I am getting errors I don't know (I am not an expert in bat files). Why this does not work? How can I do what I want?
Spacing is important when using set (unless you're doing math with the /A switch). As written, the variable you're setting isn't %list%. It's %list %. Change your set command as follows:
set "list=A B C D"

How to format SQLCMD output

I am using below command line to run a SQL query using SQLCMD
sqlcmd -S Server -Q "select top 100 * From people" -d people -t 10
The table has 20 columns and when i look at output command line window wraps the text and makes it difficult to read.
I want my results to be displayed the same way it displays in SQL Server Management Studio (properly formatted). I am not looking for any grids, but i need all my columns to be displayed in row 1 and the results properly beneath.
Thanks in advance.
Answer
We can set the width of each column.
C:/> sqlcmd -S my_server
> :setvar SQLCMDMAXVARTYPEWIDTH 30
> :setvar SQLCMDMAXFIXEDTYPEWIDTH 30
> SELECT * from my_table
> go
We can also set it like this: sqlcmd -S my_server -y 30 -Y 30.
Details
SQLCMDMAXVARTYPEWIDTH (-y)
It limits the number of characters that are returned for the large variable length data type
SQLCMDMAXFIXEDTYPEWIDTH (-Y)
Limits the number of characters that are returned for the following data types
Note: setting -y has serious performance implications.
See https://learn.microsoft.com/en-us/sql/tools/sqlcmd-utility
Formatting issues usually pop up due to your console window.
One solution is to output to the file and use notepad/your favorite editor:
sqlcmd -S myServer -d myDB -E -Q "select top 100 * From people"
-o "output.txt"
This is how I isolated a scalar.
sqlcmd -S xxx.xxx.xxx.xxx,xxxxx -d MyDb -U myUser -P MyPassword -h -1 -W -Q "set NOCOUNT ON; select a from b where b.id='c'"

BCP command insert text file to SQL table

I am trying to import into SQL a text file using this BCP command:
bcp test.dbo.bcp2 in C:\Test\test.txt -c -t -SSQServer -U user -P
test1 -t \t -r\n -e C:\Test\error.txt
The text.txt file has \t as column delimiter and \n as row delimiter.
The error received is Unexpected EOF.
I can confirm that the SQL table has the right table definition so there should not be any conversion errors.
i think no need to put any delimiter have text which is well arranged example if you have data in the excel copy and paste it in the text file and run the command
BCP tablename in c:\test.txt -S server name -Uuserid -Ppassword -c

How to extract results with column name in shell from postgres

I would like to extract results with title of the column from postgres. I am using shell script to do the same. Please find following code which is giving only result without header.
#!/bin/sh
DATABASE=retail
USERNAME=root
HOSTNAME=localhost
export PGPASSWORD=root
psql -h $HOSTNAME -U $USERNAME $DATABASE << EOF
COPY (select name,rollno,mark from student';')
EOF
echo "Hi \n Please find student report " | mutt -a "/tmp/query1.csv" -s " Alert" -- abc_email#gmail.com
COPY statement has option HEADER. Use it. Another issue in your example is missing target in COPY statement (and other syntax error). From security reasons, it should be stdout in this case (I do export of pg_class table columns relname, and relpages):
psql -c "COPY pg_class(relname,relpages) TO stdout CSV HEADER" postgres > /tmp/query1.csv
With this syntax you will get valid CSV file.