请帮助我理解以下Oracle游标代码。我在get_geog= geography
部分特别困惑。 geography
的价值是多少?是否采用了new.geography的价值?另外,c2b_rec.geolevel_id >= c2a_rec.geolevel_id
c2b_rec和c2a_rec之间有什么区别?
CREATE OR REPLACE TRIGGER tr_name
BEFORE DELETE OR INSERT OR UPDATE OF geography,
ON tbl_name
FOR EACH ROW
DECLARE
CURSOR c2(get_geog IN VARCHAR2, get_geolevel IN VARCHAR2) IS
SELECT *
FROM geolevels
WHERE get_geog = geography
AND get_geolevel = geolevel;
c2a_rec c2%ROWTYPE;
c2b_rec c2%ROWTYPE;
IF c2b_rec.geolevel_id >= c2a_rec.geolevel_id THEN
some code
答案 0 :(得分:0)
这部分:
CURSOR c2(get_geog IN VARCHAR2, get_geolevel IN VARCHAR2)
IS
SELECT *
FROM geolevels
WHERE get_geog = geography
AND get_geolevel = geolevel;
表示您声明一个基于其SELECT
语句的结果集的新游标。由于您没有向我们展示其他变量,因此我可以得出结论geography
和geolevel
是geolevels
表的字段。 get_geog
和get_geolevel
是光标的参数。
这一行:
c2a_rec c2%ROWTYPE;
c2b_rec c2%ROWTYPE;
声明两个可用于从游标中获取数据的变量。
在此之前:
IF c2b_rec.geolevel_id >= c2a_rec.geolevel_id THEN
你必须有一个打开和取出光标的部分。像这样:
OPEN c2('one', 'two');
fetch c2 into c2a_rec;
fetch c2 into c2b_rec;
获取游标时,从select语句返回的结果集中提取行。因此c2a_rec
变量将具有结果集的第一行,c2b_rec
变量将具有结果集的第二行。然后,将游标的geolevel_id
字段的值与此行进行比较:
IF c2b_rec.geolevel_id >= c2a_rec.geolevel_id THEN
因此,如果您不知道如何使用参数化游标,请查看以下内容:
create table t1 (
id number(10)
);
create table t2 (
id number(10)
);
create OR REPLACE trigger t1_trg
after insert
on t1
for each row
declare
cursor c1 (p_id NUMBER)
is
select id
from t2
where id = p_id
;
c1_row c1%ROWTYPE;
begin
open c1(1);
fetch c1 into c1_row;
IF c1%FOUND
THEN
DBMS_OUTPUT.put_line(c1_row.id);
ELSE
DBMS_OUTPUT.put_line('There is not such value');
END IF;
close c1;
END t1_trg;
INSERT INTO t1 values (1);
There is not such value
1 row(s) inserted.
INSERT INTO t2 values (1);
1 row(s) inserted.
INSERT INTO t1 values (2);
1
1 row(s) inserted.
正如您所看到的,当我打开游标时,我传递了值1,并且在光标打开时使用了该值。