DB2存储过程返回太多行

时间:2015-07-09 19:20:21

标签: stored-procedures db2

我有一个非常简单的存储过程,可以根据我的架构和表参数查找特定架构中的表。

问题是存储过程返回表的所有行。

  • 忽略我的第二个参数(table_name)
  • 我可以拼错table_name参数,并且该过程返回所有行。
  • 如果我拼错了架构参数,它将不会返回任何行。

有什么想法吗?提前致谢。存储过程如下。

CREATE OR REPLACE PROCEDURE FINDTABLECOLUMNS 
(IN SCHEMA VARCHAR(25), IN TABLE_NAME VARCHAR(25))
LANGUAGE SQL
READS SQL DATA
RESULT SETS 1

BEGIN

  DECLARE COLUMN_TABLE_LIST CURSOR WITH RETURN TO CALLER FOR
    SELECT
        ROW_NUMBER ( ) OVER ( ORDER BY TABLE_NAME , ORDINAL_POSITION ) AS # 
       ,TABLE_NAME
       ,COLUMN_NAME
       ,SYSTEM_COLUMN_NAME
       ,DATA_TYPE
       ,LENGTH
       ,COLUMN_TEXT
  FROM QSYS2 . SYSCOLUMNS
  WHERE TABLE_SCHEMA = UPPER ( SCHEMA ) AND TABLE_NAME = UPPER ( TABLE_NAME )
  FOR READ ONLY ;

  OPEN COLUMN_TABLE_LIST ;
END 

1 个答案:

答案 0 :(得分:2)

我认为这是因为columnname和parametername是相同的。你可以解决它:

CREATE OR REPLACE PROCEDURE FINDTABLECOLUMNS 
(IN SCHEMA VARCHAR(25), IN TABLE_NAME_PAR VARCHAR(25))
LANGUAGE SQL
READS SQL DATA
RESULT SETS 1
BEGIN
DECLARE COLUMN_TABLE_LIST CURSOR WITH RETURN TO CALLER FOR
SELECT
    ROW_NUMBER ( ) OVER ( ORDER BY TABLE_NAME , ORDINAL_POSITION ) AS # 
   ,TABLE_NAME
   ,COLUMN_NAME
   ,SYSTEM_COLUMN_NAME
   ,DATA_TYPE
   ,LENGTH
   ,COLUMN_TEXT
FROM QSYS2 . SYSCOLUMNS
WHERE TABLE_SCHEMA = UPPER ( SCHEMA ) AND TABLE_NAME = UPPER ( TABLE_NAME_PAR )
FOR READ ONLY ;
OPEN COLUMN_TABLE_LIST ;
END