September 9, 2010 at 8:25 am
Ok it seems i´ve not been the only with this problem.
I found on the web the following;
create table fred
(fredId int primary key,
value varchar(10))
go
create trigger fred$insertTrigger
on fred
after insert
as
update fred
set fredId = fredId * 10000
where fredId in (select fredId from inserted)
go
Ideas?
September 9, 2010 at 8:28 am
if the three columns make the row unique, why are you changing one of the values? a trigger like that should change columns other than those comrising the primary key.
if you are going to do that, this should be an INSTEADOF INSERT trigger, not an AFTER INSERT
Lowell
September 9, 2010 at 8:37 am
Lowell (9/9/2010)
if the three columns make the row unique, why are you changing one of the values? a trigger like that should change columns other than those comrising the primary key.if you are going to do that, this should be an INSTEADOF INSERT trigger, not an AFTER INSERT
if i do an instead of trigger that would delete the original record. I dont want the original record to be deleted. I want a new record that takes all the data from from the recently inserted record where idcontrato =2 and eicanalid =91. And then give this new record a new idcontrato.
September 9, 2010 at 10:31 am
any ideas?
September 9, 2010 at 10:41 am
A trigger certainly does not seem to be the best method to use to accomplish what needs to be done.
Michael L John
If you assassinate a DBA, would you pull a trigger?
To properly post on a forum:
http://www.sqlservercentral.com/articles/61537/
September 9, 2010 at 10:43 am
igngua (9/9/2010)
if i do an instead of trigger that would delete the original record. I dont want the original record to be deleted. I want a new record that takes all the data from from the recently inserted record where idcontrato =2 and eicanalid =91. And then give this new record a new idcontrato.
if you want a new record, you need to INSERT, not update the existing.
Lowell
September 9, 2010 at 10:46 am
Michael L John (9/9/2010)
A trigger certainly does not seem to be the best method to use to accomplish what needs to be done.
why?
September 9, 2010 at 11:49 am
Lowell (9/9/2010)
igngua (9/9/2010)
if i do an instead of trigger that would delete the original record. I dont want the original record to be deleted. I want a new record that takes all the data from from the recently inserted record where idcontrato =2 and eicanalid =91. And then give this new record a new idcontrato.if you want a new record, you need to INSERT, not update the existing.
can you give me an example please.?
September 9, 2010 at 11:58 am
lets try something different...show us what you think the INSERT INTO statement would look like;
your requirement changed form updating colA and colB to something different....all the clues for your trigger are there...i'd like to see some input from you this time.
Lowell
September 9, 2010 at 12:02 pm
Lowell (9/9/2010)
lets try something different...show us what you think the INSERT INTO statement would look like;your requirement changed form updating colA and colB to something different....all the clues for your trigger are there...i'd like to see some input from you this time.
ok.. here i go...
insert into EICANALESRES
select * from inserted
where idcontrato= 2
and eicanalid = 19
My question using the insert is; how do i change the value from idcontrato from '2' to '13'
thanks.
September 9, 2010 at 12:09 pm
You said:
I want a new record that takes all the data from from the recently inserted record where idcontrato =2 and eicanalid =91. And then give this new record a new idcontrato.
I understand this to mean that you ACTUALLY need to insert TWO records in the table, if the idcontrato and eicanalid values are 2 and 91 respectively.
All of the fields will contain identical data except for the idcontrato field.
So, would it not make sense to create a second insert statement if the proper conditions are met when the first insert statement occurs?
Plus, and I may get a few arguments here, if you trying to put business logic in a trigger, then you are asking for trouble. What is the primary purpose of a trigger? To enforce data integrity.
Michael L John
If you assassinate a DBA, would you pull a trigger?
To properly post on a forum:
http://www.sqlservercentral.com/articles/61537/
September 9, 2010 at 12:15 pm
Michael L John (9/9/2010)
You said:I want a new record that takes all the data from from the recently inserted record where idcontrato =2 and eicanalid =91. And then give this new record a new idcontrato.
absolutely right.
I understand this to mean that you ACTUALLY need to insert TWO records in the table, if the idcontrato and eicanalid values are 2 and 91 respectively.
All of the fields will contain identical data except for the idcontrato field.
So, would it not make sense to create a second insert statement if the proper conditions are met when the first insert statement occurs?
i understand that but i dont know how to do it, i get lost in the syntax.
im kinda noob.:-)
September 9, 2010 at 12:34 pm
Can you post the original insert statement/procedure that performs the first insert?
It's probably will be something like:
INSERT INTO Table (field, field,...)
VALUES(x, y, ...)
If x = 91 and y = 3 Begin
INSERT INTO Table (field, field,...)
VALUES(New X value, y, ...)
End
Michael L John
If you assassinate a DBA, would you pull a trigger?
To properly post on a forum:
http://www.sqlservercentral.com/articles/61537/
September 9, 2010 at 12:38 pm
Michael L John (9/9/2010)
Can you post the original insert statement/procedure that performs the first insert?It's probably will be something like:
INSERT INTO Table (field, field,...)
VALUES(x, y, ...)
If x = 91 and y = 3 Begin
INSERT INTO Table (field, field,...)
VALUES(New X value, y, ...)
End
the first insert is made by a program. I dont know how it does it.
September 9, 2010 at 1:30 pm
UFFF! ive made it...after a lot of effort.
thanks for your help.
now i share.
alter TRIGGER trgAfterInsert ON eicanalesres
FOR INSERT
AS
declare @contrato decimal (10,0);
declare @canal decimal (10,0);
declare @servidor datetime;
declare @maquina datetime;
declare @corregida datetime;
declare @fisico decimal (25,4);
declare @numerico decimal (25,4);
declare @calculado decimal (25,4);
declare @estado smallint
delete from eicanalesres
where idcontrato = '13' and
eicanalid = '91' and
eivalorfis = '0' and
eivalornum = '0'
select @canal = i.eicanalid from inserted i where idcontrato ='2';
select @canal = i.eicanalid from inserted i where eicanalid ='91';
select @servidor = i.eifechserv from inserted i;
select @maquina = i.eifechmaq from inserted i;
select @corregida = i.eifechcorr from inserted i;
select @fisico = i.eivalorfis from inserted i;
select @numerico = i.eivalornum from inserted i;
select @calculado = i.eivalorcalc from inserted i;
select @estado = i.eiest from inserted i;
insert into eicanalesres
(IdContrato,
EiCanalId,
eiFechServ,
EiFechMaq,
EiFechCorr,
EiValorFis,
EiValorNum,
EiValorCalc,
EiEst)
values('13',
@canal,
@servidor,
@maquina,
@corregida,
@fisico,
@numerico,
@calculado,
@estado);
GO
Viewing 15 posts - 16 through 30 (of 34 total)
You must be logged in to reply to this topic. Login to reply