How to set a default value for one column in SQL based on another column

后端 未结 8 775
耶瑟儿~
耶瑟儿~ 2021-01-02 00:12

I\'m working with an old SQL 2000 database and I don\'t have a whole lot of SQL experience under my belt. When a new row is added to one of my tables I need to assign a defa

8条回答
  •  滥情空心
    2021-01-02 00:28

    So, for example, in a TAG table (where tags are applied to posts) if you want to count one tag as another...but default to counting new tags as themselves, you would have a trigger like this:

    CREATE TRIGGER [dbo].[TR_Tag_Insert]
       ON  [dbo].[Tag]
       AFTER INSERT
    AS 
    BEGIN
       SET NOCOUNT ON;
    
       UPDATE dbo.Tag 
       SET [CountAs] = I.[ID]
       FROM INSERTED AS I
       WHERE I.[CountAs] IS NULL
       AND dbo.Tag.ID = I.ID
    END
    

提交回复
热议问题