9

I have a database that holds common functions that I use across multiple databases. One of these functions takes in a table as a parameter which is a user defined TYPE. I would like to know if there is a way to call this function from another database.

I tried to define the type in the other database like so:

DECLARE @bits as Common.dbo.Bits

However I received an error too many prefixes

I have tried adding the TYPE to each database, and then passing a table of that type to the function in the common database, but there I get an error of

Operand type clash: Bits is incompatible with Bits

Bauer
  • 317
  • 3
  • 11

2 Answers2

6

You can't. Even if you declare the type identically in two databases, they're not treated the same. And the DECLARE statement is only allowed a schema name and an object name, so there's no way to reference a type from another database.

See also this question for some possible work arounds (if you control both databases involved)

Community
  • 1
  • 1
Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
  • 3
    Unfortunately this is not the answer I wanted to hear. Thank you – Bauer Apr 23 '12 at 07:47
  • @Damien_The_Unbeliever Isn't this strange of Microsoft to do? It's kind of like saying that in C# you can't use a class from a file unless it's on a file inside your current directory? – Kulingar Jul 31 '13 at 23:24
  • @Kulingar - if you're working in two different assemblies, and you create two types with identical (full) names and members, you still can't substitute one for the other (directly). – Damien_The_Unbeliever Aug 01 '13 at 05:39
  • @Damien_The_Unbeliever Thanks for the answer that keeps on giving, this has been causing me some headaches the last hour. – conroyrw Apr 10 '15 at 14:45
5

According to Creating User-Defined Data Types:

If a user-defined data type is created in the model database, it exists in all new user-defined databases. However, if the data type is created in a user-defined database, the data type exists only in that user-defined database.

Vikram
  • 8,235
  • 33
  • 47
  • That's true about the Model database, but I don't think that they are the same type. Besides, I am working with existing databases. – Bauer Mar 06 '12 at 14:44