cancel
Showing results for 
Search instead for 
Did you mean: 

Informations inside trigger

0 Kudos
1,403

Hi,

Can I get information about the connection that fired the trigger ? Especially the connection name.

On an after update trigger can I get the name of the column(s) that are updated ?

Thanks in advence.

Accepted Solutions (0)

Answers (1)

Answers (1)

VolkerBarth
Contributor

You can get the connection name via connection_property('Name').

For the second question, see that FAQ and that.

0 Kudos

Thanks for you reply.

I try your method to know witch culomn is updated but it seems that the "If UPDATE(col) Then" works only if we pass the column name and not a variable with the column name.

I try that but it dont pass inside the IF :

FOR lpp AS ColCurs CURSOR FOR SELECT cname FROM sys.SYSCOLUMNS WHERE Creator = 'DBA' AND tname = 'CLI'
DO
    SET wCol = cname;
    IF UPDATE(wCol) THEN
        SET wValAv = 'AV';
        SET wValAp = 'Ap';
    END IF;
END FOR;
VolkerBarth
Contributor
0 Kudos

You can use the UPDATING() function here which expects the column name as string whereas UPDATE() expects an identifier.

For an example, see the second FAQ I cited in my answer.

0 Kudos

I try "IF UPDATING(wCol) THEN", but I have an -143 Syntax error near 'wCol'. Here wCol is a long nvarchar.

VolkerBarth
Contributor
0 Kudos

Hm, in my tests with 17.0.9.4857, I could avoid the syntax error but within a cursor loop, the UPDATE search condition does not seem to work properly.

Here's my attempt with a trigger on the sample database's Products table, with several debug messages within:

create or replace trigger TU_Products
   after update on GROUPO.Products
   referencing old as o new as n
for each row 
begin
   message 'Trigger "TU_Products" fired' to log;
   for lpp as crs cursor
      for select column_name as @ColName
          from SYS.SYSTABCOL SC key join SYS.SYSTAB
          where creator = user_id('GROUPO') and table_name = 'Products'
          order by column_id
      for read only
   do
      message 'Trigger "TU_Products" checks whether column "' || @ColName || '" was modified.' to log;
      if update(@ColName) then
         message 'Trigger "TU_Products": Column "' || @ColName || '" was modified.' to log;
      end if;
   end for;
   if update(Quantity) then
      message 'Trigger "TU_Products": Column "' || 'Quantity' || '" was modified.' to log;
   end if;
   message 'Trigger "TU_Products" finished' to log;
end;

-- Update one row
update Products
set Quantity = Quantity - 1, UnitPrice = UnitPrice + 0.50
where ID = 300;
-- display the last 11 log messages
select msg_text from sa_server_messages(null, -11) order by msg_id;
VolkerBarth
Contributor
0 Kudos

The test shows the following output:

Trigger "TU_Products" fired
Trigger "TU_Products" checks whether column "ID" was modified.
Trigger "TU_Products" checks whether column "Name" was modified.
Trigger "TU_Products" checks whether column "Description" was modified.
Trigger "TU_Products" checks whether column "Size" was modified.
Trigger "TU_Products" checks whether column "Color" was modified.
Trigger "TU_Products" checks whether column "Quantity" was modified.
Trigger "TU_Products" checks whether column "UnitPrice" was modified.
Trigger "TU_Products" checks whether column "Photo" was modified.
Trigger "TU_Products": Column "Quantity" was modified.
Trigger "TU_Products" finished

Apparently, the "if update(@ColName)" test did not succeed for the "Quantity" column whereas the test via "if update(Quantity)" does - as does the test via the alternate syntax with "if updating('Quantity')".

Breck_Carter
Participant
0 Kudos

There is no column named @ColName so UPDATE ( @ColName ) might be expected to return false.

I don't know if EXECUTE IMMEDIATE will work, but it's the next Dead Chicken to try 🙂

VolkerBarth
Contributor
0 Kudos

Well, being on v17, I had tried with the cool indirect identifier feature but could not provide valid syntax...

0 Kudos

The replies are hidden 😞

VolkerBarth
Contributor
0 Kudos

Hm? What do you mean?