Friday, February 24, 2012

Default values for a column - From a function

I am trying to set the Default value for a table column to a user defined function . Is this possible?

I have a Function that generates a RandomID as a string and returns that string. I would like that value in the Column to which I am calling the function as the default value.

Thanks

Jawahar

BOL 2000 : "Only a constant value, such as a character string; a system function, such as SYSTEM_USER(); or NULL can be used as a default"|||

Is it true for SQl Server 2005? If not, can you please point me to a sample of using UDF as a default value for a column?

Thanks in advance.

|||

You can use a user-defined function as default but it cannot take any parameters (meaning you can't pass the any column value). Additionally, this will get evaluated only once per statement (INSERT/UPDATE) not for every row.

So can you please explain what you are trying to do by calling a UDF with non-deterministic behavior?

|||

I see, thanks a lot. It actually was not my question, I just found this question on another forum and then I tried to make some research. When I found this thread I decided to post this question for clarification.

You're saying, that it would work, but it would be the same value for all inserted records, correct? Because I'm not sure if default value for the column comes into play on UPDATE.

Thanks again for your help and if you have any more information, I would appreciate it.

No comments:

Post a Comment