Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Firebird create a context variable

Posted on 2007-11-20
9
Medium Priority
?
2,136 Views
Last Modified: 2013-12-09
I have a trigger for saving the changes into a history table.

For DELETE and UPDATE i use the context variable "old", and for INSERT i have to use the "new" context variable.

I want to use only ONE trigger for INSERT, UPDATE and DELETE because its easier to mantain it later, and my problem ist that i look for a way to use only one variable for acessing the values from "old" and "new" context variable. Also, if INSERT then i have to use the values from "new" else from "old". But i dondt want to use the both on the same procedure.

Hou can i create a variable and assing the "new" if INSERTING and otherwise the "old" context variable?

0
Comment
Question by:Daniel Junges
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 19

Expert Comment

by:Gary Benade
ID: 20321091
CREATE TRIGGER TEST_ FOR TEST ACTIVE AFTER INSERT OR UPDATE OR DELETE POSITION 0 AS
DECLARE VARIABLE CONTEXTVAR VARCHAR(10);
begin
     IF (INSERTING) THEN
         CONTEXTVAR = OLD.LINK;
     ELSE IF (UPDATING) THEN
         CONTEXTVAR = OLD.LINK;
     ELSE // (DELETING)
        CONTEXTVAR = OLD.LINK;
     // REST OF CODE
end
0
 
LVL 19

Expert Comment

by:NickUpson
ID: 20321169
small correction

CREATE TRIGGER TEST_ FOR TEST ACTIVE AFTER INSERT OR UPDATE OR DELETE POSITION 0 AS
DECLARE VARIABLE CONTEXTVAR VARCHAR(10);
begin
     IF (INSERTING) THEN
         CONTEXTVAR = NEW.LINK;             <-------
     ELSE IF (UPDATING) THEN
         CONTEXTVAR = OLD.LINK;             could be either, depending upon which you want to use
     ELSE // (DELETING)
        CONTEXTVAR = OLD.LINK;
     // REST OF CODE
end

I don't think firebird 2 will allow references to invalid context variables for the trigger defn, If I'm right the above will not be allowed in any trigger as eg, a delete trigger doesn't allow code that refers to a new.field
0
 
LVL 14

Author Comment

by:Daniel Junges
ID: 20322173
this solution i have also applied, but i have a lot of Fields. What im searching is a solution like this:

CREATE TRIGGER ....
DECLARE VARIABLE myRow ROW;
begin
     IF (INSERTING) THEN
         myRow = NEW;
     ELSE IF (UPDATING) THEN
         myRow = NEW;
     ELSE
        myRow = OLD;
    ...
    myRow.CompanyName
    ...
end

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 19

Accepted Solution

by:
NickUpson earned 2000 total points
ID: 20322653
you could do it with execute statement

str = 'name = ' || myRow || '.CompanyName';
execute statement str;
0
 
LVL 19

Expert Comment

by:Gary Benade
ID: 20325806
I understand what you are trying to do but since there are no object or array datatypes available to declare in PSQL it isn't possible. Nickupsons last suggestion is the only alternative and it will work but I'm not sure it will achieve your goal of writing simpler code.
0
 
LVL 19

Expert Comment

by:NickUpson
ID: 21341098
I disagree with the Question being deleted, the Question has been answered. It is, perhaps, not the answer that the OP wanted to hear.
0
 
LVL 1

Expert Comment

by:Vee_Mod
ID: 21393913
Force accepted.
Vee_Mod
Experts Exchange Moderator
0
 
LVL 14

Author Comment

by:Daniel Junges
ID: 21393942
i not have received the solution for my problem, so i want to delete this question and have no reason to accept one of these sugestions.
0

Featured Post

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lotus Notes has been used since a very long time as an e-mail client and is very popular because of it's unmatched security. In this article we are going to learn about  RRV Bucket corruption and understand various methods to Fix "RRV Bucket Corrupt…
In this article, we’ll look at how to deploy ProxySQL.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question