0

So, I added a default value for a column in my table. the default value is the result of a function. for example:

--drop table t2
create table t2
(
    id datetime2 default(dbo.idDT()),
    v nvarchar(50)
)

It's ok. But, if I need to use the same funcion in a another db? If I try to add the db, SQL gives me error.

--drop table t2
create table t2
(
    id datetime2 default(database.dbo.idDT()),
    v nvarchar(50)
)

The trick I used is create a function that recall the function in the db like this:

CREATE FUNCTION [dbo].[idDT2]()
RETURNS datetime2
AS
BEGIN   
    return dbo.iddt()
END

and then:

--drop table t2
create table t2
(
    id datetime2 default(dbo.idDT2()),
    v nvarchar(50)
)

but performance are not good (at least 2x slow). There is a better trick??

or, could be be possible to improve the preformance calling function?

elle0087
  • 840
  • 9
  • 23
  • Quite frankly - don't use "tricks" in the first place. This creates dependencies between databases and usually between environments that defy documentation (because no one creates it or reads it or maintains it). In turn that just leads to more work. – SMor Jul 09 '21 at 11:41
  • @Charlieface -> Synonyms are invalid in a schemabound object or a constraint expression – elle0087 Jul 09 '21 at 11:53
  • @SMor -> then? which solution? – elle0087 Jul 09 '21 at 11:56
  • Out of curiosity, what does the function do, and why does it need to be in a default constraint? – Charlieface Jul 09 '21 at 11:59
  • thank you for prev suggestion. i have a db with commons utility functions. this function is a clr that give a unique id. i can't and i dont't want to deploy for all databases all functions... – elle0087 Jul 09 '21 at 12:03
  • Is "Synonyms are invalid in a schemabound object or a constraint expression" the error mentioned in the question? If so, it should be edited into the question, rather than left in [comments](//stackoverflow.com/help/privileges/comment). – outis Feb 02 '22 at 07:34
  • Does this answer your question? [use function from another database in computed column](https://stackoverflow.com/q/19035020/90527) – outis Feb 03 '22 at 00:46

0 Answers0