Trigger after update with 2 contions

laroussi derbel 41 Reputation points
2021-11-12T12:20:52.09+00:00

hello everyone ,
i have a question , i have a trigger that actually insert values from a table to a table every time that a conditon is meet

ALTER TRIGGER [dbo].[tinsertV1]

ON [dbo].[book1]

AFTER UPDATE

IF EXISTS (SELECT *
FROM inserted i
JOIN deleted d ON i.ConfigID = d.ConfigID
WHERE i.ConfigID = 251
AND d.Value = 0
AND i.Value = 1)
BEGIN
INSERT INTO OfenbuchVC1212_V10 ( Datum , Zeit, Temperatur , Oxidationszeit )
...
END

my question is , can i add another condition to this trigger ?
i tried this program but it didnt work

ALTER TRIGGER [dbo].[tinsertV1]

ON [dbo].[book1]

AFTER UPDATE

IF EXISTS (SELECT *
FROM inserted i
JOIN deleted d ON i.ConfigID = d.ConfigID
WHERE i.ConfigID = 251
AND d.Value = 0
AND i.Value = 1)

and EXISTS (SELECT *
FROM inserted i
JOIN deleted d ON i.ConfigID = d.ConfigID
WHERE i.ConfigID = 250
AND d.Value = 0
AND i.Value = 1)

BEGIN
INSERT INTO OfenbuchVC1212_V10 ( Datum , Zeit, Temperatur , Oxidationszeit )
...
END

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
14,488 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,690 questions
{count} votes

2 answers

Sort by: Most helpful
  1. CathyJi-MSFT 22,346 Reputation points Microsoft Vendor
    2021-11-15T07:04:16.297+00:00

    Hi @laroussi derbel ,

    Did you want to create a trigger when all conditions are met or when either one condition is met?

    Please also check if the similar thread SQL Server Trigger with multiple conditions could help you.

    0 comments No comments

  2. Tom Phillips 17,741 Reputation points
    2021-11-15T17:22:12.723+00:00

    Just to be clear.

    A trigger can contain almost any valid TSQL commands. Any criteria you can think of you can do.

    However, keep in mind a trigger runs after every UPDATE command. So the more complex your trigger, the slower the UPDATE will run.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.