(TRIGGER) if cell1 = 'value' THEN INSERT INTO table (date) VALUES (CURRENT_TIMESTAMP)











up vote
0
down vote

favorite












I need to create a trigger in SQL Server that triggers every time that the value from column "status" = 'Baja'



CREATE TRIGGER trg_triggerName 
ON dbo.table1
AFTER UPDATE
AS
BEGIN
IF status = 'Baja' THEN BEGIN
INSERT INTO dbo.table1 (fechaBaja)
VALUES (CURRENT_TIMESTAMP)
END
END
GO


I got this error message




Msg 207, Level 16, State 1, Procedure trg_FechaBaja, Line 3 [Batch Start Line 34]

Invalid column name 'status'.




     IF status = 'Baja' THEN BEGIN


In this line the "status" give me the message "invalid column name 'status'" and I'm 100% sure that my column has that name.



IN RESUME: Got a table named table1 that has a column named 'status' and another column named 'fechaBaja'



Every time that the 'status' value changes to 'Baja', I need to trigger and update the cell 'fechaBaja' with the current_timestamp



All the operations are in the same table1.










share|improve this question
























  • Yes you have that column, but where and which row, INSERTED table? DELETED table? Table1? You need to check INSERTED table there. Please explain what are you trying to do there
    – Sami
    Nov 19 at 18:58












  • also status is a reserved word... so it's best to enclose it in brackets or not use it as a column name at all
    – scsimon
    Nov 19 at 19:00










  • hi @Sami, it is on table1, all the operation in fact. about the row, does this mean that everytime I perform this operation all the rows could be affected ?
    – HiramL.
    Nov 19 at 19:02










  • @HiramL. Since your trigger is fired AFTER UPDATE you need to check INSERTED table not dbo.table1, and also you need to change you code to handle multiple rows. I suggest that you edit your question and add the DDL of your table and explain what are trying to do there
    – Sami
    Nov 19 at 19:06

















up vote
0
down vote

favorite












I need to create a trigger in SQL Server that triggers every time that the value from column "status" = 'Baja'



CREATE TRIGGER trg_triggerName 
ON dbo.table1
AFTER UPDATE
AS
BEGIN
IF status = 'Baja' THEN BEGIN
INSERT INTO dbo.table1 (fechaBaja)
VALUES (CURRENT_TIMESTAMP)
END
END
GO


I got this error message




Msg 207, Level 16, State 1, Procedure trg_FechaBaja, Line 3 [Batch Start Line 34]

Invalid column name 'status'.




     IF status = 'Baja' THEN BEGIN


In this line the "status" give me the message "invalid column name 'status'" and I'm 100% sure that my column has that name.



IN RESUME: Got a table named table1 that has a column named 'status' and another column named 'fechaBaja'



Every time that the 'status' value changes to 'Baja', I need to trigger and update the cell 'fechaBaja' with the current_timestamp



All the operations are in the same table1.










share|improve this question
























  • Yes you have that column, but where and which row, INSERTED table? DELETED table? Table1? You need to check INSERTED table there. Please explain what are you trying to do there
    – Sami
    Nov 19 at 18:58












  • also status is a reserved word... so it's best to enclose it in brackets or not use it as a column name at all
    – scsimon
    Nov 19 at 19:00










  • hi @Sami, it is on table1, all the operation in fact. about the row, does this mean that everytime I perform this operation all the rows could be affected ?
    – HiramL.
    Nov 19 at 19:02










  • @HiramL. Since your trigger is fired AFTER UPDATE you need to check INSERTED table not dbo.table1, and also you need to change you code to handle multiple rows. I suggest that you edit your question and add the DDL of your table and explain what are trying to do there
    – Sami
    Nov 19 at 19:06















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I need to create a trigger in SQL Server that triggers every time that the value from column "status" = 'Baja'



CREATE TRIGGER trg_triggerName 
ON dbo.table1
AFTER UPDATE
AS
BEGIN
IF status = 'Baja' THEN BEGIN
INSERT INTO dbo.table1 (fechaBaja)
VALUES (CURRENT_TIMESTAMP)
END
END
GO


I got this error message




Msg 207, Level 16, State 1, Procedure trg_FechaBaja, Line 3 [Batch Start Line 34]

Invalid column name 'status'.




     IF status = 'Baja' THEN BEGIN


In this line the "status" give me the message "invalid column name 'status'" and I'm 100% sure that my column has that name.



IN RESUME: Got a table named table1 that has a column named 'status' and another column named 'fechaBaja'



Every time that the 'status' value changes to 'Baja', I need to trigger and update the cell 'fechaBaja' with the current_timestamp



All the operations are in the same table1.










share|improve this question















I need to create a trigger in SQL Server that triggers every time that the value from column "status" = 'Baja'



CREATE TRIGGER trg_triggerName 
ON dbo.table1
AFTER UPDATE
AS
BEGIN
IF status = 'Baja' THEN BEGIN
INSERT INTO dbo.table1 (fechaBaja)
VALUES (CURRENT_TIMESTAMP)
END
END
GO


I got this error message




Msg 207, Level 16, State 1, Procedure trg_FechaBaja, Line 3 [Batch Start Line 34]

Invalid column name 'status'.




     IF status = 'Baja' THEN BEGIN


In this line the "status" give me the message "invalid column name 'status'" and I'm 100% sure that my column has that name.



IN RESUME: Got a table named table1 that has a column named 'status' and another column named 'fechaBaja'



Every time that the 'status' value changes to 'Baja', I need to trigger and update the cell 'fechaBaja' with the current_timestamp



All the operations are in the same table1.







sql-server triggers database-trigger






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 19:36









marc_s

568k12810991249




568k12810991249










asked Nov 19 at 18:55









HiramL.

54




54












  • Yes you have that column, but where and which row, INSERTED table? DELETED table? Table1? You need to check INSERTED table there. Please explain what are you trying to do there
    – Sami
    Nov 19 at 18:58












  • also status is a reserved word... so it's best to enclose it in brackets or not use it as a column name at all
    – scsimon
    Nov 19 at 19:00










  • hi @Sami, it is on table1, all the operation in fact. about the row, does this mean that everytime I perform this operation all the rows could be affected ?
    – HiramL.
    Nov 19 at 19:02










  • @HiramL. Since your trigger is fired AFTER UPDATE you need to check INSERTED table not dbo.table1, and also you need to change you code to handle multiple rows. I suggest that you edit your question and add the DDL of your table and explain what are trying to do there
    – Sami
    Nov 19 at 19:06




















  • Yes you have that column, but where and which row, INSERTED table? DELETED table? Table1? You need to check INSERTED table there. Please explain what are you trying to do there
    – Sami
    Nov 19 at 18:58












  • also status is a reserved word... so it's best to enclose it in brackets or not use it as a column name at all
    – scsimon
    Nov 19 at 19:00










  • hi @Sami, it is on table1, all the operation in fact. about the row, does this mean that everytime I perform this operation all the rows could be affected ?
    – HiramL.
    Nov 19 at 19:02










  • @HiramL. Since your trigger is fired AFTER UPDATE you need to check INSERTED table not dbo.table1, and also you need to change you code to handle multiple rows. I suggest that you edit your question and add the DDL of your table and explain what are trying to do there
    – Sami
    Nov 19 at 19:06


















Yes you have that column, but where and which row, INSERTED table? DELETED table? Table1? You need to check INSERTED table there. Please explain what are you trying to do there
– Sami
Nov 19 at 18:58






Yes you have that column, but where and which row, INSERTED table? DELETED table? Table1? You need to check INSERTED table there. Please explain what are you trying to do there
– Sami
Nov 19 at 18:58














also status is a reserved word... so it's best to enclose it in brackets or not use it as a column name at all
– scsimon
Nov 19 at 19:00




also status is a reserved word... so it's best to enclose it in brackets or not use it as a column name at all
– scsimon
Nov 19 at 19:00












hi @Sami, it is on table1, all the operation in fact. about the row, does this mean that everytime I perform this operation all the rows could be affected ?
– HiramL.
Nov 19 at 19:02




hi @Sami, it is on table1, all the operation in fact. about the row, does this mean that everytime I perform this operation all the rows could be affected ?
– HiramL.
Nov 19 at 19:02












@HiramL. Since your trigger is fired AFTER UPDATE you need to check INSERTED table not dbo.table1, and also you need to change you code to handle multiple rows. I suggest that you edit your question and add the DDL of your table and explain what are trying to do there
– Sami
Nov 19 at 19:06






@HiramL. Since your trigger is fired AFTER UPDATE you need to check INSERTED table not dbo.table1, and also you need to change you code to handle multiple rows. I suggest that you edit your question and add the DDL of your table and explain what are trying to do there
– Sami
Nov 19 at 19:06














3 Answers
3






active

oldest

votes

















up vote
0
down vote



accepted










I believe you want to update fechaBaja field if updated row status field equal'Baja'. You need to capture primary key information as well. I put 'Id' but you need to change it with your actuel field name.



 CREATE TRIGGER trg_triggerName ON dbo.table1 
AFTER UPDATE AS
BEGIN

UPDATE dbo.table1 SET fechaBaja=CURRENT_TIMESTAMP
WHERE EXISTS (SELECT 1 FROM INSERTED I WHERE dbo.table1.Id = I.Id AND I.Status='Baja')

END
GO





share|improve this answer



















  • 3




    What if there is more than one (1) row updated??
    – Sami
    Nov 19 at 19:10










  • you are right @Sami. I have updated the trigger to cover multiple update.
    – Zeki Gumus
    Nov 19 at 19:15










  • @ZekiGumus thank you for your help, this TRIGGER works perfectly, now I'm having trouble because I have another AFTER UPDATE trigger for this table that affects other column, but thanks man, this was I was looking for
    – HiramL.
    Nov 19 at 19:32


















up vote
1
down vote













First, you want an update, not an insert.



Second, A trigger in SQL Server is fired once per statement, not once per row. This means that if the update statement that fired the trigger have updated multiple rows, your trigger will be fired once, and include data about these rows in the inserted and deleted tables.



Third, You need to make sure that the update statement inside the trigger will not raise it again. Do that by configuring the database.



The code you need is something like this:



 CREATE TRIGGER trg_triggerName ON dbo.table1 
AFTER UPDATE AS
BEGIN

UPDATE t
SET fechaBaja = CURRENT_TIMESTAMP
FROM dbo.table1 As T
INNER JOIN Inserted As I
ON T.<PrimaryKey> = I.<PrimaryKey>
INNER JOIN Deleted As D
ON T.<PrimaryKey> = D.<PrimaryKey>
WHERE I.[status] = 'Baja'
AND (D.[Status] IS NULL OR D.[Status] <> 'Baja')

END
GO





share|improve this answer























  • You miss ) there. AND (D.[Status] IS NULL OR D.[Status] <> 'Baja')
    – Sami
    Nov 19 at 19:34










  • @sami thanks!..
    – Zohar Peled
    Nov 19 at 19:47


















up vote
0
down vote













You can do it by joining the three tables like



CREATE TRIGGER trg_triggerName ON dbo.table1 AFTER UPDATE AS
BEGIN
UPDATE T1
SET T1.fechaBaja = CURRENT_TIMESTAMP
FROM dbo.table1 T1 INNER JOIN INSERTED T2 ON T1.ID = T2.ID
INNER JOIN DELETED T3 ON T1.ID = T3.ID
WHERE T2.[status] = 'Baja'
AND
(T3.[status] <> 'Baja' OR T3.[status] IS NULL);
END
GO



  • There is no need to use IF.


  • status is a reserved word, enclose it in brackets.

  • Join your table with INSERTED table to get all the IDs of the rows updated.

  • Join your table with DELETED table to get all the IDs where the data is not 'Baja'.

  • Filter by where INSERTED.[status] = 'Baja' AND DELETED.[status] <> 'Baja' to update only those rows.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53380978%2ftrigger-if-cell1-value-then-insert-into-table-date-values-current-times%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    I believe you want to update fechaBaja field if updated row status field equal'Baja'. You need to capture primary key information as well. I put 'Id' but you need to change it with your actuel field name.



     CREATE TRIGGER trg_triggerName ON dbo.table1 
    AFTER UPDATE AS
    BEGIN

    UPDATE dbo.table1 SET fechaBaja=CURRENT_TIMESTAMP
    WHERE EXISTS (SELECT 1 FROM INSERTED I WHERE dbo.table1.Id = I.Id AND I.Status='Baja')

    END
    GO





    share|improve this answer



















    • 3




      What if there is more than one (1) row updated??
      – Sami
      Nov 19 at 19:10










    • you are right @Sami. I have updated the trigger to cover multiple update.
      – Zeki Gumus
      Nov 19 at 19:15










    • @ZekiGumus thank you for your help, this TRIGGER works perfectly, now I'm having trouble because I have another AFTER UPDATE trigger for this table that affects other column, but thanks man, this was I was looking for
      – HiramL.
      Nov 19 at 19:32















    up vote
    0
    down vote



    accepted










    I believe you want to update fechaBaja field if updated row status field equal'Baja'. You need to capture primary key information as well. I put 'Id' but you need to change it with your actuel field name.



     CREATE TRIGGER trg_triggerName ON dbo.table1 
    AFTER UPDATE AS
    BEGIN

    UPDATE dbo.table1 SET fechaBaja=CURRENT_TIMESTAMP
    WHERE EXISTS (SELECT 1 FROM INSERTED I WHERE dbo.table1.Id = I.Id AND I.Status='Baja')

    END
    GO





    share|improve this answer



















    • 3




      What if there is more than one (1) row updated??
      – Sami
      Nov 19 at 19:10










    • you are right @Sami. I have updated the trigger to cover multiple update.
      – Zeki Gumus
      Nov 19 at 19:15










    • @ZekiGumus thank you for your help, this TRIGGER works perfectly, now I'm having trouble because I have another AFTER UPDATE trigger for this table that affects other column, but thanks man, this was I was looking for
      – HiramL.
      Nov 19 at 19:32













    up vote
    0
    down vote



    accepted







    up vote
    0
    down vote



    accepted






    I believe you want to update fechaBaja field if updated row status field equal'Baja'. You need to capture primary key information as well. I put 'Id' but you need to change it with your actuel field name.



     CREATE TRIGGER trg_triggerName ON dbo.table1 
    AFTER UPDATE AS
    BEGIN

    UPDATE dbo.table1 SET fechaBaja=CURRENT_TIMESTAMP
    WHERE EXISTS (SELECT 1 FROM INSERTED I WHERE dbo.table1.Id = I.Id AND I.Status='Baja')

    END
    GO





    share|improve this answer














    I believe you want to update fechaBaja field if updated row status field equal'Baja'. You need to capture primary key information as well. I put 'Id' but you need to change it with your actuel field name.



     CREATE TRIGGER trg_triggerName ON dbo.table1 
    AFTER UPDATE AS
    BEGIN

    UPDATE dbo.table1 SET fechaBaja=CURRENT_TIMESTAMP
    WHERE EXISTS (SELECT 1 FROM INSERTED I WHERE dbo.table1.Id = I.Id AND I.Status='Baja')

    END
    GO






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 19 at 19:14

























    answered Nov 19 at 19:08









    Zeki Gumus

    1,033111




    1,033111








    • 3




      What if there is more than one (1) row updated??
      – Sami
      Nov 19 at 19:10










    • you are right @Sami. I have updated the trigger to cover multiple update.
      – Zeki Gumus
      Nov 19 at 19:15










    • @ZekiGumus thank you for your help, this TRIGGER works perfectly, now I'm having trouble because I have another AFTER UPDATE trigger for this table that affects other column, but thanks man, this was I was looking for
      – HiramL.
      Nov 19 at 19:32














    • 3




      What if there is more than one (1) row updated??
      – Sami
      Nov 19 at 19:10










    • you are right @Sami. I have updated the trigger to cover multiple update.
      – Zeki Gumus
      Nov 19 at 19:15










    • @ZekiGumus thank you for your help, this TRIGGER works perfectly, now I'm having trouble because I have another AFTER UPDATE trigger for this table that affects other column, but thanks man, this was I was looking for
      – HiramL.
      Nov 19 at 19:32








    3




    3




    What if there is more than one (1) row updated??
    – Sami
    Nov 19 at 19:10




    What if there is more than one (1) row updated??
    – Sami
    Nov 19 at 19:10












    you are right @Sami. I have updated the trigger to cover multiple update.
    – Zeki Gumus
    Nov 19 at 19:15




    you are right @Sami. I have updated the trigger to cover multiple update.
    – Zeki Gumus
    Nov 19 at 19:15












    @ZekiGumus thank you for your help, this TRIGGER works perfectly, now I'm having trouble because I have another AFTER UPDATE trigger for this table that affects other column, but thanks man, this was I was looking for
    – HiramL.
    Nov 19 at 19:32




    @ZekiGumus thank you for your help, this TRIGGER works perfectly, now I'm having trouble because I have another AFTER UPDATE trigger for this table that affects other column, but thanks man, this was I was looking for
    – HiramL.
    Nov 19 at 19:32












    up vote
    1
    down vote













    First, you want an update, not an insert.



    Second, A trigger in SQL Server is fired once per statement, not once per row. This means that if the update statement that fired the trigger have updated multiple rows, your trigger will be fired once, and include data about these rows in the inserted and deleted tables.



    Third, You need to make sure that the update statement inside the trigger will not raise it again. Do that by configuring the database.



    The code you need is something like this:



     CREATE TRIGGER trg_triggerName ON dbo.table1 
    AFTER UPDATE AS
    BEGIN

    UPDATE t
    SET fechaBaja = CURRENT_TIMESTAMP
    FROM dbo.table1 As T
    INNER JOIN Inserted As I
    ON T.<PrimaryKey> = I.<PrimaryKey>
    INNER JOIN Deleted As D
    ON T.<PrimaryKey> = D.<PrimaryKey>
    WHERE I.[status] = 'Baja'
    AND (D.[Status] IS NULL OR D.[Status] <> 'Baja')

    END
    GO





    share|improve this answer























    • You miss ) there. AND (D.[Status] IS NULL OR D.[Status] <> 'Baja')
      – Sami
      Nov 19 at 19:34










    • @sami thanks!..
      – Zohar Peled
      Nov 19 at 19:47















    up vote
    1
    down vote













    First, you want an update, not an insert.



    Second, A trigger in SQL Server is fired once per statement, not once per row. This means that if the update statement that fired the trigger have updated multiple rows, your trigger will be fired once, and include data about these rows in the inserted and deleted tables.



    Third, You need to make sure that the update statement inside the trigger will not raise it again. Do that by configuring the database.



    The code you need is something like this:



     CREATE TRIGGER trg_triggerName ON dbo.table1 
    AFTER UPDATE AS
    BEGIN

    UPDATE t
    SET fechaBaja = CURRENT_TIMESTAMP
    FROM dbo.table1 As T
    INNER JOIN Inserted As I
    ON T.<PrimaryKey> = I.<PrimaryKey>
    INNER JOIN Deleted As D
    ON T.<PrimaryKey> = D.<PrimaryKey>
    WHERE I.[status] = 'Baja'
    AND (D.[Status] IS NULL OR D.[Status] <> 'Baja')

    END
    GO





    share|improve this answer























    • You miss ) there. AND (D.[Status] IS NULL OR D.[Status] <> 'Baja')
      – Sami
      Nov 19 at 19:34










    • @sami thanks!..
      – Zohar Peled
      Nov 19 at 19:47













    up vote
    1
    down vote










    up vote
    1
    down vote









    First, you want an update, not an insert.



    Second, A trigger in SQL Server is fired once per statement, not once per row. This means that if the update statement that fired the trigger have updated multiple rows, your trigger will be fired once, and include data about these rows in the inserted and deleted tables.



    Third, You need to make sure that the update statement inside the trigger will not raise it again. Do that by configuring the database.



    The code you need is something like this:



     CREATE TRIGGER trg_triggerName ON dbo.table1 
    AFTER UPDATE AS
    BEGIN

    UPDATE t
    SET fechaBaja = CURRENT_TIMESTAMP
    FROM dbo.table1 As T
    INNER JOIN Inserted As I
    ON T.<PrimaryKey> = I.<PrimaryKey>
    INNER JOIN Deleted As D
    ON T.<PrimaryKey> = D.<PrimaryKey>
    WHERE I.[status] = 'Baja'
    AND (D.[Status] IS NULL OR D.[Status] <> 'Baja')

    END
    GO





    share|improve this answer














    First, you want an update, not an insert.



    Second, A trigger in SQL Server is fired once per statement, not once per row. This means that if the update statement that fired the trigger have updated multiple rows, your trigger will be fired once, and include data about these rows in the inserted and deleted tables.



    Third, You need to make sure that the update statement inside the trigger will not raise it again. Do that by configuring the database.



    The code you need is something like this:



     CREATE TRIGGER trg_triggerName ON dbo.table1 
    AFTER UPDATE AS
    BEGIN

    UPDATE t
    SET fechaBaja = CURRENT_TIMESTAMP
    FROM dbo.table1 As T
    INNER JOIN Inserted As I
    ON T.<PrimaryKey> = I.<PrimaryKey>
    INNER JOIN Deleted As D
    ON T.<PrimaryKey> = D.<PrimaryKey>
    WHERE I.[status] = 'Baja'
    AND (D.[Status] IS NULL OR D.[Status] <> 'Baja')

    END
    GO






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 19 at 19:47

























    answered Nov 19 at 19:16









    Zohar Peled

    51.9k73173




    51.9k73173












    • You miss ) there. AND (D.[Status] IS NULL OR D.[Status] <> 'Baja')
      – Sami
      Nov 19 at 19:34










    • @sami thanks!..
      – Zohar Peled
      Nov 19 at 19:47


















    • You miss ) there. AND (D.[Status] IS NULL OR D.[Status] <> 'Baja')
      – Sami
      Nov 19 at 19:34










    • @sami thanks!..
      – Zohar Peled
      Nov 19 at 19:47
















    You miss ) there. AND (D.[Status] IS NULL OR D.[Status] <> 'Baja')
    – Sami
    Nov 19 at 19:34




    You miss ) there. AND (D.[Status] IS NULL OR D.[Status] <> 'Baja')
    – Sami
    Nov 19 at 19:34












    @sami thanks!..
    – Zohar Peled
    Nov 19 at 19:47




    @sami thanks!..
    – Zohar Peled
    Nov 19 at 19:47










    up vote
    0
    down vote













    You can do it by joining the three tables like



    CREATE TRIGGER trg_triggerName ON dbo.table1 AFTER UPDATE AS
    BEGIN
    UPDATE T1
    SET T1.fechaBaja = CURRENT_TIMESTAMP
    FROM dbo.table1 T1 INNER JOIN INSERTED T2 ON T1.ID = T2.ID
    INNER JOIN DELETED T3 ON T1.ID = T3.ID
    WHERE T2.[status] = 'Baja'
    AND
    (T3.[status] <> 'Baja' OR T3.[status] IS NULL);
    END
    GO



    • There is no need to use IF.


    • status is a reserved word, enclose it in brackets.

    • Join your table with INSERTED table to get all the IDs of the rows updated.

    • Join your table with DELETED table to get all the IDs where the data is not 'Baja'.

    • Filter by where INSERTED.[status] = 'Baja' AND DELETED.[status] <> 'Baja' to update only those rows.






    share|improve this answer



























      up vote
      0
      down vote













      You can do it by joining the three tables like



      CREATE TRIGGER trg_triggerName ON dbo.table1 AFTER UPDATE AS
      BEGIN
      UPDATE T1
      SET T1.fechaBaja = CURRENT_TIMESTAMP
      FROM dbo.table1 T1 INNER JOIN INSERTED T2 ON T1.ID = T2.ID
      INNER JOIN DELETED T3 ON T1.ID = T3.ID
      WHERE T2.[status] = 'Baja'
      AND
      (T3.[status] <> 'Baja' OR T3.[status] IS NULL);
      END
      GO



      • There is no need to use IF.


      • status is a reserved word, enclose it in brackets.

      • Join your table with INSERTED table to get all the IDs of the rows updated.

      • Join your table with DELETED table to get all the IDs where the data is not 'Baja'.

      • Filter by where INSERTED.[status] = 'Baja' AND DELETED.[status] <> 'Baja' to update only those rows.






      share|improve this answer

























        up vote
        0
        down vote










        up vote
        0
        down vote









        You can do it by joining the three tables like



        CREATE TRIGGER trg_triggerName ON dbo.table1 AFTER UPDATE AS
        BEGIN
        UPDATE T1
        SET T1.fechaBaja = CURRENT_TIMESTAMP
        FROM dbo.table1 T1 INNER JOIN INSERTED T2 ON T1.ID = T2.ID
        INNER JOIN DELETED T3 ON T1.ID = T3.ID
        WHERE T2.[status] = 'Baja'
        AND
        (T3.[status] <> 'Baja' OR T3.[status] IS NULL);
        END
        GO



        • There is no need to use IF.


        • status is a reserved word, enclose it in brackets.

        • Join your table with INSERTED table to get all the IDs of the rows updated.

        • Join your table with DELETED table to get all the IDs where the data is not 'Baja'.

        • Filter by where INSERTED.[status] = 'Baja' AND DELETED.[status] <> 'Baja' to update only those rows.






        share|improve this answer














        You can do it by joining the three tables like



        CREATE TRIGGER trg_triggerName ON dbo.table1 AFTER UPDATE AS
        BEGIN
        UPDATE T1
        SET T1.fechaBaja = CURRENT_TIMESTAMP
        FROM dbo.table1 T1 INNER JOIN INSERTED T2 ON T1.ID = T2.ID
        INNER JOIN DELETED T3 ON T1.ID = T3.ID
        WHERE T2.[status] = 'Baja'
        AND
        (T3.[status] <> 'Baja' OR T3.[status] IS NULL);
        END
        GO



        • There is no need to use IF.


        • status is a reserved word, enclose it in brackets.

        • Join your table with INSERTED table to get all the IDs of the rows updated.

        • Join your table with DELETED table to get all the IDs where the data is not 'Baja'.

        • Filter by where INSERTED.[status] = 'Baja' AND DELETED.[status] <> 'Baja' to update only those rows.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 19 at 19:43

























        answered Nov 19 at 19:14









        Sami

        7,08831038




        7,08831038






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53380978%2ftrigger-if-cell1-value-then-insert-into-table-date-values-current-times%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Ottavio Pratesi

            Tricia Helfer

            15 giugno