这样的?
测试用例:
SQL> create table mac
2 (state number,
3 rpd varchar2(2),
4 mac_address varchar2(20) constraint uk_mac unique,
5 total_count number
6 );
Table created.
SQL> insert into mac
2 select 26, 'aa', 'aa:bb:12:cc:ab:aa', 1 from dual union
3 select 26, 'bb', 'aa:bb:12:cc:ab:ab', 1 from dual union
4 select 26, 'cc', 'aa:bb:12:cc:ab:ac', 1 from dual;
3 rows created.
一个过程:我将整行选择为一个局部变量。正如您可能得到的NO_DATA_FOUND,我正在处理它(通过nothing)。如果匹配,我将同一 MAC 地址行的 TOTAL_COUNT 列值减去 1 并插入整个新行。
但是,这个模型似乎支持重复的 MAC 地址。那合法吗?在这种情况下你想做什么?例如,如果您再次输入相同的 MAC 地址? SELECT 将返回应处理的 TOO_MANY_ROWS。一种选择是只更新一个(哪一个?)行;还是...?
SQL> create or replace procedure p_mac
2 (par_state in number, par_rpd in varchar2, par_mac_address in varchar2)
3 is
4 l_row mac%rowtype;
5 begin
6 select *
7 into l_row
8 from mac
9 where mac_address = par_mac_address;
10
11 update mac set
12 total_count = total_count - 1
13 where mac_address = par_mac_address;
14
15 insert into mac (state, rpd, mac_address, total_count)
16 values
17 (par_state, par_rpd, par_mac_address, 1);
18 exception
19 when no_data_found then null;
20 end;
21 /
Procedure created.
测试:
SQL> select * from mac;
STATE RP MAC_ADDRESS TOTAL_COUNT
---------- -- -------------------- -----------
26 aa aa:bb:12:cc:ab:aa 1
26 bb aa:bb:12:cc:ab:ab 1
26 cc aa:bb:12:cc:ab:ac 1
SQL> exec p_mac(26, 'ab', 'aa:bb:12:cc:ab:ac');
PL/SQL procedure successfully completed.
SQL> select * From mac;
STATE RP MAC_ADDRESS TOTAL_COUNT
---------- -- -------------------- -----------
26 aa aa:bb:12:cc:ab:aa 1
26 bb aa:bb:12:cc:ab:ab 1
26 cc aa:bb:12:cc:ab:ac 0
26 ab aa:bb:12:cc:ab:ac 1
SQL>