仅获取第一个“可变”行

时间:2017-06-07 04:19:42

标签: db2

可以将值传递给fetch first语法吗?

        SET inputtotalCount = inputcount - inputintSimilarityLevel;
        IF inputtotalCount > 0 THEN
            BEGIN
                DELETE FROM EUSYSUPW WHERE UPRECID IN (
                SELECT UPRECID FROM EUSYSUPW
                WHERE UPUSRID = inputSACUSRID
                ORDER BY UPMODDT ASC
                FETCH FIRST inputtotalCount ROWS ONLY);
            END;
        END IF;

2 个答案:

答案 0 :(得分:2)

您好,您不能在获取第一行时使用参数,但是您可以创建rownumber并删除行num< =参数的行。我没有您的数据所以我添加了一些随机整数。

begin atomic
declare   inputtotalCount int;
declare          inputcount  int;
declare          inputintSimilarityLevel int;
declare          inputSACUSRID int;
set inputcount=9;
set inputintSimilarityLevel=2;
SET inputtotalCount = inputcount - inputintSimilarityLevel;
set inputSACUSRID=5;
    SET inputtotalCount = 7;

    IF inputtotalCount > 0 THEN

           DELETE  from (
            SELECT e.*,row_number() over (PARTITION BY UPUSRID order by UPMODDT  ) as rownum
            FROM work.EUSYSUPW e
            WHERE UPUSRID = inputSACUSRID) as a
            where rownum<=inputtotalCount;


    END IF;
end 

第二个选项是创建动态sql

答案 1 :(得分:1)

您也可以使用全局变量:

--Declare a global variable
CREATE OR REPLACE VARIABLE MyVar INTEGER DEFAULT 15;

--Modify value if necessary (you can do a calcul too)
SET MyVar=27;

--Delete in function of global variable
DELETE FROM EUSYSUPW f1
WHERE exists 
(
  select * from 
  (
       select f2.UPRECID, rownumber() over(order by f2.UPMODDT ASC) rang from EUSYSUPW f2
  ) f3 
  where f1.UPRECID=f3.UPRECID and f3.rang=MyVar 

);