DB2 procedure compilation from KSH script - db2

I am writing a KSH script to accept a .sql file parameter followed by each schema that file is to be compiled in. The script assumes the .sql file is a DB2 9.7 procedure.
I believe I am stuck on the syntax of the db2 command for the termination character. We always use the at symbol ('#'), however the following snippet fails with error "DB21001E The option "-#" specified after the 'db2' command or in the DB2OPTIONS variable is incorrect." Any assistance would be greatly appreciated.
#!/bin/ksh
. $IBM_DB_DIR/db2profile
db2 connect to dwdev3
const_compil_string_suffix="-vtd# -f ../../stored_procedures/"
script_name="ETL.THING.sql"
db2 "$const_compil_string_suffix$script_name"
db2 terminate
I have confirmed that the resulting string command above the 'terminate' does work at linux prompt compiling the procedure as expected:
db2 -vtd# -f ../../stored_procedures/ETL.THING.sql
Thank you in advance.

Try losing the quotation marks:
db2 ${const_compil_string_suffix}${script_name}

I suspect the option string -vtd# might be the culprit.
When using single character options most unix command accept two types of options, those with and those without arguments.
Apparently the # character is being processed as an option argument, however the error message indicates it is seen as an option on its own.
Try separating the options, as in: -v -t -d#, reorder them. or remove one of the -t or -v options.
Beware though. I have no knowledge about the db2 cli. Experiment at your own risk.
Was the -t option added later?

Related

trying to locate output file from postgresql \o -o command

I am making an automated script from terminal that creates a file with the output of \l
But I do not know where the \o command in postgresql prints out the file that it has made. The documentation doesn't inform where.
I did read this, but no luck:
Sincerely
\o points at the named file in current working directory of psql. As you found out, this has some issues in automated environments.
This means you have basically two options:
use absolute paths with \o
Alternatively you can use \cd to set your current working directory inside your psql script.
In your particular case, however, you know that psql -l gives you the same info? That may be easier to manage in an automated environment.

how to invoke unix command in perl

i am trying to execute following unix command but its not getting executed
$array_of_tables= `dbsmp $srv_name`;
print "$array_of_tables\n";
please help me to find out list of tables in a data base through perl scripting.
Also i am trying to copy a file from a path to different path by using following command:-
copy(`cd /osp/slee/service/$srv_name/bin/exec/script.txt`,`cd /osp/local/home/linus/amit/scripts`);
but getting an error:-
Usage: copy(FROM, TO [, BUFFERSIZE])
please provide some solution
Thanks
Use doublequotes instead of back ticks.
copy("/osp/slee/service/$srv_name/bin/exec/script.txt","/osp/local/home/linus/amit/scripts");
and remove the cd
In Perl, the preferable way to capture the output of a system (shell) command is the qx() operator. See http://perldoc.perl.org/perlop.html#Quote-Like-Operators.
$array_of_tables = qx(dbsmp $srv_name);
print("$array_of_tables\n");
Actually, backticks should also work, so the problem must lie with your dbsmp command. I'm not sure what that command is; you'll have to provide more information about the utility and what error you're seeing.
For comparison, I can retrieve the list of tables in my local postgres database as a pipe-separated table using this shell command:
> psql -tAXq main postgres <<<\\d;
And this can be run from Perl as follows:
> perl -e 'print(qx(psql -tAXq main postgres <<<\\\\d;));'

powershell/sqlplus error SP2-0042: unknown command " ■#" - rest of line ignored

I am running this command in powershell:
sqlplus system/passwd#mydb #my_sql
I have tried it with and without backticks and various other versions I found via Google. I keep getting an error when the command is passed off to sqlplus and have been unsucessful in finding the fix. Hopefully someone here can help out?
The error I get is:
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SP2-0042: unknown command " ■#" - rest of line ignored.
So I am sucessfully connecting to the database but there is an extra character being passed to sqlplus in front of the '#'. " ■#" in notepad++ looks like " ¦#"
If you created your SQL command file using a redirect (> or >>) in powershell - like:
myProgram > mySQL.out
and then run it like:
&sqlplus mypw/myuser#mydb.xyz.com "#mySQL.out"
Powershell may have saved the output file in UTF-16 format, which Sqlplus does not like.
(You can confirm by creating the exact same file by hand and then comparing it - byte count will be off and in KDiff you'll get message to the effect that the text is equal, but the files are not binary equal).
To fix - you need to do two things: :
Add some blank lines to the top of your SQL commands - Powershell will still write a BOM (Byte Order Mark) there and it looks like it's pretty hard to get it to avoid that - but sqlplus will just go by it, albeit giving an error - but will move on to the rest of your code OK.
And then run this command in powershell before creating your file: $PSDefaultParameterValues['Out-File:Encoding'] = 'utf8'
See Changing PowerShell's default output encoding to UTF-8
I received this error:
SP2-0042: unknown command " ■S" - rest of line ignored.
and this fixed that - or at least I was able to run it. You can also just cut and past it from one window into another using Notepad++ and that will solve the BOM and encoding issue.
Update Problem Solved. This turned out being "not seeing the trees through the forest". I have been using these sql scripts for several years without issue called from a bash script. When I tried converting the bash script to powershell and ran into issues I blamed it on powershell. However; it turned out there was something corrupt in the sql file itself. There were no obvious errors when looking at the file in notepad++ even with show all symbols clicked and it was ANSI format. I determined it was the sql file itself when I manually ran sqlplus from a cmd window I still had the same error I was getting with powershell. I rewrote the script and saved it and the problem was fixed. I should have manually ran the script on day one and I probably could have resolved sooner.
I had the same problem. My issue was caused because the script file was saved as unicode. I don't know if this will help you or not, but here is how I fixed it:
Edit the script with notepad. Click File -> Save As. Change type from Unicode (or whatever) to ANSI, and save.
A couple of suggestions
Try the invoke operator:
&sqlplus system/passwd#mydb #my_sql
Try start-process:
start-process -NoNewWindow -FilePath sqlplus -ArgumentList #"
system/passwd#mydb #my_sql
"#
I had typical problem. The message was:
unknown command "and" - rest of line ignored.
The reason was an empty string in code.
e.g.
select ...
from ...
where ...
[empty string]
and ... < here was an error message
use as following
sqlplus -s system/passwd#mydb "#my_sql";

How to use \COPY command inside a function in PostgreSQL

I want to export the table into a .CSV file using \COPY command. I can able to do that as a stand-alone command. I can't embed the same line inside a function in PostgreSQL. Actually that call should come from a ECPG. I chose \COPY over COPY command as I don't have super-user account! Please guide me on this.
Thanks and Regards,
Siva.
Unfortunately, all of the \ commands are psql commands. You can run psql -E to see what those commands expand into and get sent to the server as, but if you need a super-user account to run COPY, you're going to need a super-user account to do this.
If you are talking about using copy with plpgsql, I think this might help:
Dynamically-generated table-name in PostgreSQL COPY command

postgres equivilent of oracle sqlplus "set echo on"

Is there an equivalent in PostgreSQL of the Oracle SQLPLUS "set echo on" so that I can get batch input
statements echoed in the output?
I have a very large file with input statements in it that has a few errors when I run it.
I am having difficulty finding the statement that produced the error because psql is only reporting
the error - not the statement that generated the error.
You need to pass the -a (or --echo-all) argument to psql. It's described at https://www.postgresql.org/docs/current/static/app-psql.html under OPTIONS.
PostgreSQL also logs errors in its server logs, along with the statement that caused it. That might be useful to bear in mind for debugging errors with tools other than psql that don't report errors very well.