If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Hard time getting this script to work. Help pls

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-09-08, 20:09
zulu zulu is offline
Registered User
 
Join Date: Oct 2007
Posts: 6
Hard time getting this script to work. Help pls

My goal is to get a list of tables from the SYSPROC.REORGCHK_TB_STATS. I will then pass the resulting tables as an input into a second select query

First I tried this:

TRY 1
---------

#!/bin/ksh
. /home/db2inst1/sqllib/db2profile
db2 connect to tecdb
db2 -x "CALL SYSPROC.REORGCHK_TB_STATS('T', 'ALL')" | grep '*' | awk '{print $2 }'|while read TABLES
do
db2 "select substr(A.tabschema,1,10)schema,substr(A.tabname,1, 50)tabname,A.npages,substr(B.tbspace,1,30)tablespa
ce,B.pagesize,C.indextype from syscat.tables A,syscat.tablespaces B,syscat.indexes C where A.tabname=$TABLES and
C.indextype='CLUS' and A.tbspaceid = B.tbspaceid and A.tabname = C.tabname order by 3 desc"
done
db2 connect reset
-----------------------------------------------------
This is the error I get: SQL0206N "<tablename>" is not valid in the context where it is used-----------------------------------------------------
After several unsuccessfull attempts I gave up. Probably the logic is not right.



TRY 2
----------

I then tried to generate a select statement for each of the tables in the tables list initially generated. Here too, I can't seem to get the syntax right. Either I get a unmatched '( or nothing is returned. You can tell I am not good at scripting. Just trying very hard to learn. Any help will be greatly appreciated.
Thanks
- Rosemary.



#!/bin/ksh
. /home/db2inst1/sqllib/db2profile

db2 -x "CALL SYSPROC.REORGCHK_TB_STATS('T', 'ALL')" | grep '*' |tee sysreorg.seed

cat sysreorg.seed|awk '{print "db2 select A"".""schema,"" ""A"."tabname,"" ""A"."npages,"" ""B"."pagesize,""""C"
."indextype from syscat"".""tables A,""""syscat"".""tablespaces B,""""syscat"."indexes C"" ""where A"."tabname""
" "=" \'$1\' """ "and C"."indextype""=\'CLUS\' " "" "and A"."tbspaceid" "=" "B"".""tbspaceid and A"".""tabname"
"=" "C"".""tabname order by 3 desc"}'


--- either the logic is not good or the "" dont match and I cannot fix it. I have spent like 4hrs on this.
I will appreciate any other way to achieve my goal.

Thanks again.
Reply With Quote
  #2 (permalink)  
Old 10-10-08, 02:05
Tyveleyn Tyveleyn is offline
Registered User
 
Join Date: Aug 2006
Location: The Netherlands
Posts: 248
Hi, I'm not realy familiar with DB2 but I see some things that might go wrong. First try your first attempt preceding with this assignment
Code:
#!/bin/ksh
. /home/db2inst1/sqllib/db2profile
export TABLES
db2 connect to tecdb
...
If that doesn't work redesign the AWK command of your second try like this:
Code:
awk '{
	printf("%s \"%s %s %s %s%s.%s%s %s %s%s%s %s %s;\"\n",
		"db2", "select A.schema, A.tabname, A.npages, B.pagesize,",
		"C.indextype from syscat.tables A, syscat.tablespaces B,",
		"syscat.indexes C where A.tabname =", quote, $1, $2, quote,
		"and C.indextype =", quote, "CLUS", quote,
		"and A.tbspaceid = B.tbspaceid",
		"and A.tabname = C.tabname order by 3 desc")
	}' quote="'" `db2 -x "CALL SYSPROC.REORGCHK_TB_STATS('T', 'ALL')" | grep '*'`
The single quote can't be used within the AWK code because the interpreter regards that as source terminator. That's why it's done here in a roundabout way by assigning the ' to a variable (quote) before the value of the db2 command is read. Printf is the formatting version of print and gives much more flexibility as you can see, it's the equivalent of the C printf function. If you're not acquainted with it: the first argument is the formatstring, which consists of placeholders for stringvariables (%s) and spaces only. The single dot in it is the dot between the table_schema and table_name. The other arguments are the subsequent literals/variables ($1 and $2 refer to the first and second field of the input record) that are substituted by the placeholders. See the man page for further information.

If that's printing the correct SQL statements (don't know what kind of UNIX system and therefore what kind of AWK you're using) complete the AWK program in a way it will execute the SQL statements as well. You can do that like:
Code:
awk '{	stmnt = sprintf("%s \"%s %s %s %s%s.%s%s %s %s%s%s %s %s;\",
		...
	system(stmnt)
	}' quote="'" ...
With this you'll store the dynamicly build string in variable stmnt and finally execute that statement as a system command.

Good luck,
Hans
Reply With Quote
  #3 (permalink)  
Old 10-10-08, 15:06
zulu zulu is offline
Registered User
 
Join Date: Oct 2007
Posts: 6
Hans,

Thanks a million. I was able to edit it along the lines of what you gave me and it worked.

Rosemary
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On