rutgermons
asked on
microsoft, sql server, 2000, trigger help
folks
In the following trigger I copy the value of the asset.id field to the assetmeter table:
create trigger T_equipment on equipment
for insert as
DECLARE @number int
SELECT @number = MAX(seed)+1 FROM autokey
insert into measurepoint ( eqnum, description,pointname,poin tnum ) ( select eqnum, description,'hours',
@number +1 from inserted)
update autokey set seed =(select max(seed)+1 from autokey )
however based on the value of the id i need to do perfom the follows:
if the id field starts with P-100 then I need to now put in 2 inserts ie
insert into measurepoint ( eqnum, description,pointname,poin tnum )
( select eqnum, description,'hours',@numbe r +1 from inserted)
and
insert into measurepoint ( eqnum, description,pointname,poin tnum )
( select eqnum, description,'meter',@numbe r +1 from inserted)
if the if the id field starts with w-2000 then I need to now put in 3 inserts ie
insert into measurepoint ( eqnum, description,pointname,poin tnum )
( select eqnum, description,'moves',@numbe r +1 from inserted)
and
insert into measurepoint ( eqnum, description,pointname,poin tnum )
( select eqnum, description,'meter',@numbe r +1 from inserted)
and
insert into measurepoint ( eqnum, description,pointname,poin tnum )
( select eqnum, description,'yards',@numbe r +1 from inserted)
how do i achieve this,note i must also make sure that for each insert the update to the autokey table is also ensured
all help will do
In the following trigger I copy the value of the asset.id field to the assetmeter table:
create trigger T_equipment on equipment
for insert as
DECLARE @number int
SELECT @number = MAX(seed)+1 FROM autokey
insert into measurepoint ( eqnum, description,pointname,poin
@number +1 from inserted)
update autokey set seed =(select max(seed)+1 from autokey )
however based on the value of the id i need to do perfom the follows:
if the id field starts with P-100 then I need to now put in 2 inserts ie
insert into measurepoint ( eqnum, description,pointname,poin
( select eqnum, description,'hours',@numbe
and
insert into measurepoint ( eqnum, description,pointname,poin
( select eqnum, description,'meter',@numbe
if the if the id field starts with w-2000 then I need to now put in 3 inserts ie
insert into measurepoint ( eqnum, description,pointname,poin
( select eqnum, description,'moves',@numbe
and
insert into measurepoint ( eqnum, description,pointname,poin
( select eqnum, description,'meter',@numbe
and
insert into measurepoint ( eqnum, description,pointname,poin
( select eqnum, description,'yards',@numbe
how do i achieve this,note i must also make sure that for each insert the update to the autokey table is also ensured
all help will do
ASKER
thanks
will it incrment the autokey table based on every key inserted? ie will this take effetc for each insert?
update autokey set seed =(select max(seed)+1 from autokey )
will it incrment the autokey table based on every key inserted? ie will this take effetc for each insert?
update autokey set seed =(select max(seed)+1 from autokey )
>>will it incrment the autokey table based on every key inserted? ie will this take effetc for each insert?
No, it will not. If you need it to do that, you'll need to call your snippet before each insert and take out the UNION to make them all distinct inserts...
SELECT @number = MAX(seed)+1 FROM autokey
insert into measurepoint ( eqnum, description,pointname,poin tnum ) ( select eqnum, description,'hours',
@number +1 from inserted)
update autokey set seed =(select max(seed)+1 from autokey )
No, it will not. If you need it to do that, you'll need to call your snippet before each insert and take out the UNION to make them all distinct inserts...
SELECT @number = MAX(seed)+1 FROM autokey
insert into measurepoint ( eqnum, description,pointname,poin
@number +1 from inserted)
update autokey set seed =(select max(seed)+1 from autokey )
ASKER
thanks
could you provided the full solution pls?
r
could you provided the full solution pls?
r
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
thanks
lemme check
lemme check
Note that you will get duplicate numbers in the pointnum column if more than one row is inserted. Also the seed value will increment if the trigger fires even if no row is inserted. Is that what you intended?
Triggers must always be written to perform correctly whether zero, one or many rows are inserted. Otherwise they are an accident waiting to happen.
Triggers must always be written to perform correctly whether zero, one or many rows are inserted. Otherwise they are an accident waiting to happen.
ASKER
dportas:
how do i get around ur problem,i agree that the given solutions will cause this problem
if u could help i would be very grateful
r
how do i get around ur problem,i agree that the given solutions will cause this problem
if u could help i would be very grateful
r
Thats always a problem when using a seed table like you're using...you could probably get around it by joining on a UDF and passing in the seed value, but that would put your seed values out of whack because you can't alter data in a function. Is there anyway you can make your seed column an identity column instead?
ASKER
chapmandew
can we not just build a case in the trigger that first determines the value of the eqnum field and then fires the inserts,if the eqnum does not match this then it should fire inserts in the first place
all help will do
can we not just build a case in the trigger that first determines the value of the eqnum field and then fires the inserts,if the eqnum does not match this then it should fire inserts in the first place
all help will do
But...like dportas said, if you have more than one record inserted your identity values will be off. The only way around it is to do this in a cursor...which is kinda nasty to do in a trigger.
ASKER
what other options do i have?
Your best option (IMO) is to create a cursor, loop through the records in the inserted table, and do the inserts that you need, incrementing your seed value after each insert.
DECLARE CursorTemplate CURSOR
FAST_FORWARD FOR
SELECT * FROM inserted
OPEN CursorTemplate
FETCH NEXT FROM CursorTemplate
INTO @Var1, @Var2, @Var3
WHILE (@@FETCH_STATUS = 0)
BEGIN
--do your inserts here
FETCH NEXT FROM CursorTemplate
INTO @Var1, @Var2, @Var3
END
CLOSE CursorTemplate
DEALLOCATE CursorTemplate
DECLARE CursorTemplate CURSOR
FAST_FORWARD FOR
SELECT * FROM inserted
OPEN CursorTemplate
FETCH NEXT FROM CursorTemplate
INTO @Var1, @Var2, @Var3
WHILE (@@FETCH_STATUS = 0)
BEGIN
--do your inserts here
FETCH NEXT FROM CursorTemplate
INTO @Var1, @Var2, @Var3
END
CLOSE CursorTemplate
DEALLOCATE CursorTemplate
ASKER
phew
im gonna ask if u can pls help me with the solution,im a bit stumped
tia
r
im gonna ask if u can pls help me with the solution,im a bit stumped
tia
r
OK...give me an hour or so...working on some other things, and this might take me a couple of minutes.
ASKER
thanks mate,ur saving me some grey hair!!
:)
:)
ASKER
Chapmandrew
i have solved the issue
alter trigger T_eqnumqq on equipment
for insert as
begin
DECLARE @number int
SELECT @number = MAX(seed)+1 FROM autokey where tbname ='MEASUREPOINT' and name ='POINTNUM'
insert into measurepoint ( eqnum, description,pointname,poin tnum,prior ity,lowerw arning,low eraction,
upperwarning,upperaction,u nitofmeasu re,siteid, orgid )
(select eqnum, 'Malmaat wissel','MMW010',@number ,1,1435, 1434,
1437, 1438,NULL,siteid,orgid from inserted where eqnum like 'W-%')
update autokey set seed =(select MAX(pointnum) FROM measurepoint )where tbname='MEASUREPOINT'
--Wissel
SELECT @number = MAX(seed)+1 FROM autokey where tbname ='MEASUREPOINT' and name ='POINTNUM'
and orgid='DEGLOBE'
insert into measurepoint ( eqnum, description,pointname,poin tnum,prior ity,lowerw arning,low eraction,
upperwarning,upperaction,u nitofmeasu re,siteid, orgid )
(select eqnum, 'Verkanting wissel','VKW011',@number ,1 ,-2,-5, 2,
5,NULL,siteid,orgid from inserted where eqnum like 'W-%')
update autokey set seed =(select MAX(pointnum) FROM measurepoint )where tbname='MEASUREPOINT'
SELECT @number = MAX(seed)+1 FROM autokey where tbname ='MEASUREPOINT' and name ='POINTNUM'
and orgid='DEGLOBE'
insert into measurepoint ( eqnum, description,pointname,poin tnum,prior ity,lowerw arning,low eraction,
upperwarning,upperaction,u nitofmeasu re,siteid, orgid )
(select eqnum, 'Inlaskop','INLSKP010',@nu mber ,1,15,14,18,25,
NULL,siteid,orgid from inserted where eqnum like 'BO-%')
update autokey set seed =(select MAX(pointnum) FROM measurepoint )where tbname='MEASUREPOINT'
--3 Bogen
SELECT @number = MAX(seed)+1 FROM autokey where tbname ='MEASUREPOINT' and name ='POINTNUM'
and orgid='DEGLOBE'
insert into measurepoint ( eqnum, description,pointname,poin tnum,prior ity,lowerw arning,low eraction,
upperwarning,upperaction,u nitofmeasu re,siteid, orgid )
(select eqnum, 'Kopslijtage Bovenbeen','KPBB010',@numb er ,1,10, 9,
20, 23,NULL,siteid,orgid from inserted where eqnum like 'BO-%')
update autokey set seed =(select MAX(pointnum) FROM measurepoint )where tbname='MEASUREPOINT'
SELECT @number = MAX(seed)+1 FROM autokey where tbname ='MEASUREPOINT' and name ='POINTNUM'
and orgid='DEGLOBE'
insert into measurepoint ( eqnum, description,pointname,poin tnum,prior ity,lowerw arning,low eraction,
upperwarning,upperaction,u nitofmeasu re,siteid, orgid )
(select eqnum, 'Kopslijtage Onderbeen','KPOB020',@numb er ,1,10,9,
20,23,NULL,siteid,orgid from inserted where eqnum like 'BO-%')
update autokey set seed =(select MAX(pointnum) FROM measurepoint )where tbname='MEASUREPOINT'
SELECT @number = MAX(seed)+1 FROM autokey where tbname ='MEASUREPOINT' and name ='POINTNUM'
and orgid='DEGLOBE'
insert into measurepoint ( eqnum, description,pointname,poin tnum,prior ity,lowerw arning,low eraction,u pperwarnin g,upperact ion,unitof measure,si teid,orgid )
(select eqnum, 'Slijtage Bovenbeen Boog','SLBB010',@number ,1,-1,-2,
6,8,NULL,siteid,orgid from inserted where eqnum like 'BO-%')
update autokey set seed =(select MAX(pointnum) FROM measurepoint )where tbname='MEASUREPOINT'
SELECT @number = MAX(seed)+1 FROM autokey where tbname ='MEASUREPOINT' and name ='POINTNUM'
and orgid='DEGLOBE'
insert into measurepoint ( eqnum, description,pointname,poin tnum,prior ity,lowerw arning,low eraction,u pperwarnin g,upperact ion,unitof measure,si teid,orgid )
(select eqnum, 'Zijdelingse slijtage vangrail Bovenbeen','SLVGRLBB010',@ number ,1,-1, -2,
3, 4,NULL,siteid,orgid from inserted where eqnum like 'BO-%')
update autokey set seed =(select MAX(pointnum) FROM measurepoint )where tbname='MEASUREPOINT'
SELECT @number = MAX(seed)+1 FROM autokey where tbname ='MEASUREPOINT' and name ='POINTNUM'
and orgid='DEGLOBE'
insert into measurepoint ( eqnum, description,pointname,poin tnum,prior ity,lowerw arning,low eraction,u pperwarnin g,upperact ion,unitof measure,si teid,orgid )
(select eqnum, 'Zijdelingse slijtage vangrail Onderbeen','SLVGRLOB0202', @number ,1,-1, -2,
3, 4,NULL,siteid,orgid from inserted where eqnum like 'BO-%')
update autokey set seed =(select MAX(pointnum) FROM measurepoint )where tbname='MEASUREPOINT'
end
however if u have ur solution i would be keen to see it,i will credit u anyhonw because of u dilligent assistance
r
i have solved the issue
alter trigger T_eqnumqq on equipment
for insert as
begin
DECLARE @number int
SELECT @number = MAX(seed)+1 FROM autokey where tbname ='MEASUREPOINT' and name ='POINTNUM'
insert into measurepoint ( eqnum, description,pointname,poin
upperwarning,upperaction,u
(select eqnum, 'Malmaat wissel','MMW010',@number ,1,1435, 1434,
1437, 1438,NULL,siteid,orgid from inserted where eqnum like 'W-%')
update autokey set seed =(select MAX(pointnum) FROM measurepoint )where tbname='MEASUREPOINT'
--Wissel
SELECT @number = MAX(seed)+1 FROM autokey where tbname ='MEASUREPOINT' and name ='POINTNUM'
and orgid='DEGLOBE'
insert into measurepoint ( eqnum, description,pointname,poin
upperwarning,upperaction,u
(select eqnum, 'Verkanting wissel','VKW011',@number ,1 ,-2,-5, 2,
5,NULL,siteid,orgid from inserted where eqnum like 'W-%')
update autokey set seed =(select MAX(pointnum) FROM measurepoint )where tbname='MEASUREPOINT'
SELECT @number = MAX(seed)+1 FROM autokey where tbname ='MEASUREPOINT' and name ='POINTNUM'
and orgid='DEGLOBE'
insert into measurepoint ( eqnum, description,pointname,poin
upperwarning,upperaction,u
(select eqnum, 'Inlaskop','INLSKP010',@nu
NULL,siteid,orgid from inserted where eqnum like 'BO-%')
update autokey set seed =(select MAX(pointnum) FROM measurepoint )where tbname='MEASUREPOINT'
--3 Bogen
SELECT @number = MAX(seed)+1 FROM autokey where tbname ='MEASUREPOINT' and name ='POINTNUM'
and orgid='DEGLOBE'
insert into measurepoint ( eqnum, description,pointname,poin
upperwarning,upperaction,u
(select eqnum, 'Kopslijtage Bovenbeen','KPBB010',@numb
20, 23,NULL,siteid,orgid from inserted where eqnum like 'BO-%')
update autokey set seed =(select MAX(pointnum) FROM measurepoint )where tbname='MEASUREPOINT'
SELECT @number = MAX(seed)+1 FROM autokey where tbname ='MEASUREPOINT' and name ='POINTNUM'
and orgid='DEGLOBE'
insert into measurepoint ( eqnum, description,pointname,poin
upperwarning,upperaction,u
(select eqnum, 'Kopslijtage Onderbeen','KPOB020',@numb
20,23,NULL,siteid,orgid from inserted where eqnum like 'BO-%')
update autokey set seed =(select MAX(pointnum) FROM measurepoint )where tbname='MEASUREPOINT'
SELECT @number = MAX(seed)+1 FROM autokey where tbname ='MEASUREPOINT' and name ='POINTNUM'
and orgid='DEGLOBE'
insert into measurepoint ( eqnum, description,pointname,poin
(select eqnum, 'Slijtage Bovenbeen Boog','SLBB010',@number ,1,-1,-2,
6,8,NULL,siteid,orgid from inserted where eqnum like 'BO-%')
update autokey set seed =(select MAX(pointnum) FROM measurepoint )where tbname='MEASUREPOINT'
SELECT @number = MAX(seed)+1 FROM autokey where tbname ='MEASUREPOINT' and name ='POINTNUM'
and orgid='DEGLOBE'
insert into measurepoint ( eqnum, description,pointname,poin
(select eqnum, 'Zijdelingse slijtage vangrail Bovenbeen','SLVGRLBB010',@
3, 4,NULL,siteid,orgid from inserted where eqnum like 'BO-%')
update autokey set seed =(select MAX(pointnum) FROM measurepoint )where tbname='MEASUREPOINT'
SELECT @number = MAX(seed)+1 FROM autokey where tbname ='MEASUREPOINT' and name ='POINTNUM'
and orgid='DEGLOBE'
insert into measurepoint ( eqnum, description,pointname,poin
(select eqnum, 'Zijdelingse slijtage vangrail Onderbeen','SLVGRLOB0202',
3, 4,NULL,siteid,orgid from inserted where eqnum like 'BO-%')
update autokey set seed =(select MAX(pointnum) FROM measurepoint )where tbname='MEASUREPOINT'
end
however if u have ur solution i would be keen to see it,i will credit u anyhonw because of u dilligent assistance
r
I haven't had a chance to get to it yet, unfortunately....but, it would basically be something like this:
alter trigger T_eqnumqq on equipment
for insert as
begin
DECLARE @number int
DECLARE CursorTemplate CURSOR
FAST_FORWARD FOR
SELECT field1, field2, field3 FROM inserted
OPEN CursorTemplate
FETCH NEXT FROM CursorTemplate
INTO @Var1, @Var2, @Var3
WHILE (@@FETCH_STATUS = 0)
BEGIN
insert into measurepoint ( eqnum, description,pointname)
select @var1, @var2, @var3
--update your seed table to +1
FETCH NEXT FROM CursorTemplate
INTO @Var1, @Var2, @Var3
END
CLOSE CursorTemplate
DEALLOCATE CursorTemplate
end
However, if yours works then go with it. :)
alter trigger T_eqnumqq on equipment
for insert as
begin
DECLARE @number int
DECLARE CursorTemplate CURSOR
FAST_FORWARD FOR
SELECT field1, field2, field3 FROM inserted
OPEN CursorTemplate
FETCH NEXT FROM CursorTemplate
INTO @Var1, @Var2, @Var3
WHILE (@@FETCH_STATUS = 0)
BEGIN
insert into measurepoint ( eqnum, description,pointname)
select @var1, @var2, @var3
--update your seed table to +1
FETCH NEXT FROM CursorTemplate
INTO @Var1, @Var2, @Var3
END
CLOSE CursorTemplate
DEALLOCATE CursorTemplate
end
However, if yours works then go with it. :)
create trigger T_equipment on equipment
for insert as
begin
DECLARE @number int
SELECT @number = MAX(seed)+1 FROM autokey
insert into measurepoint ( eqnum, description,pointname,poin
@number +1 from inserted)
update autokey set seed =(select max(seed)+1 from autokey )
--2 inserts
insert into measurepoint ( eqnum, description,pointname,poin
select eqnum, description,'hours',@numbe
union all
select eqnum, description,'meter',@numbe
--3 inserts
insert into measurepoint ( eqnum, description,pointname,poin
select eqnum, description,'moves',@numbe
union all
select eqnum, description,'moves',@numbe
union all
select eqnum, description,'moves',@numbe
end