将过程从 Oracle 转换为 DB2,尝试在运行时更新 table 时出错

Convert Procedure from Oracle to DB2, Error when try to update table on runtime

您好 DB2 专家我需要您的帮助将以下过程转换为更动态的过程。 我们必须为每个 table 更新多个带有 id 列最大值的序列。

CREATE PROCEDURE mySchema.UPDATE_SEQUENCE ( )
 DYNAMIC RESULT SETS 1
 MODIFIES SQL DATA
----------------------------------------------------------------------
-- SQL Stored Procedure
----------------------------------------------------------------------
P1: BEGIN
 DECLARE counter BIGINT;
 DECLARE q VARCHAR(500);
set (counter) = (select max(N_PRI_KEY) from mySchema.myTable);
set q = 'alter sequence mySchema.mySequence RESTART WITH ' || counter;
 EXECUTE IMMEDIATE q;

END P1
@

这是我根据上面的代码写的: 在这里,我希望 N_PRI_KEY 是动态的,并且 mySchema.myTable 在 运行 时间使用 table 中的值进行更新。

CREATE OR REPLACE PROCEDURE getText ()
LANGUAGE SQL
DYNAMIC RESULT SETS 1
  BEGIN
  DECLARE maxval  INTEGER DEFAULT 0;

  CALL DBMS_OUTPUT.PUT( 'a' );

  FOR vrows AS 
      SELECT NAME, SEQUENCENAME, TBNAME FROM MAXSEQUENCE WHERE SEQUENCENAME='ASSETSEQ'
    DO 

      SELECT MAX(vrows.NAME) INTO maxval FROM vrow.TBNAME; -- This is where I am getting error.

    EXECUTE IMMEDIATE 'ALTER SEQUENCE '||vrows.SEQUENCENAME||' RESTART WITH '|| maxval;

  END FOR;      
END@

这是我在尝试创建过程时遇到的错误。

DB21034E  The command was processed as an SQL statement because it was not a
valid Command Line Processor command.  During SQL processing it returned:
SQL0204N  "VROW.TBNAME" is an undefined name.  LINE NUMBER=18.  SQLSTATE=42704

当我 运行 这一行时,它起作用并在 TEMPOUTPUT table.

中插入最大值
execute immediate  'INSERT INTO TEMPOUTPUT VALUES (select max('||vrow.NAME||') from '||vrow.TBNAME||')';

我试过这样做但是没有用。

execute immediate  'ALTER SEQUENCE '||SEQUENCENAME||' RESTART WITH select max('||vrow.NAME||') from '||vrow.TBNAME;

仅供参考 - 这是一个用 Oralce 编写的程序,它正在做类似的事情。


declare
    maxval int;
    seqval int;
  begin
  for i in ( select ucc.column_name, s.sequence_name, uc.table_name
             from   user_cons_columns ucc,
                    user_constraints uc,
                    user_sequences s
             where  uc.constraint_name = ucc.constraint_name
             and    uc.constraint_type = 'P'
             and    ucc.position = 1
             and    s.sequence_name = 'SEQ_'||uc.table_name
           )
  loop
    execute immediate  'select max('||i.column_name||') from '||i.table_name into maxval;
    execute immediate 'select '||i.sequence_name||'.nextval from dual' into seqval;

    dbms_output.put_line(maxval||','||seqval);

     if maxval > seqval then
        execute immediate  'alter sequence '||i.sequence_name||' increment by '|| ( maxval - seqval );
        execute immediate 'select '||i.sequence_name||'.nextval from dual' into seqval;
        execute immediate  'alter sequence '||i.sequence_name||' increment by 1';
        execute immediate 'select '||i.sequence_name||'.nextval from dual' into seqval;
        dbms_output.put_line(maxval||','||seqval);
     end if;
  end loop;
  end;

尝试以下操作,而不是出现错误的 SELECT MAX(...) 行:

PREPARE S1 FROM 'SET ? = (SELECT MAX(' || vrows.NAME || ') FROM ' || vrows.TBNAME || ')';
EXECUTE S1 INTO maxval;