Bind a column default value to a function in SQL 2005

♀尐吖头ヾ 提交于 2019-11-28 00:49:16

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

IF someone wants to do it using the interface, typing

[dbo].[NEWDOC_Order]()

does the trick. You apparently need all brackets or it will reject your input.

Here's screen shots to do it through SQL Server Management Studio GUI:

  1. Right click on table and select Design

  1. Select DOC_Order column (or other column needing default) in the table's design view to see properties

  1. Update Default Value or Binding with function name with brackets like so:

Note: as Luk stated, all brackets are needed including the schema (dbo in this case).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!