Operating System - HP-UX
1752679 Members
5476 Online
108789 Solutions
New Discussion юеВ

DB2 SQL from Shell Script

 
Prabhu_9
Occasional Advisor

DB2 SQL from Shell Script

hi,
I want to run a DB2 script from the shell.
How can i do it?
I even want to accept an argument value from the user which i'l be substituting in the script & then execute the DB2 script by a shell script.

Please help me out...
tx in advance.
7 REPLIES 7
Steven E. Protter
Exalted Contributor

Re: DB2 SQL from Shell Script

Shalom,

General scripting rules will work here. Simplistic example.

arg1=$2
# second argument is in a variable, which can be used any way you like below

sqlplus -s ${DB_CONNECT} < INSERT INTO T_PROCESS_STATS ( START_DT, PROCESS_NAME_TXT, END_DT, RECORDS_PROCESSED_VAL, MSG_TXT)
VALUES (
SYSDATE, 'backup_ref_data_dly.ksh', SYSDATE, 0, 'Backup of reference data - daily');
SHOW SQLCODE
end_sql

Note the general principle is to put what would normally be hand typed statements into the script line by line. Each line simulates an enter between <
Change the sqlplus to the proper DB2 equivalent and you can script darned near anything you want.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Prabhu_9
Occasional Advisor

Re: DB2 SQL from Shell Script

Is this for me ?
OldSchool
Honored Contributor

Re: DB2 SQL from Shell Script

"Is this for me ?"

Yes.

He's showing you a shell script containing a "here-doc". That is the part from
bounded by "<
anything between those strings are sent, in this case, to the "sqlpus" command as if they were typed at the keyboard.

Note that their are rules about here-docs. from the sh-posix man page:

<<[-]word The shell input is read up to a line that matches word, or to an end-of-file. No parameter substitution, command substitution or file name generation is performed on word. The resulting document, called a here-document, becomes the standard input. If any character of word is quoted, no interpretation is placed upon the characters of the document. Otherwise, parameter and command substitution occurs, \newline is ignored, and \ must be used to quote the characters \, $, `, and the first character of word. If - is appended to <<, all leading tabs are stripped from word and from the document.


Prabhu_9
Occasional Advisor

Re: DB2 SQL from Shell Script

*** Replace to DB2 equivalent*****
i have samples for calling Oracle or SQL Server SQLs from unix shell script... but
iam not finding script to call DB2 from shell script. thats what i wanted.
OldSchool
Honored Contributor

Re: DB2 SQL from Shell Script

hmm...db2 command line processor is called "db2"...is that what you're looking for?

also see here:

http://sqlrelay.sourceforge.net/sqlrelay/gettingstarted/db2.html


Prabhu_9
Occasional Advisor

Re: DB2 SQL from Shell Script

Nope.

Iam looking for samplee shell script which 1) connects to DB2
2) Fetches count from a table.

I just need exact syntax of how to connect to DB2 from shell script and fetch the count.
OldSchool
Honored Contributor

Re: DB2 SQL from Shell Script

well, how would you do it from the command line? If you can't answer that, it's time to hit the manuals