dbTalk Databases Forums  

Problem with a trigger

sybase.public.sqlanywhere.general sybase.public.sqlanywhere.general


Discuss Problem with a trigger in the sybase.public.sqlanywhere.general forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Claude
 
Posts: n/a

Default Problem with a trigger - 10-04-2009 , 12:57 PM






I would like to set up a trigger that updates a field in the row modified
with a timestamp.

The table (linkitad) looks as follow

lktyp char(1)
lkitem char(12)
lkadcode char(10)
as primary key
lkmoddat timestamp to receive the timestamp of the modification by the
trigger

let say 2 fields I want to "observe" the modification
field1, field2..

and some other fields I do not want to observe.

I wrote the following sql

create trigger dba.zvf_linkitad after update of field1, field2
order 1 on DBA.linkitad
referencing old as old_data
for each row
begin
update linkitad set lkmoddat = now(*) where lktyp = old.lktyp and lkitem =
old.lkitem and lkadcode = old.lkadcode
end;

I can run it with no error, but when I change one of the 'observed' fields,
nothing happens ( no mod to lkmoddat, no error reported )

Any idea of the error in the trigger SQL ?

Reply With Quote
  #2  
Old   
Lucjan Chmura [Sybase iAnywhere]
 
Posts: n/a

Default Re: Problem with a trigger - 10-05-2009 , 12:16 PM






Next time please mention SA version.

Couple problems with your trigger.

- you say 'create trigger dba.zvf_linkitad after update of field1,
field2... ' but field1, field2 are not defined on your table, these should
be valid columns' names
- you have defined 'referencing old as 'old_data' but in your trigger you
are referring to 'old' in your WHERE clause
- you are joining 'new' and 'old' with a WHERE clause on 3 columns assuming
that this will give equality , but if any of these 3 columns is updated
'new' will never be equal to 'old'

Try following:

create trigger dba.zvf_linkitad after update of lktyp, lkitem
order 1 on DBA.linkitad
referencing old as old_data
for each row
begin
update linkitad set lkmoddat = now(*) where old_data.lkadcode = lkadcode
end;

BTW. it would be better to have a default (current timestamp) on lkmoddat,
so then when row is inserted you also have record of that.

Quote:
I can run it with no error, but when I change one of the 'observed'
fields, nothing happens ( no mod to lkmoddat, no error reported )
Check your dbisql Options -> 'On error' , you may have it set to 'Continue'
which would ignore errors, but you should still be able to see SQLCode <0.

Lucjan









"Claude" <c.verstichel (AT) gmail (DOT) com> wrote

Quote:
I would like to set up a trigger that updates a field in the row modified
with a timestamp.

The table (linkitad) looks as follow

lktyp char(1)
lkitem char(12)
lkadcode char(10)
as primary key
lkmoddat timestamp to receive the timestamp of the modification by the
trigger

let say 2 fields I want to "observe" the modification
field1, field2..

and some other fields I do not want to observe.

I wrote the following sql

create trigger dba.zvf_linkitad after update of field1, field2
order 1 on DBA.linkitad
referencing old as old_data
for each row
begin
update linkitad set lkmoddat = now(*) where lktyp = old.lktyp and lkitem
= old.lkitem and lkadcode = old.lkadcode
end;

I can run it with no error, but when I change one of the 'observed'
fields, nothing happens ( no mod to lkmoddat, no error reported )

Any idea of the error in the trigger SQL ?

Reply With Quote
  #3  
Old   
Claude
 
Posts: n/a

Default Re: Problem with a trigger - 10-06-2009 , 03:03 AM



Your second point was the problem. Thank you.
Thank you as well for your other comment.
Claude

"Lucjan Chmura [Sybase iAnywhere]" <lchmura_at_sybase_dot_com> a écrit dans
le message de news: 4aca29d6$1 (AT) forums-1-dub (DOT) ..
Quote:
Next time please mention SA version.

Couple problems with your trigger.

- you say 'create trigger dba.zvf_linkitad after update of field1,
field2... ' but field1, field2 are not defined on your table, these should
be valid columns' names
- you have defined 'referencing old as 'old_data' but in your trigger you
are referring to 'old' in your WHERE clause
- you are joining 'new' and 'old' with a WHERE clause on 3 columns
assuming that this will give equality , but if any of these 3 columns is
updated 'new' will never be equal to 'old'

Try following:

create trigger dba.zvf_linkitad after update of lktyp, lkitem
order 1 on DBA.linkitad
referencing old as old_data
for each row
begin
update linkitad set lkmoddat = now(*) where old_data.lkadcode =
lkadcode
end;

BTW. it would be better to have a default (current timestamp) on lkmoddat,
so then when row is inserted you also have record of that.

I can run it with no error, but when I change one of the 'observed'
fields, nothing happens ( no mod to lkmoddat, no error reported )

Check your dbisql Options -> 'On error' , you may have it set to
'Continue' which would ignore errors, but you should still be able to see
SQLCode <0.

Lucjan









"Claude" <c.verstichel (AT) gmail (DOT) com> wrote in message
news:4ac8e222$1 (AT) forums-1-dub (DOT) ..
I would like to set up a trigger that updates a field in the row modified
with a timestamp.

The table (linkitad) looks as follow

lktyp char(1)
lkitem char(12)
lkadcode char(10)
as primary key
lkmoddat timestamp to receive the timestamp of the modification by the
trigger

let say 2 fields I want to "observe" the modification
field1, field2..

and some other fields I do not want to observe.

I wrote the following sql

create trigger dba.zvf_linkitad after update of field1, field2
order 1 on DBA.linkitad
referencing old as old_data
for each row
begin
update linkitad set lkmoddat = now(*) where lktyp = old.lktyp and lkitem
= old.lkitem and lkadcode = old.lkadcode
end;

I can run it with no error, but when I change one of the 'observed'
fields, nothing happens ( no mod to lkmoddat, no error reported )

Any idea of the error in the trigger SQL ?



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.