Solved

microsoft, sql server, 2000, trigger help

Posted on 2008-06-10
18
269 Views
Last Modified: 2010-03-20
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,pointnum ) ( 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,pointnum )
  ( select eqnum, description,'hours',@number +1 from inserted)

and

insert into measurepoint ( eqnum, description,pointname,pointnum )
  ( select eqnum, description,'meter',@number +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,pointnum )
  ( select eqnum, description,'moves',@number +1 from inserted)

and

insert into measurepoint ( eqnum, description,pointname,pointnum )
  ( select eqnum, description,'meter',@number +1 from inserted)
and

insert into measurepoint ( eqnum, description,pointname,pointnum )
  ( select eqnum, description,'yards',@number +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
0
Comment
Question by:rutgermons
  • 9
  • 8
18 Comments
 
LVL 60

Expert Comment

by:chapmandew
ID: 21750476
give this a shot:

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,pointnum ) ( select eqnum, description,'hours',
 @number +1 from inserted)
update autokey set seed  =(select max(seed)+1 from autokey )

--2 inserts

insert into measurepoint ( eqnum, description,pointname,pointnum )
select eqnum, description,'hours',@number +1 from inserted where idfield like 'P-100%'
union all
select eqnum, description,'meter',@number +1 from inserted where idfield like 'P-100%'

--3 inserts

insert into measurepoint ( eqnum, description,pointname,pointnum )
select eqnum, description,'moves',@number +1 from inserted where idfield like 'w-2000%'
union all
select eqnum, description,'moves',@number +1 from inserted where idfield like 'w-2000%'
union all
select eqnum, description,'moves',@number +1 from inserted where idfield like 'w-2000%'

end
0
 

Author Comment

by:rutgermons
ID: 21750514
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 )
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 21750636
>>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,pointnum ) ( select eqnum, description,'hours',
 @number +1 from inserted)
update autokey set seed  =(select max(seed)+1 from autokey )
0
Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

 

Author Comment

by:rutgermons
ID: 21750753
thanks

could you provided the full solution pls?

r
0
 
LVL 60

Accepted Solution

by:
chapmandew earned 500 total points
ID: 21750794
sure...try this:

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,pointnum ) ( select eqnum, description,'hours',
 @number +1 from inserted)
update autokey set seed  =(select max(seed)+1 from autokey )

--2 inserts

SELECT @number = MAX(seed)+1 FROM autokey
insert into measurepoint ( eqnum, description,pointname,pointnum )
select eqnum, description,'hours',@number +1 from inserted where idfield like 'P-100%'
update autokey set seed  =(select max(seed)+1 from autokey )

SELECT @number = MAX(seed)+1 FROM autokey
insert into measurepoint ( eqnum, description,pointname,pointnum )
select eqnum, description,'meter',@number +1 from inserted where idfield like 'P-100%'
update autokey set seed  =(select max(seed)+1 from autokey )

--3 inserts
SELECT @number = MAX(seed)+1 FROM autokey
insert into measurepoint ( eqnum, description,pointname,pointnum )
select eqnum, description,'moves',@number +1 from inserted where idfield like 'w-2000%'
update autokey set seed  =(select max(seed)+1 from autokey )

SELECT @number = MAX(seed)+1 FROM autokey
insert into measurepoint ( eqnum, description,pointname,pointnum )
select eqnum, description,'moves',@number +1 from inserted where idfield like 'w-2000%'
update autokey set seed  =(select max(seed)+1 from autokey )

SELECT @number = MAX(seed)+1 FROM autokey
insert into measurepoint ( eqnum, description,pointname,pointnum )
select eqnum, description,'moves',@number +1 from inserted where idfield like 'w-2000%'
update autokey set seed  =(select max(seed)+1 from autokey )

end
0
 

Author Comment

by:rutgermons
ID: 21750940
thanks
lemme check
0
 
LVL 22

Expert Comment

by:dportas
ID: 21754084
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.


0
 

Author Comment

by:rutgermons
ID: 21759526
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
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 21759543
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?
0
 

Author Comment

by:rutgermons
ID: 21759662
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
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 21759745
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.
0
 

Author Comment

by:rutgermons
ID: 21759799
what other options do i have?
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 21759819
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
0
 

Author Comment

by:rutgermons
ID: 21759848
phew

im gonna ask if u can pls help me with the solution,im a bit stumped

tia

r
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 21759884
OK...give me an hour or so...working on some other things, and this might take me a couple of minutes.
0
 

Author Comment

by:rutgermons
ID: 21759907
thanks mate,ur saving me some grey hair!!

:)
0
 

Author Comment

by:rutgermons
ID: 21761419
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,pointnum,priority,lowerwarning,loweraction,
upperwarning,upperaction,unitofmeasure,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,pointnum,priority,lowerwarning,loweraction,
upperwarning,upperaction,unitofmeasure,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,pointnum,priority,lowerwarning,loweraction,
upperwarning,upperaction,unitofmeasure,siteid,orgid )
(select eqnum, 'Inlaskop','INLSKP010',@number ,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,pointnum,priority,lowerwarning,loweraction,
upperwarning,upperaction,unitofmeasure,siteid,orgid )
(select eqnum, 'Kopslijtage Bovenbeen','KPBB010',@number  ,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,pointnum,priority,lowerwarning,loweraction,
upperwarning,upperaction,unitofmeasure,siteid,orgid )
(select eqnum, 'Kopslijtage Onderbeen','KPOB020',@number  ,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,pointnum,priority,lowerwarning,loweraction,upperwarning,upperaction,unitofmeasure,siteid,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,pointnum,priority,lowerwarning,loweraction,upperwarning,upperaction,unitofmeasure,siteid,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,pointnum,priority,lowerwarning,loweraction,upperwarning,upperaction,unitofmeasure,siteid,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
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 21761948
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. :)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

756 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