1

Is it possible to add default constraint with synonym or UDF from other database?

Create table TestTable ( ID int identity(1,1), SData varchar(100), UserName varchar(100) default [OtherDatabaseName].dbo.fn_Test('value')) 

Below is the error message, while I am trying to add default constraint

The name "OtherDatabaseName" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted.

We can add UDF from same database but I want add UDF from other database. Because it is used in multiple databases on same server.

And I do not want to create that UDF in all database and prevent duplicate code.

Please let me know if there is any other/better way. Thank you.

6
  • I'm assuming you're asking this because it doesn't work? Commented Jul 31, 2017 at 18:08
  • Yes it does not work with function from different database? Commented Jul 31, 2017 at 18:13
  • What did the error message say? Commented Jul 31, 2017 at 18:38
  • Added error message in question. Commented Jul 31, 2017 at 18:56
  • I've been looking for an official reference where it sais you can or cannot do it, but they don't mention this case. But apparantly SQL Server doesn't support it, hence the error message. Have you tried with a synonym as well? Commented Jul 31, 2017 at 19:02

1 Answer 1

1

Please let me know if there is any other/better way.

One other way would be to use a trigger instead of a default.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.