Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Update Trigger.

Author  Topic 

Amjath
Yak Posting Veteran

66 Posts

Posted - 2006-02-01 : 00:15:45
hai to all,

i want to create a update trigger, that should fire only when a particular column updated with particular value. Is it possible.

Help me out

with regards
-Amjath

shallu1_gupta
Constraint Violating Yak Guru

394 Posts

Posted - 2006-02-01 : 01:09:46
Yes you can check that using
if update(columnName) with in an update trigger
Go to Top of Page

Amjath
Yak Posting Veteran

66 Posts

Posted - 2006-02-01 : 02:10:22
sorry i didnt get u

i am new to this trigger,

plz help me out with example code.

plz do this need full help.



with regards
-Amjath
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-02-01 : 02:24:31
Your the other thread's trigger is an UPDATE TRIGGER. http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=61054.
An UPDATE TRIGGER will always fired whenever the table is updated. If you only want to perform something only when a particular column is updated, use if update(columnName) as shallu1_gupta suggested.

Read the link that i provided in that thread and you will understand better

----------------------------------
'KH'


Go to Top of Page

Amjath
Yak Posting Veteran

66 Posts

Posted - 2006-02-01 : 02:42:32
hai khtan nice to c u again.

my problem is i want to fire the trigger only when a column get updated with particular value.

how can i do this, i try like this but it wont work

if update(columnName = 'Value')

Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-02-01 : 03:03:08
See the code below for illustration
create table table1
(
col1 int,
col2 int,
col3 int
)

create trigger tu_table1 on table1 for update
as
begin
if update (col2)
begin
RAISERROR ('These columns should never be updated', 16, 1)
ROLLBACK TRANSACTION
end
end

insert into table1
select 1, 1, 1 union all
select 2, 2, 2 union all
select 3, 3, 3

select * from table1
Result
======
col1 col2 col3
1 1 1
2 2 2
3 3 3

update table1 set col3 = 30 where col1 = 3

select * from table1
Result
======
col1 col2 col3
1 1 1
2 2 2
3 3 30

update table1 set col2 = 3
Result
======
Server: Msg 50000, Level 16, State 1, Procedure tu_table1, Line 6
These columns should never be updated


----------------------------------
'KH'


Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-02-01 : 03:23:42
Also, read about Create Trigger in BOL, SQL Server help file

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Amjath
Yak Posting Veteran

66 Posts

Posted - 2006-02-01 : 03:27:33
hai khtan,

for that particular column i have various value and various update is going to happen, so there is possible for raise trigger which will be waste. to avoid that i like to have this


to fire trigger when particular column updated with particular value then fire the trigger

with regards
-Amjath.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-02-01 : 03:38:50
quote:
for that particular column i have various value and various update is going to happen, so there is possible for raise trigger which will be waste]

An UPDATE Trigger will fire whenever an UPDATE is perform to the TABLE. This cannot be avoided.
What you can do is place your code in side the IF UPDATE(column_name) BEGIN ... END block.

----------------------------------
'KH'


Go to Top of Page

Amjath
Yak Posting Veteran

66 Posts

Posted - 2006-02-01 : 03:51:17
Hai khtan

Is this correct,
ALTER TRIGGER trgSR_CheckAndAddSampleRequest_Mill1
ON NYS1ReHeat FOR UPDATE
AS
BEGIN
DECLARE @nHeat AS INT,
@nRollMillBarID AS INT,
@sProduct AS VARCHAR(20),
@nSRCount AS INT,
@sLocation AS VARCHAR(20)

IF UPDATE(Location)
BEGIN
SELECT @sLocation = Location FROM INSERTED
IF (@sLocation = 'REHEAT')
IF NOT EXISTS (SELECT * FROM Physicals phy INNER JOIN Inserted ins ON phy.Heat = ins.Heat
AND phy.SQLProduct = ins.FinalProd)
BEGIN
SELECT @nSRCount = COUNT(*)
FROM Inserted ins INNER JOIN NYS1Reheat Reheat
ON ins.Heat = Reheat.Heat
AND ins.FinalProd = Reheat.FinalProd
INNER JOIN SampleRequest SR
ON SR.RollMillBarID = Reheat.LocSeq
WHERE (SR.SampleRequest = 1 OR SR.SampleCut = 1)

IF (@nSRCount < 2)
BEGIN
INSERT INTO SampleRequest(RollMillBarID, SampleRequest, SampleCut, AutoRequest)
SELECT LocSeq, 1, 0, 1
FROM inserted
END
END
END
END
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-02-01 : 04:36:15
why don't you modify the trigger based on the one i posted on http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=61054
This trigger does not handle multiple inserted record.
quote:
SELECT @sLocation = Location FROM INSERTED




----------------------------------
'KH'


Go to Top of Page

Amjath
Yak Posting Veteran

66 Posts

Posted - 2006-02-01 : 04:44:03
Hai khtan,

Sorry for the trouble.

Location value is must then how can i take the location and the locseq from the inserted table. for the same heat and product there are lots of locations are there in reheat table, then how can i get that one.

once again sorry for the inconvience

With Regards
-Amjath
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-02-01 : 04:54:38
Sorry... missed out that IF statement. See changes in RED
ALTER TRIGGER trgSR_CheckAndAddSampleRequest_Mill1
ON NYS1ReHeat FOR UPDATE
AS
BEGIN
DECLARE @nHeat AS INT,
@nRollMillBarID AS INT,
@sProduct AS VARCHAR(20),
@nSRCount AS INT,
@sLocation AS VARCHAR(20)

-- SELECT @nHeat = Heat, @nRollMillBarID = LocSeq, @sProduct = FinalProd, @sLocation = Location FROM INSERTED
-- REMOVED. This only handled one row not multiple row


-- IF (@sLocation = 'REHEAT')
-- BEGIN
-- IF NOT EXISTS (SELECT * FROM Physicals WHERE Heat = @nHeat AND SQLProduct = @sProduct)

IF NOT EXISTS (SELECT * FROM Physicals p inner join inserted i
on p.Heat = i.Heat
and p.SQLProduct = i.FinalProd
and i.Location = 'REHEAT')
BEGIN
-- SELECT @nSRCount = COUNT(*)
-- FROM SampleRequest SR INNER JOIN NYS1Reheat Reheat
-- ON SR.RollMillBarID = Reheat.LocSeq
-- WHERE Reheat.Heat = @nHeat
-- AND Reheat.FinalProd = @sProduct
-- AND (SR.SampleRequest = 1 OR SR.SampleCut=1)

SELECT @nSRCount = COUNT(*)
FROM inserted i inner join NYS1Reheat Reheat
on i.Heat = Reheat.Heat
and i.FinalProd = Reheat.FinalProd
inner join SampleRequest SR
on SR.RollMillBarID = Reheat.LocSeq
WHERE (SR.SampleRequest = 1 OR SR.SampleCut = 1)
AND i.Location = 'REHEAT'

IF (@nSRCount < 2)
BEGIN
-- INSERT INTO SampleRequest(RollMillBarID, SampleRequest, SampleCut, AutoRequest) VALUES(@nRollMillBarID, 1, 0, 1)
INSERT INTO SampleRequest(RollMillBarID, SampleRequest, SampleCut, AutoRequest)
SELECT i.LocSeq, 1, 0, 1
FROM inserted i
WHERE i.Location = 'REHEAT')
END
END
-- END
END


----------------------------------
'KH'


Go to Top of Page

Amjath
Yak Posting Veteran

66 Posts

Posted - 2006-02-01 : 04:59:56
hai khtan

then y u used like this
INSERT INTO SampleRequest(RollMillBarID, SampleRequest, SampleCut, AutoRequest)
SELECT i.LocSeq, 1, 0, 1 FROM inserted

i think the same think i used in the above statement like this

SELECT @LocSeq = LocSeq FROM inserted

wats the difference. sorry for the inconvience, and also the stupid ?????(question)

With Regards
-Amjath
Go to Top of Page

Amjath
Yak Posting Veteran

66 Posts

Posted - 2006-02-01 : 05:05:30
hai

i think the inserted table conatin only the current value which is going to be insert.
am i right.

if not, ok its my time to learn

Then when will be the inserted table get cleared.

my next query is

i think this inserted table created for every trigger is that right

Plz explain abt inserted table.


With Regards
-Amjath
Go to Top of Page

Amjath
Yak Posting Veteran

66 Posts

Posted - 2006-02-01 : 05:34:23
hai khtan,

is this correct or not, plz help me

ALTER TRIGGER trgSR_CheckAndAddSampleRequest_Mill1
ON NYS1ReHeat FOR UPDATE
AS
BEGIN
DECLARE @nHeat AS INT,
@nRollMillBarID AS INT,
@sProduct AS VARCHAR(20),
@nSRCount AS INT,
@sLocation AS VARCHAR(20)

IF UPDATE(Location)
BEGIN
SELECT @nRollMillBarID = ins.LocSeq FROM INSERTED ins WHERE ins.Location = 'REHEAT'
IF NOT EXISTS (SELECT * FROM SampleRequest WHERE RollMillBarID = @nRollMillBarID)
IF NOT EXISTS (SELECT * FROM Physicals phy INNER JOIN Inserted ins ON phy.Heat = ins.Heat
AND phy.SQLProduct = ins.FinalProd AND ins.Location = 'REHEAT')
BEGIN
SELECT @nSRCount = COUNT(*)
FROM Inserted ins INNER JOIN NYS1Reheat Reheat
ON ins.Heat = Reheat.Heat
AND ins.FinalProd = Reheat.FinalProd
INNER JOIN SampleRequest SR
ON SR.RollMillBarID = Reheat.LocSeq
WHERE (SR.SampleRequest = 1 OR SR.SampleCut = 1)
AND ins.Location = 'REHEAT'

IF (@nSRCount < 2)
BEGIN
INSERT INTO SampleRequest(RollMillBarID, SampleRequest, SampleCut, AutoRequest)
SELECT ins.LocSeq, 1, 0, 1
FROM inserted ins
WHERE ins.Location = 'REHEAT'
END
END
END
END
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-02-01 : 06:49:51
quote:
INSERT INTO SampleRequest(RollMillBarID, SampleRequest, SampleCut, AutoRequest)
SELECT i.LocSeq, 1, 0, 1 FROM inserted

i think the same think i used in the above statement like this

SELECT @LocSeq = LocSeq FROM inserted

The inserted table contains multiple rows of records being inserted. Your code will not be able to handle this as @LocSeq will only contain the value of one of the rows.

quote:
Then when will be the inserted table get cleared.

The inserted table only exists within the lifetime of the insert or update trigger.

quote:
i think this inserted table created for every trigger is that right

Only for INSERT and UPDATE trigger. Not available in DELETE trigger.

quote:
i think the inserted table conatin only the current value which is going to be insert

From Books OnLine
quote:
The inserted table stores copies of the affected rows during INSERT and UPDATE statements. During an insert or update transaction, new rows are added simultaneously to both the inserted table and the trigger table. The rows in the inserted table are copies of the new rows in the trigger table.

Refer to Books OnLine for more details on inserted as well as deleted table.




----------------------------------
'KH'


Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-02-01 : 06:59:54
You can remove most of the unused variable declared.
There is only one point that i would want to point out which is the highlighted in blue. The @nSRCount < 2 is it suppose to be per one record inserted into NYS1ReHeat ? Remember that the inserted table might contain multiple record. So your count(*) might be wrong.

ALTER TRIGGER trgSR_CheckAndAddSampleRequest_Mill1
ON NYS1ReHeat FOR UPDATE
AS
BEGIN
DECLARE @nHeat AS INT,
@nRollMillBarID AS INT,
@sProduct AS VARCHAR(20),
@nSRCount AS INT,
@sLocation AS VARCHAR(20)

IF UPDATE(Location)
BEGIN
SELECT @nRollMillBarID = ins.LocSeq FROM INSERTED ins WHERE ins.Location = 'REHEAT'
IF NOT EXISTS (SELECT * FROM SampleRequest WHERE RollMillBarID = @nRollMillBarID)
IF NOT EXISTS (SELECT * FROM Physicals phy
INNER JOIN Inserted ins
ON phy.Heat = ins.Heat
AND phy.SQLProduct = ins.FinalProd
-- AND ins.Location = 'REHEAT')
WHERE ins.Location = 'REHEAT') -- Preferable joining condition in ON and other in WHERE
BEGIN
SELECT @nSRCount = COUNT(*)
FROM Inserted ins
INNER JOIN NYS1Reheat Reheat
ON ins.Heat = Reheat.Heat
AND ins.FinalProd = Reheat.FinalProd
INNER JOIN SampleRequest SR
ON SR.RollMillBarID = Reheat.LocSeq
WHERE (SR.SampleRequest = 1 OR SR.SampleCut = 1)
AND ins.Location = 'REHEAT'

IF (@nSRCount < 2)
BEGIN
INSERT INTO SampleRequest(RollMillBarID, SampleRequest, SampleCut, AutoRequest)
SELECT ins.LocSeq, 1, 0, 1
FROM inserted ins
WHERE ins.Location = 'REHEAT'
END
END
END
END


----------------------------------
'KH'


Go to Top of Page

Amjath
Yak Posting Veteran

66 Posts

Posted - 2006-02-01 : 07:21:47
hai khtan,

i have one Query.

maximum number of rows in the instered table is 2(b4 update qnd after update) ok upto my knowledge.

so there is no problem when taking count with where contion of Locatio ok.

Why u strike the Select @LocSeq value, i define that column as primary key to avoid voilation i do like this. because if voilation occurs then the update for Main table will not reflect. am i do this

or any suggestions.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-02-02 : 00:45:16
quote:
maximum number of rows in the instered table is 2

You mean the maximum number of record for SampleRequest is 2 for each record in NYS1ReHeat ?

What is the primary key for table SampleRequest ?




----------------------------------
'KH'


Go to Top of Page

Amjath
Yak Posting Veteran

66 Posts

Posted - 2006-02-02 : 01:16:52
i set primary key on rollmillbarid but now i removed that one for primary key violation.

during primary key violation, the row in nys1reheat didnt get updated. ok,

The Inserted table is virtual table which get created at the time of trigger, for update it contain the information of the affected row and the new row (totally 2 rows in Inserted table) is it correct.
Go to Top of Page
    Next Page

- Advertisement -