当我在DB2表上删除带有SQL请求的行时,我想在敏感表上添加安全性。
我想模仿MySQL允许你限制SQL请求中删除的行数的方式。
基本上我想用DB2做这个:
DELETE FROM table WHERE info = '1' LIMIT 1
有没有办法用DB2做到这一点?
答案 0 :(得分:9)
delete from table where id in (select id from table where info = '1' order by id fetch first 1 rows only)
答案 1 :(得分:6)
这实际上取决于您的平台。
如果你在Linux / Unix / Windows上使用DB2,你可以创建一个获取所需行的select,并将其作为删除的子查询,DB2将能够删除结果你的选择。像这样:
DELETE FROM (
SELECT 1
FROM table
WHERE info = '1'
ORDER BY your_key_columns
FETCH FIRST ROW ONLY
) AS A
;
如果您使用的是DB2 for z / OS,那么该语法不可行。但是,你可以使用你的主键基本上做同样的事情(这个也适用于LUW):
DELETE FROM table
WHERE (info, key2) IN (
SELECT info, key2
FROM table
WHERE info = 1
ORDER BY key2
FETCH FIRST ROW ONLY
);
以下是演示如何使用的示例脚本:
DECLARE GLOBAL TEMPORARY TABLE SESSION.TEST(
ID INT
,RN INT
) ON COMMIT PRESERVE ROWS;
INSERT INTO SESSION.TEST
SELECT 1,1 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 1,2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 1,3 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 1,4 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 1,5 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 2,1 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 2,2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 2,3 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 3,1 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 3,2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 3,3 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 4,1 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 4,2 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 5,1 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 6,1 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 7,1 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 8,1 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 9,1 FROM SYSIBM.SYSDUMMY1 UNION ALL
SELECT 10,1 FROM SYSIBM.SYSDUMMY1
;
SELECT * FROM SESSION.TEST ORDER BY ID, RN;
-- LUW Version
DELETE FROM (
SELECT 1
FROM SESSION.TEST
WHERE ID = 1
ORDER BY RN
FETCH FIRST ROW ONLY
) AS A
;
--Mainframe version
DELETE FROM SESSION.TEST
WHERE (ID, RN) IN (
SELECT ID, RN
FROM SESSION.TEST
WHERE ID = 1
ORDER BY RN
FETCH FIRST ROW ONLY
);
SELECT * FROM SESSION.TEST ORDER BY ID, RN;
DROP TABLE SESSION.TEST;
答案 2 :(得分:1)
在IBMi DB2上:
DELETE FROM table WHERE RRN(table) in
(SELECT RRN(table) FROM table WHERE col1 = '1' AND col2 = '2' FETCH FIRST 5 ROWS ONLY)
答案 3 :(得分:1)
如果您的主键有多个值,或者您只需要多个值作为条件,则这是有效的查询:
DELETE FROM TABLE
WHERE (COLUMN1, COLUMN2) IN (
SELECT COLUMN1, COLUMN2 FROM TABLE
WHERE SOME_COLUMN='THIS'
AND SOME_OTHER_COLUMN LIKE 'THAT%'
FETCH FIRST 10 ROWS ONLY)
答案 4 :(得分:0)
这个查询怎么样?
delete from table D where exists
( select * from ( select * from table M fetch first 10 rows only ) as M
where M.object_id = D.object_id )
答案 5 :(得分:0)
MERGE INTO XYZ A<BR>
USING (<BR>
SELECT RID_BIT(B) CHAVE<BR>
FROM XYZ B<BR>
FETCH FIRST 100000 ROWS ONLY) B<BR>
ON RID_BIT(A) = B.CHAVE<BR>
WHEN MATCHED THEN DELETE;
答案 6 :(得分:-1)
只需选择一个语句,然后将该语句放在删除查询中:
delete from (
select from table WHERE info = '1' order by id fetch first 25000 rows only
)
答案 7 :(得分:-1)
DELETE
FROM Bibl/File
WHERE RRN(File) = (
SELECT min(RRN(File))
FROM Bibl/File
WHERE Fld1 = 'xx'
)
RRN功能仅适用于AS400 / iSeries / PowerSystem。在其他环境中,相对记录号还有其他功能。
这使得即使没有UNIQUE键也可以擦除几个相同的记录。它也可以用来进行微小的更改。
的作用类似于LIMIT,但是使用DELETE和/或UPDATE。
它只适用于SQL DB2,其他设置应该由RRN函数更改以返回列号
答案 8 :(得分:-3)
DELETE FROM table
WHERE info = '1'
FETCH FIRST 1 ROWS ONLY