Вопрос

I have a DEFAULT constraint created in the database, which I want to share among several tables, and I want to script an unique alter table statement for adding a field in that tables as follows:

ALTER TABLE MyTable ADD MyField NOT NULL DEFAULT(DF_EXISTING_DEFAULT)

The following error ocurrs:

Msg 128, Level 15, State 1, Line 1
The name "DF_EXISTING_DEFAULT" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted.

Notice that the default value is already defined in DF_EXISTING_DEFAULT, I don't want to write it again. How can I do it? Any reason I shouldn't do it? Is it a t-sql limitation?

Это было полезно?

Решение

You actually can do it but the mechanism to do so is deprecated so shouldn't be used.

You can CREATE DEFAULT object and then use sp_bindefault.

There is no non deprecated replacement. If the logic for the default is complex you could put it in a scalar UDF to avoid repeating yourself.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top