使用 UDF 作为列的默认值 [英] Using UDF for default value of a column

查看:38
本文介绍了使用 UDF 作为列的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 UDF,用于为列生成默认值.它工作得很好,但我想将另一个字段作为参数传递给函数.这可能吗?

I created a UDF that I am using to generate a default value for a column. It works great, but I want to pass another field as a parameter into the function. Is this possible?

例如,其中一个字段是 DealerID 字段,我想将 DealerID 字段的值传递到我的 UDF 中,因为我将使用它来计算新值.任何帮助将不胜感激!

For example, one of the fields is a DealerID field, and I want to pass in the value of the DealerID field into my UDF because I will use it to calculate the new value. Any help would be appreciated!

推荐答案

否,因为在知道 DealerID 之前(例如在 INSERT 上)需要默认值

No, because the default value will be needed before DealerID is known (eg on INSERT)

这意味着 SQL Server 并没有在插入时表中的值,只有在插入之后.因此,它不能作为默认的 UDF.

This means that SQL Server does not the value in the table at the time of insert, only after. Therefore, it can not a UDF for the default.

例如,多行插入怎么样,或者你有 NEWID() 默认值?

For example, what about a multiple row insert, or where you have NEWID() default?

现在,在 DealerID 上使用基本逻辑:如果是 GUID,为什么?这是一个内部的、非用户可读的值.

Now, using logic basic on DealerID: if it's GUID, why? It's an internal, non-user readable value.

如果你真的需要这个,你必须使用一个计算列作为基本"值,另一列作为实际"值和 ISNULL.

If you really need this, you'll have to use a computed column for the "base" value and another column for the "actual" value with ISNULL.

这篇关于使用 UDF 作为列的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆