动态SQL并不难。。。
基本上,将SQL语句构建到字符串变量中。使用
CONCAT
@sqlStmt = 'Insert into mytable values (''ConstVal'',' concat SomeVar concat ')';
execute immediate @sqlStmt;
问题是必须对字符串进行转义,比如上面的“ConstVal”带有双引号。
另一个问题是你不能使用
SELECT
就像你想做的那样。如果你只有一行要返回,
SELECT INTO
VALUES INTO
相反。
但是,似乎您想要返回多行。在这种情况下,需要使用光标。不幸的是,动态游标有点复杂,因为您必须使用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;
说了这么多,您不需要任何信息来获取表的行计数…syspartitionstat目录视图已经有了这些信息。
select table_name, number_rows
from syspartitionstat
where table_schema = 'ABCDEFGH'
and TABLE_NAME in ('ARCMNTST', 'ARIMSGT', 'ARTENT', 'ETC', 'ETC', 'ETC');