是否允许您检索触发器内的序列

发布于 2024-12-06 10:05:03 字数 662 浏览 1 评论 0原文

我希望能够获取序列值,并在触发器中的多个操作上使用相同的值来对表进行审计操作。因此,当更新表时,我想获取所有已更新的列。但当我在数据库中注册更改的列时,我希望将它们分组为一个。有没有办法获取序列号并将其存储在触发器内的变量中。我能够编写的代码不断出现 PLS-00357。

CREATE OR REPLACE TRIGGER TRG_EMPLOYEE_CHANGED AFTER INSERT OR DELETE
 OR UPDATE ON EMPLOYEE  REFERENCING OLD AS old NEW AS new  FOR EACH ROW
DECLARE   
    GROUPID NUMBER := SEQ_POPERATIONLOG_LOGSUBNO.NEXTVAL; 
BEGIN

   OPERATION('FIRST_NAME', GROUPID, :old.FIRST_NAME, :new.FIRST_NAME); 
 OPERATION('LAST_NAME', GROUPID, :old.LAST_NAME, :new.LAST_NAME);  
 OPERATION('ADDRESS', GROUPID, :old.ADDRESS, :new.ADDRESS);  
 OPERATION('TELEPHONE', GROUPID, :old.TELEPHONE, :new.TELEPHONE); 
END;

I want to be able to get teh sequence value and use the same value on several operations in a trigger to do an audit operation for a table. So when a table is updated, I want to get all the column that were updated. But I want to have them grouped as one when I register the changed column in the database. Is there a way to get a sequence number and store in a variable inside a trigger. The code that I was able to make keeps on getting PLS-00357.

CREATE OR REPLACE TRIGGER TRG_EMPLOYEE_CHANGED AFTER INSERT OR DELETE
 OR UPDATE ON EMPLOYEE  REFERENCING OLD AS old NEW AS new  FOR EACH ROW
DECLARE   
    GROUPID NUMBER := SEQ_POPERATIONLOG_LOGSUBNO.NEXTVAL; 
BEGIN

   OPERATION('FIRST_NAME', GROUPID, :old.FIRST_NAME, :new.FIRST_NAME); 
 OPERATION('LAST_NAME', GROUPID, :old.LAST_NAME, :new.LAST_NAME);  
 OPERATION('ADDRESS', GROUPID, :old.ADDRESS, :new.ADDRESS);  
 OPERATION('TELEPHONE', GROUPID, :old.TELEPHONE, :new.TELEPHONE); 
END;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

别理我 2024-12-13 10:05:03

在 11g 之前,唯一支持从序列中获取值的语法是:

DECLARE   
    GROUPID NUMBER ; 
BEGIN
   SELECT  SEQ_POPERATIONLOG_LOGSUBNO.NEXTVAL
   INTO GROUPID
   FROM DUAL;
   ...

一种替代解决方案是在第一个 INSERT 语句中使用 NEXTVAL 和 CURRVAL(即此会话中最近分配的值)对于其他人。

Prior to 11g, the only supported syntax for getting a value from a sequence was:

DECLARE   
    GROUPID NUMBER ; 
BEGIN
   SELECT  SEQ_POPERATIONLOG_LOGSUBNO.NEXTVAL
   INTO GROUPID
   FROM DUAL;
   ...

One alternative solution would be to use NEXTVAL in the first INSERT statement and CURRVAL (i.e. the most recently assigned value in this session) for the others.

江南月 2024-12-13 10:05:03

如果您使用的是 Oracle 10g 或更早版本,那么您需要从 Oracle 的虚拟表 DUAL 中选择序列下一个值。

CREATE OR REPLACE 
TRIGGER TRG_EMPLOYEE_CHANGED 
AFTER INSERT OR DELETE  OR UPDATE 
ON    EMPLOYEE  
REFERENCING OLD AS old NEW AS new  
FOR EACH ROW 
DECLARE        
   GROUPID NUMBER;
BEGIN
   SELECT SEQ_POPERATIONLOG_LOGSUBNO.NEXTVAL
     INTO GROUPID
     FROM dual;

   OPERATION('FIRST_NAME', GROUPID, :old.FIRST_NAME, :new.FIRST_NAME);   
   OPERATION('LAST_NAME', GROUPID, :old.LAST_NAME, :new.LAST_NAME);    
   OPERATION('ADDRESS', GROUPID, :old.ADDRESS, :new.ADDRESS);    
   OPERATION('TELEPHONE', GROUPID, :old.TELEPHONE, :new.TELEPHONE);  
END; 

If you are using Oracle 10g or older then you'd need to select the sequence next value from Oracle's dummy table DUAL.

CREATE OR REPLACE 
TRIGGER TRG_EMPLOYEE_CHANGED 
AFTER INSERT OR DELETE  OR UPDATE 
ON    EMPLOYEE  
REFERENCING OLD AS old NEW AS new  
FOR EACH ROW 
DECLARE        
   GROUPID NUMBER;
BEGIN
   SELECT SEQ_POPERATIONLOG_LOGSUBNO.NEXTVAL
     INTO GROUPID
     FROM dual;

   OPERATION('FIRST_NAME', GROUPID, :old.FIRST_NAME, :new.FIRST_NAME);   
   OPERATION('LAST_NAME', GROUPID, :old.LAST_NAME, :new.LAST_NAME);    
   OPERATION('ADDRESS', GROUPID, :old.ADDRESS, :new.ADDRESS);    
   OPERATION('TELEPHONE', GROUPID, :old.TELEPHONE, :new.TELEPHONE);  
END; 
小巷里的女流氓 2024-12-13 10:05:03

我想我通过使用 select into 得到了答案。

SELECT SEQ_X_LOGSUBNO.NEXTVAL INTO GROUPID FROM DUAL;

I think I got the answer by using select into.

SELECT SEQ_X_LOGSUBNO.NEXTVAL INTO GROUPID FROM DUAL;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文