更新了特定列上的SQL Server触发器

时间:2016-11-29 11:18:54

标签: sql sql-server triggers

我有一个包含以下列的表:

ID | CLIENT_SYNCED_TIME  |  NAME  | DESCRIPTION | LM_TIME

当任何其他列值更新时,触发器会自动设置LM_TIME列。

但是,当LM_TIME列更新时,我希望触发器更新CLIENT_SYNCED_TIME .... NOT

我现在正在使用以下触发器,当任何列值发生更改时,它会更新LM_TIME

我只是想让触发器不要担心CLIENT_SYNCED_TIME列。我必须做出哪些修改才能达到这种效果?

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TRIGGER [dbo].[updateLM_TIME]
ON [dbo].[INSTITUTIONS]
AFTER INSERT, UPDATE 
AS
    UPDATE dbo.INSTITUTIONS
    SET lm_time = CONVERT(DATETIME, CONVERT(VARCHAR(20), GETDATE(), 120))
    WHERE ID IN (SELECT DISTINCT ID FROM Inserted)
GO

2 个答案:

答案 0 :(得分:3)

试试这个。

            USE [lms_db]
            GO

            SET ANSI_NULLS ON
            GO

            SET QUOTED_IDENTIFIER ON
            GO

            CREATE TRIGGER [dbo].[updateLM_TIME]
            ON [dbo].[INSTITUTIONS]

            AFTER INSERT, UPDATE 
            AS
                IF UPDATE(ID) or UPDATE(NAME) or UPDATE(DESCRIPTION)
                BEGIN


                UPDATE dbo.INSTITUTIONS
                SET lm_time = CONVERT(DATETIME, CONVERT(VARCHAR(20), GETDATE(), 120))
                WHERE ID IN (SELECT DISTINCT ID FROM Inserted)

             end

            AFTER INSERT, UPDATE 
            AS
                IF UPDATE(CLIENT_SYNCED_TIME) 

                 PRINT 'Not Updated';
                else                    
                BEGIN               
                UPDATE dbo.INSTITUTIONS
                SET lm_time = CONVERT(DATETIME, CONVERT(VARCHAR(20), GETDATE(), 120))
                WHERE ID IN (SELECT DISTINCT ID FROM Inserted)
                PRINT 'Updated';
             end

答案 1 :(得分:0)

感谢Bhosale先生

int main(int argc, char *argv[]) {
  char print[] = "--print";
  if(strcmp(argv[1], print) == 0) {
    solve_a_bunch_of_functions_and_print_without_user_input();
  }
  else {
    ask_for_user_input();
    then_solve_bunch_of_functions();
  }
}