#1.How to Pass variables into a atomic script in db2. Something like DEFINE in oracle pl/sql block. And #2 is Is there a way to use INTO clause in select statements in and atomic sqlpl script.
Tags:db2, unix, How to pass variable from shell script into atomic sqlpl in db2.
I have a script file named abcd.pl code like this...
BEGIN ATOMIC DECLARE abcd VARCHAR(50); SELECT xxxx INTO abcd FROM <table>; END;
This is throwing out an error saying that abcd is not a valid after INTO. Can some one suggest an alternative ?
I have another quick question..... How can we pass Input variables into a script like above while calling it from a Unix prompt. Like... $db2 -td@ -f abcd.pl <PARM1> <PARM2> ...etc.
If memory serves, you have to put your DECLARE statements before any "functional" statements.
You posted the question in the DB2 topic area in addition to the "AS/400" and Unix topic areas. Which flavor of DB2 are you running? On what hardware are you running it?
Well, I have no experience with DB2 on Unix, but I know DB2 on AS/400 doesn't allow you to define variables and atomic blocks in scripts. You'd need a stored procedure for that.
UDB (DB2 for Unix) will not allow you to declare variables at the script level, either.
Regarding your second question:
>I have another quick question..... >How can we pass Input variables into a script like above while calling it from a Unix prompt. Like... >$db2 -td@ -f abcd.pl <PARM1> <PARM2> ...etc.
the cleanest solution is to write the script as a shell script, not a SQL statement. Then call the shell script and let the shell interpreter perform the substitutions.
Your example:
$db2 -td@ -f abcd.sql <PARM1> <PARM2> ...etc.
(Note that I've changed the name to abcd.sql as DB2/UNIX doesn't know what a PL/SQL file is.)
And let's assume that the abcd.sql file contains the query:
SELECT * FROM mytable WHERE City = <PARM1> and state = <PARM2>
The DB2/unix way to do this is a slight rewrite. Create the file abcd.sh, containing the following:
# abcd.sh # execute the former Oracle query from abcd.pl # # Check the first parameter. If blank, default to "Tampa" if [ "" = "$1" ]; then CITY="Tampa" else CITY="$1" fi # Check the second parameter. If blank, default to "FL" if [ "" = "$2" ]; then STATE="FL" else STATE="$2" fi db2 connect to database db2 "SELECT * FROM mytable WHERE City = '$CITY' and state = upper('$STATE')" db2 terminate
You would then execute abcd.sh from the command line, passing the desired parameters.
abcd.sh Atlanta Ga abcd.sh Seattle Wa etc...
If the query is more complex than you want to put on a single line, it can be written to another file, but parameter substitution may be different. I'll gladly explain several ways to do that if necessary.