I have an sql file that contains below script that is ran via isql.
May I ask whats wrong with my output syntax? I am getting "Incorrect syntax near the keyword 'output'"
Sybase ASE version is 15.7
select * from tempdb..M3_STI_extracts_checking
output to employee.txt format ASCII
GO
isql offers the possibility to write the output into a file, if you set the option -o (Utility Commands Reference).
input.sql
select * from tempdb..M3_STI_extracts_checking
go
isql -i input.sql -o employee.txt
-J sets the charset (ASE 15.7 charsets)
isql -i input.sql -o employee.txt -J ascii_7
Was able to workaround by passing the variable from a shell script.
test.sh
output_file=test_file_'date +%m%d%Y'
${PARAM} isql << EOF
select * from tempdb..M3_STI_extracts_checking
GO > ${output_file}
EOF
Related
we are exporting data into a csv file by using unix shell script (using snowsql)
below is the script
#!/bin/ksh
snowsql -c newConnection -o log_level=DEBUG -o
log_file=~/snowsql_sso_debug.log -r SRVC_ACCT_ROLE -w LOAD_WH -d
ETL_DEV_DB -s CTL_DB -q "select * from mytable" -o friendly=False -o
header=False -o output_format=pipe -o timing=False>test_file.csv
output starts something like below
|:--------|:-----------|
i dont want to display above lines in my csv file, what is the option that we need to use in my snowsql query?
appricate your response.
Thanks.
Providing my comment as an answer, just in case it works better for you.
I would leverage a COPY INTO command to create a CSV file to an internal stage location:
https://docs.snowflake.com/en/sql-reference/sql/copy-into-location.html
And then use a GET statement to pull the file down to your Unix machine.
https://docs.snowflake.com/en/sql-reference/sql/get.html
This gives you greater control over the output format and will likely perform faster, as well.
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
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).
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
I am using bcp command in sql server to export data generated from a query to .csv file with the help of following command
*xp_cmdshell bcp EXEC .DBO. QUERYOUT -U -P /c /t, -T -S *
It is working fine and exporting data as expected but now we have a column which contain multilingual data and during exporting with above command data in csv file shows as "????????".
After doing some googling I found some other switch like -w to be used for unicode character but this option is creating unicode file and doesnot open in excel properly(columns are not separted by comma(,))
Can anybody help me if I am missing anything?
/t, is not correct. It should read -t, to set the field delimeter. And -w is the correct flag for unicode. Also I don't think -U/-P is used with -T.
bcp AdventureWorks2012..myTestUniCharData out C:\myTestUniCharData-w.Dat -w -t, -T
See the examples area in the following tech note:
http://technet.microsoft.com/en-us/library/ms188289.aspx
try using -w -T only.
don't add a field delimeter
i dont think "/c" or "/t" are the correct switches. see the documentation here: it should be -c or in your case -w for unicode. try from the cmd line using:
bcp AdventureWorks2012.dbo.myTestUniCharData IN C:\temp\logs\ex170112.log -w -S
localhost\SQLEXPRESS -T
you could also use a format file (.fmt) to tell it to format certain columns. see here.
Using -w switch produces a file with UTF16 format, which is not easy to work with.
If your special characters are covered by ISO 8859-1 characters, then use the switches -C -c in your bcp command. -C makes a ISO 8859-1 file.
The following worked for me to import unicode from text file:
bcp dbo.MyTable in unicode-input.txt -S localhost -T -d EWBKonfig -C 65001 -c -t, -r '0x0A'