views:

94

answers:

3

I have a Table Type defined in a database. It is used as a table-valued parameter in a stored procedure. I would like to call this procedure from another database, and in order to pass the parameter, I need to reference this defined type.

But when I do DECLARE @table dbOtherDatabase.dbo.TypeName , it tells me that The type name 'dbOtherDatabase.dbo.TypeName' contains more than the maximum number of prefixes. The maximum is 1.

How could I reference this table type?

A: 

Can you not just define the type in both databases.

Edit

See this article on how to do what you require

Ben Robinson
It doesn't work :( If I do so, I get "Operand type clash: TypeName is incompatible with TypeName"
Victor Rodrigues
I have updated my answer with a link to an msdn article that tells you how to do it.
Ben Robinson
This doesn't work. I think it is like @devio said, only CLR types can be shared between databases (your link doesn't say this exactly, but we can understand it from this phrase: "The types must have the same name, including the same CLR name, and must be implemented through the same assembly in both databases. "). If it is really this way, used-defined types seems to suck!
Victor Rodrigues
A: 

you could try using sp_executesql:

DECLARE @mylist integer_list_tbltype,
        @sql nvarchar(MAX)
SELECT  @sql = N'SELECT p.ProductID, p.ProductName
                 FROM    Northwind..Products p
                 WHERE   p.ProductID IN (SELECT n FROM @prodids)'
INSERT @mylist VALUES(9),(12),(27),(37)
EXEC sp_executesql @sql, N'@prodids integer_list_tbltype READONLY', @mylist

and if that doesn't work you may have to create a wrapper procedure in the remote DB, where you pass in a CSV string and the wrapper procedure splits it and creates the table (now using the local table type) to then pass into the actual procedure. See this answer for an explanation on how to split a CVS string.

KM
It could work, but unfortunately this is something we can't do here :P Usually this is a very bad coding practice, so we avoid very heavily using script that is not compiled. EDIT: and the problem really isn't calling the procedure. In fact, the issue is with type declaration.
Victor Rodrigues
A: 

Cross-database user-defined types seems to work only for CLR-based types. See this forum and MSDN (plus comments).

devio

related questions