DB2 in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting DB2 in awk
# 1  
Old 01-04-2016
DB2 in awk

Hi,

I want to retrieve the base table name for a table and replace the table with the view name in the file.

I was trying to retrieve the base with the below db2 command but i am getting "1" as return value.

Could you anyone please assist ?

Code:
 Table_name=system("db2 -x \"SELECT TRIM(TD.BSCHEMA) || \'.\' || TRIM(TD.BNAME) AS TABLE_NAME FROM SYSCAT.TABDEP TD WHERE TD.BTYPE=\'T\' AND TD.TABSCHEMA=\'XXXXXXX\' AND TD.TABNAME=\'\"$View_Name\"\' or
der  by BSCHEMA desc fetch first 1 row only\"");
    print Table_name;


Regards,
Nantha.Y

---------- Post updated at 05:21 AM ---------- Previous update was at 04:51 AM ----------

Hi ,

Input line : Left Outer Join XXXXXX.YYYYYYY_YYYYY_YYY

where XXXXX is VIEW_SCHEMA and YYYYY_YYYY_YY is VIEW_NAME

Objective : Replace the VIEW NAME with base table name

Output : Left Outer Join ZZZZZZ.AAAAAAA
where ZZZZZ : Base table schema
AAAAAA is Base Table name.

Regards,
Nantha.Y
# 2  
Old 01-04-2016
RudiC had made a suggestion in your other post; I'll expand on it here:

(1) Print or echo the query before passing it to DB2.
(2) Or just print it and do not pass it to DB2.
(3) When you have the query in front of you, have a close look at it. Maybe there's something wrong with it.
(4) If you think the query is correct, copy and paste it in your ad-hoc query tool (IBM Data Studio or TOAD for DB2 or whatever you are using.) Then execute it and check the result.
If you do not have a graphical tool, then run your query in the DB2 command-line processor.
# 3  
Old 01-04-2016
You mean Table_name contains a "1"? Not too surprising (man awk):
Quote:
The function system(expr) uses /bin/sh to execute expr and returns the exit status of the command expr.
You might try "db2 -x \"SELECT TRIM... " | getline Table_name, given your db2 command yields some correct result.
This User Gave Thanks to RudiC For This Post:
# 4  
Old 01-05-2016
Hi RudiC,

The query works perfectly.

My intention to store the result of the query in a variable which i cant do it through system function.

Is it the problem with AWK ?

Regards,
Nantha.Y
# 5  
Old 01-05-2016
No problem with awk, it's the specified behaviour. Did you try the proposed alternative?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem running db2 commands in awk

Hi , I am trying to use the below in awk but failed, any one assist please, awk '{ print $0; if ( $0 ~ /LOADTMP1/ ) { print $4; Table_name=system($( db2 -x "SELECT TRIM(TD.BSCHEMA) || '.' || TRIM(TD.BNAME) AS TABLE_NAME FROM SYSCAT.TABDEP TD WHERE TD.BTYPE='T' AND... (2 Replies)
Discussion started by: Nandy
2 Replies

2. Shell Programming and Scripting

DB2 in awk or PARSING VALUE in Shell Script

Guys, My Motive is, There are some View names in the file with the format of SCHEMA.VIEWNAMe, I want to read the data from file and parse it to SCHEMA and VIEWNAME and execute DB2 command to get the base Table name. I can parse easily through AWK but i could not execute db2 commands in... (1 Reply)
Discussion started by: Nandy
1 Replies

3. Shell Programming and Scripting

awk file to read values from Db2 table replacing hard coded values

Hi, I want to replace a chain of if-else statement in an old AWK file with values from Db2 table or CSV file. The part of code is below... if (start_new_rec=="true"){ exclude_user="false"; user=toupper($6); match(user, "XXXXX."); if (RSTART ==2 ) { ... (9 Replies)
Discussion started by: asandy1234
9 Replies

4. Shell Programming and Scripting

Connect db2 using db2 connect

I want to connect to DB2 database which is mainframes using a unix script and run query and get the result set into the unix box. Is this acheivable using db2 connect? pls help me with a sample script which can perform the same. (1 Reply)
Discussion started by: midhun19
1 Replies

5. AIX

DB2 basics

Dear friends I am going to study DB2 and i dont have any experience with any DB's.. Please provide me with some links or pdf's for DB2 starters. any advice will be very usefull (2 Replies)
Discussion started by: Vit0_Corleone
2 Replies

6. Shell Programming and Scripting

db2

i want to run a db2 query though shell script but i want my where condition element to chnage everytime like class_nm='abc' next time class_nm='def' next time i want it to do by passing parameters to unix script can someone give example how can i do it (0 Replies)
Discussion started by: er_zeeshan05
0 Replies

7. Shell Programming and Scripting

need help with UNIX, awk and DB2

Hi i am working on a script which takes a parameter file as input . this parameter file is having SQL statements. I am fetching column names from the output of SQL file using following code: while read Record1 do SQLQuery=`echo $Record1 | awk '{printf $0 }'` ... (2 Replies)
Discussion started by: manmeet
2 Replies

8. Shell Programming and Scripting

MEM=`ps v $PPID| grep -i db2 | grep -v grep| awk '{ if ( $7 ~ " " ) { print 0 } else

Hi Guys, I need to set the value of $7 to zero in case $7 is NULL. I've tried the below command but doesn't work. Any ideas. thanks guys. MEM=`ps v $PPID| grep -i db2 | grep -v grep| awk '{ if ( $7 ~ " " ) { print 0 } else { print $7}}' ` Harby. (4 Replies)
Discussion started by: hariza
4 Replies

9. UNIX for Dummies Questions & Answers

Exception while loading DB2 driver Class.forName("com.ibm.db2.jcc.DB2Driver")

Hi... I m working on UNIX z/OS. Actually i have to pass the parameters from the JCL to java-db2 program thru PARM. I am able to pass the arguments but the problem occured is, it is throwing an exception while loading the db2 driver as 'Javaclassnotfound:com.ibm.db2.jcc.DB2Driver'... (0 Replies)
Discussion started by: Sujatha Gowda
0 Replies

10. AIX

DB2 Monitoring

Hi everyone. I am currently looking for a monitor to monitor my DB2 UDB database, which runs on AIX 5.3. Does anyone here use any good ones or has anyone heard of any good ones? I especially need to monitor which applications run at any given time, so that I am able to ballance the workload... (0 Replies)
Discussion started by: sprellari
0 Replies
Login or Register to Ask a Question