Insert default value when parameter is null

后端 未结 16 2445
遥遥无期
遥遥无期 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 05:56

    The most succinct solution I could come up with is to follow the insert with an update for the column with the default:

    IF OBJECT_ID('tempdb..#mytest') IS NOT NULL DROP TABLE #mytest
    CREATE TABLE #mytest(f1 INT DEFAULT(1), f2 INT)
    INSERT INTO  #mytest(f1,f2) VALUES (NULL,2)
    INSERT INTO  #mytest(f1,f2) VALUES (3,3)
    
    UPDATE #mytest SET f1 = DEFAULT WHERE f1 IS NULL
    
    SELECT * FROM #mytest
    

提交回复
热议问题