efysis efysis - 3 years ago 231
SQL Question

Set my function as Default

i have function and i want to create it as default

ALTER FUNCTION [dbo].[ModifiedBy]()
RETURNS varchar(50)
RETURN host_name()

I want to do something like this, but it doesnt work. is it possible?

create default default_modifiedBy AS dbo.ModifiedBy()

Eror is
User-defined functions, partition functions, and column references are not allowed in expressions in this context.

Answer Source

From the MSDN page for create default:

Any constant, built-in function, or mathematical expression can be used, except those that contain alias data types. User-defined functions cannot be used

Like M.Ali writes, you can use a user-defined function if you create a column-bound default constraint with alter table ... add constraint or create table ... (col1 default dbo.MyFunc());.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download