I want to add a security on a sensitive table when I delete lines with an SQL request on a DB2 table.
I want to mimic the way MySQL allows you to limit the numbers of rows deleted in an SQL request.
Basically I want to do this with DB2 :
DELETE FROM table WHERE info = '1' LIMIT 1
Is there a way to do that with DB2 ?
The RRN function is to AS400/iSeries/PowerSystem alone. In other environments there are other functions for the relative record number.
This makes it possible to erase a record of several identical even without UNIQUE key. It can also be used to update with minor changes.
works like the LIMIT but with DELETE and / or UPDATE.
It only works on SQL DB2 in other settings should be changed by RRN function to return the column number
It really depends on your platform.
If you're using DB2 on Linux/Unix/Windows, you can just create a select that gets the rows you want, and put that as a subquery for your delete, and DB2 will be able to delete the results of your select. Like so:
If you're on DB2 for z/OS, that syntax doesn't work, unfortunately. But, you can use your primary keys to do basically the same thing (this one also works on LUW):
Here is an example script that demonstrates how it's used: