Insert default value when parameter is null

后端 未结 16 1971
遥遥无期
遥遥无期 2020-12-24 05:27

I have a table that has a column with a default value:

create table t (
    value varchar(50) default (\'something\')
)

I\'m using a stored

16条回答
  •  隐瞒了意图╮
    2020-12-24 06:09

    The best option by far is to create an INSTEAD OF INSERT trigger for your table, removing the default values from your table, and moving them into the trigger.

    This will look like the following:

    create trigger dbo.OnInsertIntoT
    ON TablenameT
    INSTEAD OF INSERT
    AS
    insert into TablenameT
    select
       IsNull(column1 ,)
      ,IsNull(column2 ,)
      ...
    from inserted
    

    This makes it work NO MATTER what code tries to insert NULLs into your table, avoids stored procedures, is completely transparent, and you only need to maintain your default values in one place, namely this trigger.

提交回复
热议问题