| |
|
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.
|
 |

08-24-09, 09:34
|
|
Registered User
|
|
Join Date: Dec 2008
Location: Toronto, Canada
Posts: 381
|
|
|
sql question
|
|
I need to create a list of non-SYS tables with the number of rows in each table. How would I do it?
Basic statement to create a list of tables is easy:
db2 -x “select rtrim(tabschema)||’.’||rtrim(tabname) from syscat.tables where type=’T’ and substr(tabschema,1,3) not in (‘SYS’) order by 1”
but I need help with the rest. Thanks in advance
__________________
DB2 v9.5 ESE on AIX v6.1/ v9./10 on z/OS
|
|

08-24-09, 09:49
|
|
:-)
|
|
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
|
|
All you need is to add the CARD column to the select list. This assumes that the statistics are current.
|
|

08-24-09, 09:58
|
|
Registered User
|
|
Join Date: Nov 2005
Location: IL
Posts: 554
|
|
|
|
You might want to add Stats_time to see how old the stats are, which might help you identify how current your CARD count is.
What is the real purpose for this? Do you need an exact count?
__________________
--
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
DB2 v9.1.0.2 os 5.3.0.0
|
|

08-24-09, 10:04
|
|
Registered User
|
|
Join Date: Dec 2008
Location: Toronto, Canada
Posts: 381
|
|
Quote:
|
Originally Posted by Cougar8000
You might want to add Stats_time to see how old the stats are, which might help you identify how current your CARD count is.
What is the real purpose for this? Do you need an exact count?
|
yes, i need exact row count. I am doing ADMIN_COPY_SCHEMA and need to verify that row count in tables did not change after copy. So I guess count(*) is the only option. Need some kind of subselect but can't figure out how to do it. Thank you
__________________
DB2 v9.5 ESE on AIX v6.1/ v9./10 on z/OS
|
|

08-24-09, 11:26
|
|
Registered User
|
|
Join Date: Nov 2005
Location: IL
Posts: 554
|
|
If you need an EXACT count then doing count(*) is how you get it.
Can you use shell scripting? if yes, I have a simple, but brutal script that will do it for all of your tables.
BTW. youa re aware of this, right? A backup must be taken after calling the ADMIN_COPY_SCHEMA, otherwise the copied tables will be inaccessible following recovery.
__________________
--
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
DB2 v9.1.0.2 os 5.3.0.0
|
|

08-24-09, 11:28
|
|
Registered User
|
|
Join Date: Jul 2009
Location: NY
Posts: 886
|
|
You have to use Dynamic SQL for this.
I don't see any other solution.
Lenny
|
|

08-24-09, 11:32
|
|
Registered User
|
|
Join Date: Nov 2005
Location: IL
Posts: 554
|
|
Another thing that I saw before, but did not raised a question until now. You are going to be excluding all SYS schema tables. What about your user tables? Are you going to be moving those as well.
Are you going to be moving data for all user and prod schemas?
__________________
--
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
DB2 v9.1.0.2 os 5.3.0.0
|
|

08-24-09, 12:46
|
|
Registered User
|
|
Join Date: Dec 2008
Location: Toronto, Canada
Posts: 381
|
|
Quote:
|
Originally Posted by Cougar8000
Another thing that I saw before, but did not raised a question until now. You are going to be excluding all SYS schema tables. What about your user tables? Are you going to be moving those as well.
Are you going to be moving data for all user and prod schemas?
|
it's just that in this DB we have all tables under former DBA's name xxx. So I need to move all xxx. tables to DBA. tables (not touching SYS tables). Then I want to verify that not only all tables but ALL data were moved as well. This is our client's production DB so it is very important.
__________________
DB2 v9.5 ESE on AIX v6.1/ v9./10 on z/OS
|
|

08-24-09, 12:49
|
|
Registered User
|
|
Join Date: Dec 2008
Location: Toronto, Canada
Posts: 381
|
|
Quote:
|
Originally Posted by Cougar8000
If you need an EXACT count then doing count(*) is how you get it.
Can you use shell scripting? if yes, I have a simple, but brutal script that will do it for all of your tables.
BTW. youa re aware of this, right? A backup must be taken after calling the ADMIN_COPY_SCHEMA, otherwise the copied tables will be inaccessible following recovery.
|
I can use scripting, but I thought it would be better to use SQL. So yes, please provide 'simple and brutal' script. Thank in advance
P.S. the tables actually can be accessed even without a backup (I checked). I will have to do backup anyway because of the change.
__________________
DB2 v9.5 ESE on AIX v6.1/ v9./10 on z/OS
|
Last edited by MarkhamDBA; 08-24-09 at 12:58.
|

08-24-09, 12:53
|
|
Registered User
|
|
Join Date: Mar 2006
Location: Tuticorin, India
Posts: 100
|
|
Following may help you
db2 -x "select 'select ''' || ltrim(rtrim(tabschema))||','||ltrim(rtrim(tabname) ) || ', '' , count(*) from ' || ltrim(rtrim(tabschema))||'.'||ltrim(rtrim(tabname) ) || ' with ur ;' from syscat.tables where tbspace='USERSPACE1' and ( tabschema like 'CIG%' ) order by tabschema, tabname " > sib_count.sql
Execute the sib_count.sql as regular sql file to generate count for all tables. You may need to modify the where clause per your requirement.
|
|

08-24-09, 13:19
|
|
Registered User
|
|
Join Date: Nov 2005
Location: IL
Posts: 554
|
|
Quote:
|
Originally Posted by MarkhamDBA
it's just that in this DB we have all tables under former DBA's name xxx. So I need to move all xxx. tables to DBA. tables (not touching SYS tables). Then I want to verify that not only all tables but ALL data were moved as well. This is our client's production DB so it is very important.
|
How cool is that. People still haven't learned how to secure their environments
Here is the shell script. It will generate all the needed SQL and run it and pipe out results to a sorted file.
Code:
#!/bin/ksh
# This will generate a list of all tables
db2 -x "select trim(tabschema) || '.' || trim(tabname) from syscat.tables where tabschema not like '%SYS%'" and tabschema <> 'your_old_dba_schema'>> list
#This will do a small clean up
sed -e 's/ //g' list>clean_list
for i in `cat clean_list`
do
schema=$(echo $i | awk -F"." '{print $1}')
name=$(echo $i | awk -F"." '{print $2}')
#generate count
db2 "select '$schema.$name', count(*) from $schema.$name order by 1" >> count
done
__________________
--
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
DB2 v9.1.0.2 os 5.3.0.0
|
|

08-24-09, 14:15
|
|
Registered User
|
|
Join Date: Dec 2008
Location: Toronto, Canada
Posts: 381
|
|
Quote:
|
Originally Posted by db2udbgirl
Following may help you
db2 -x "select 'select ''' || ltrim(rtrim(tabschema))||','||ltrim(rtrim(tabname) ) || ', '' , count(*) from ' || ltrim(rtrim(tabschema))||'.'||ltrim(rtrim(tabname) ) || ' with ur ;' from syscat.tables where tbspace='USERSPACE1' and ( tabschema like 'CIG%' ) order by tabschema, tabname " > sib_count.sql
Execute the sib_count.sql as regular sql file to generate count for all tables. You may need to modify the where clause per your requirement.
|
it works! thanks so much
__________________
DB2 v9.5 ESE on AIX v6.1/ v9./10 on z/OS
|
|

08-24-09, 14:21
|
|
Registered User
|
|
Join Date: Dec 2008
Location: Toronto, Canada
Posts: 381
|
|
Quote:
|
Originally Posted by Cougar8000
How cool is that. People still haven't learned how to secure their environments
Here is the shell script. It will generate all the needed SQL and run it and pipe out results to a sorted file.
Code:
#!/bin/ksh
# This will generate a list of all tables
db2 -x "select trim(tabschema) || '.' || trim(tabname) from syscat.tables where tabschema not like '%SYS%'" and tabschema <> 'your_old_dba_schema'>> list
#This will do a small clean up
sed -e 's/ //g' list>clean_list
for i in `cat clean_list`
do
schema=$(echo $i | awk -F"." '{print $1}')
name=$(echo $i | awk -F"." '{print $2}')
#generate count
db2 "select '$schema.$name', count(*) from $schema.$name order by 1" >> count
done
|
thanks for the script. What do you mean by 'People still haven't learned how to secure their environments'? 
__________________
DB2 v9.5 ESE on AIX v6.1/ v9./10 on z/OS
|
|

08-24-09, 14:26
|
|
Registered User
|
|
Join Date: Nov 2005
Location: IL
Posts: 554
|
|
Quote:
|
Originally Posted by MarkhamDBA
thanks for the script. What do you mean by 'People still haven't learned how to secure their environments'? 
|
Statement is generally directed at management. Sounds like you have had a DBA that build production objects in his own schema. Now he is not there and you are scrambling. That is a major flow.
__________________
--
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
DB2 v9.1.0.2 os 5.3.0.0
|
|

08-24-09, 14:43
|
|
Registered User
|
|
Join Date: Jul 2009
Location: NY
Posts: 886
|
|
Quote:
|
Originally Posted by db2udbgirl
Following may help you
db2 -x "select 'select ''' || ltrim(rtrim(tabschema))||','||ltrim(rtrim(tabname) ) || ', '' , count(*) from ' || ltrim(rtrim(tabschema))||'.'||ltrim(rtrim(tabname) ) || ' with ur ;' from syscat.tables where tbspace='USERSPACE1' and ( tabschema like 'CIG%' ) order by tabschema, tabname " > sib_count.sql
Execute the sib_count.sql as regular sql file to generate count for all tables. You may need to modify the where clause per your requirement.
|
It's interesting !
What count you count(*) ?
Why you use ltrim(rtrim(...)), but not STRIP(...) function ?
Thanks
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|