473,508 Members | 2,303 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SQL Dynamic Table Count Select Assistance

Can't seem to figure out how to do this and have been reading for some
time now......

I want to select a row count from a table name in SYSTABLES.

This statement does not return what I needed, but can help explain what
I'm looking for. I want the results to be the Row Count from a Table
Name out of the Sub-Select.

Select Count(*) from
(Select SYSTEM_TABLE_NAME where SYSTEM_TABLE_NAME like ('MYTABLES%)
and
SYSTEM_TABLE_SCHEMA = 'MYSCHEMA')

Any help would be appreciated.

Tx

Chris

Oct 17 '06 #1
4 7177
Chris wrote:
Can't seem to figure out how to do this and have been reading for some
time now......

I want to select a row count from a table name in SYSTABLES.
Don't use the SYSIBM.SYS* tables. They are not documented and, thus, not
supported.
This statement does not return what I needed, but can help explain what
I'm looking for. I want the results to be the Row Count from a Table
Name out of the Sub-Select.

Select Count(*) from
(Select SYSTEM_TABLE_NAME where SYSTEM_TABLE_NAME like ('MYTABLES%)
and
SYSTEM_TABLE_SCHEMA = 'MYSCHEMA')

Any help would be appreciated.
I don't quite understand what you want to do. Do you want to get the number
of tuples of all tables starting with the name MYTABLES in schema MYSCHEMA?
If so, your above query won't work, of course. The above query (even if it
would be syntactically correct and queries SYSCAT.TABLES), scans the
SYSCAT.TABLES view, filters out all rows that satisfy your search criteria
and then counts how many rows that were.

What you could do if you want to get the cardinality of the tables is to
query the CARD column in SYSCAT.TABLES:

SELECT tabname, card
FROM syscat.tables
WHERE tabname LIKE 'MYTABLES%' AND
tabschema = 'MYSCHEMA'

Note that CARD is population when you collect statistics on the respective
table. Thus, the value is not guaranteed to be correct if you didn't
collect statistics or if the stats are out of date.

If you want to count the rows in the tables (not relying on the statistics),
you will have to query those tables directly. For that, you can first scan
SYSCAT.TABLES to identify the qualifying tables, then construct a query
against each of the tables and execute those queries with dynamic SQL.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Oct 17 '06 #2
If you're looking for list of all tables in a schema and their
cardinalities, you'll probably need to do this task within an SP. While
you're able to declare a cursor in inline SQL PL, you can't issue an
EXECUTE IMMEDIATE (for the dynamic SQL), nor would you be able to see
the result set (probably a bigger show-stopper :-).

What I'd do is declare an SP with DYNAMIC RESULT SETS 1, declare a
global temporary table (DGTT) with two columns--table name and count,
create a cursor against SYSCAT.TABLES, then loop through this table
list and execute a dynamic SQL query that does a SELECT COUNT(*)
against the table the cursor is pointing to. I'd then insert the table
name and count into the temp table. When the loop is finished, the last
thing I'd do is SELECT * from the DGTT.

--Jeff

Chris wrote:
Can't seem to figure out how to do this and have been reading for some
time now......

I want to select a row count from a table name in SYSTABLES.

This statement does not return what I needed, but can help explain what
I'm looking for. I want the results to be the Row Count from a Table
Name out of the Sub-Select.

Select Count(*) from
(Select SYSTEM_TABLE_NAME where SYSTEM_TABLE_NAME like ('MYTABLES%)
and
SYSTEM_TABLE_SCHEMA = 'MYSCHEMA')

Any help would be appreciated.

Tx

Chris
Oct 17 '06 #3
Tx, I figued I needed a SP.

Chris

Oct 17 '06 #4
DB2 only keep statistical information for CARDINALITY in SYSSTAT.TABLES.
If you want an accurate count you will need to query the table itself.

Cheers
Serge

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

IOD Conference
http://www.ibm.com/software/data/ond...ness/conf2006/
Oct 18 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1539
by: dave | last post by:
I am trying to run 3 dynamic selects from stored proc, really only the table name is dynamic.. Anway I'm kinda lost on how I can accomplish this.. this is what I have but it only returns the first...
31
3643
by: NickName | last post by:
/* goal: dynamic evaluation of table row platform: sql 2000 */ use northwind; declare @tbl sysname set @tbl = 'customers' EXEC('select count(*) from ' +@tbl)
5
8277
by: Terri | last post by:
I have a form with a multi-select combo. I dynamically build a SELECT statement, open a report, and set the recordsource to my dynamic SELECT statement. I count the records returned in the report...
1
17630
by: Nathan Bloomfield | last post by:
Does anyone know if there is any documentation which relates to Access2k + ? or can anyone help adjust the code? I am having trouble converting the DAO references. TITLE :INF: How to...
6
2618
by: hb | last post by:
Hi, I have a page bill.aspx and its code-behind bill.aspx.cs. On bill.aspx I have: === Select a month: <asp:dropdownlist runat="server" id="lstDate" autopostback="True" /> <br> <asp:table...
1
4817
by: russ | last post by:
Hi all, Here's a problem I'm having with a dynamic table. Following the guidelines here (http://www.codeproject.com/aspnet/dynamiccontrols.asp), which make perfect sense. The problem is that...
3
6027
by: Cindy | last post by:
I'm trying to use the NEWID function in dynamic SQL and get an error message Incorrect syntax near the keyword 'ORDER'. Looks like I can't do an insert with an Order by clause. Here's the code:...
0
2707
by: JamesOo | last post by:
I have the code below, but I need to make it searchable in query table, below code only allowed seach the table which in show mdb only. (i.e. have 3 table, but only can search either one only,...
6
4260
by: shashi shekhar singh | last post by:
Respected Sir, I have to create multiple dynamic dropdownlist boxes and add items dynamically in <asp:table> server control but problem occurs , i.e. except of fist dropdown list no dropdownlist...
0
7224
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7120
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7323
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7039
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7494
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5626
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4706
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.