Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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.

Can you not just define the type in both databases.

Edit

See this article on how to do what you require

While ago for this thread, but I was trying to do the same thing and was annoyed by the same restriction.

Don't declare an @ table but use a # table which can be called upon from exec string where the database can be switched to the one with table type.

e.g.

use dbA
create type ThisTableRecord as table (id int, value varchar(max))

go

create procedure ThisTableSave
    @ThisTable ThisTableRecord readonly
AS
begin
    select * from @thisTable
end

go

use dbB

go

create procedure ThatTableSave

as begin

   create table #thatTable (id int, value varchar(max))

   insert into #thatTable
   values   (1, 'killing')
        ,   (2, 'joke')
        ,   (3, 'the')
        ,   (4, 'damned')

   exec ('
use dbA
declare @thisTable thisTableRecord
insert into @thisTable select * from #thatTable
exec thisTableSave @thisTable
')

end

exec ThatTableSave
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top