简体   繁体   English

在DB2 / 400 SQL查询中动态引用表名。

[英]Refer to table name dynamically in DB2/400 SQL query..?

I'd like to run a query that gets a combination of live data and metadata for a regularly changing list of table names. 我想运行一个查询,该查询获取实时数据和元数据的组合,以获取定期更改的表名列表。 This is for research & analysis on a large server with 100+ schemas and thousands of tables/views in each. 这是用于在大型服务器上进行的研究和分析,该服务器具有100多个模式,每个模式中都有成千上万个表/视图。 I need help referring to table names dynamically, which I do understand is NOT possible. 我需要动态引用表名的帮助,据我了解这是不可能的。 However... 然而...

My googling indicates the solution may be an SQL statement in a text variable, which is then executed by the EXEC statement. 我的Google搜索表明解决方案可能是文本变量中的SQL语句,然后由EXEC语句执行。 But this is DB2/400 v7r3, which is a complex thing (as is its SQL reference on the IBM web site), and I'm having difficulty creating correct syntax. 但这是DB2 / 400 v7r3,这很复杂(就像IBM网站上的SQL参考一样),并且我很难创建正确的语法。

Here is a basic example of what I wish to do, but of course it does not work: 这是我想要做的一个基本示例,但是当然不起作用:

SELECT        TABLE_NAME,
              TABLE_TEXT,
              ( SELECT COUNT(*) FROM TABLE_NAME ) AS ROW_COUNT
              -- above line of course does not work
FROM          QSYS2.SYSTABLES
WHERE         TABLE_SCHEMA = 'ABCDEFGH'
AND           TABLE_NAME IN ('ARCMNTST', 'ARIMSGT', 'ARTENT', 'DAILYHT', 'ETC')

I understand I need something like the following, but I cannot figure out the correct statements: 我了解我需要以下内容,但我无法找出正确的语句:

DECLARE       @sqltext AS VARCHAR(128)
SELECT        TABLE_NAME,
              TABLE_TEXT,
              ( SET @sqltext = 'SELECT COUNT(*) FROM ABCDEFGH.' || TABLE_NAME
                EXEC sqltest ) AS ROW_COUNT --this is probably wrong
FROM          QSYS2.SYSTABLES
WHERE         TABLE_SCHEMA = 'ABCDEFGH'
AND           TABLE_NAME IN ('ARCMNTST', 'ARIMSGT', 'ARTENT', 'ETC', 'ETC', 'ETC')
ORDER BY      TABLE_NAME

Dynamic SQL isn't difficult... 动态SQL并不难...

Basically, build your SQL statement into your string variable. 基本上,将SQL语句构建到字符串变量中。 Using CONCAT to include values from other 使用CONCAT包含其他值

@sqlStmt = 'Insert into mytable values (''ConstVal'',' concat SomeVar concat ')';

execute immediate @sqlStmt;

The catch is that you have to escape strings, like 'ConstVal' above with double single quotes. 要注意的是,您必须对字符串进行转义,例如上面的'ConstVal'用双单引号引起来。

The other catch is that you can't use SELECT like you are trying to. 另一个问题是您不能像尝试那样使用SELECT If you only had one row to return, SELECT INTO would be an option for a static statement. 如果只有一行要返回,则SELECT INTO将是静态语句的选项。 But isn't supported for dynamic. 但是不支持动态。 You have to use a dynamic VALUES INTO instead. 您必须改为使用动态VALUES INTO

However, it appears you want to get back multiple rows. 但是,您似乎想要返回多行。 In which case, you need to use a cursor. 在这种情况下,您需要使用游标。 Unfortunately, dynamic cursors are a bit more complex as you have to use the SQL Descriptor. 不幸的是,动态游标要复杂一些,因为您必须使用SQL描述符。

declare myCursor cursor for myStatement;

set @sqlStmt = 'select ....';
prepare myStatement into mySqlDescriptor from @SqlStmt;

open myCursor;
// done if you are returning the results
// assuming you want to process in your procedure..
// add a loop that does 
//   fetch next from myCursor into myData;

Having said all that, you don't need any of it to get a row count for a table...the syspartitionstat catalog view already has that info. 说了这么多,您不需要任何信息就可以获取表的行数... syspartitionstat目录视图已经具有该信息。

select table_name, number_rows 
from syspartitionstat
where table_schema = 'ABCDEFGH' 
      and TABLE_NAME in ('ARCMNTST', 'ARIMSGT', 'ARTENT', 'ETC', 'ETC', 'ETC');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM