Bind a column default value to a function in SQL 2005

后端 未结 3 485
青春惊慌失措
青春惊慌失措 2020-12-06 16:51

I have a column containing items that can be sorted by the user:

DOC_ID  DOC_Order DOC_Name
   1       1        aaa
   2       3        bbb
   3       2              


        
3条回答
  •  失恋的感觉
    2020-12-06 17:34

    The syntax to add a default like that would be

    alter table DOC_Order 
    add constraint 
    df_DOC_Order 
    default([dbo].[NEWDOC_Order]())
    for DOC_Order
    

    Also, you might want to alter your function to handle when DOC_Order is null

    Create FUNCTION [dbo].[NEWDOC_Order] 
    (
    )
    RETURNS int
    AS
    BEGIN
    
    RETURN (SELECT ISNULL(MAX(DOC_ORDER),0) + 1 FROM DOC_Documents)
    
    END
    

提交回复
热议问题