Table types declared in different databases, even if they share the same name and structure, are not treated as being the same by SQL Server:
create database DB1
go
use DB1
go
create type TT1 as table (ID int not null)
go
create procedure Echo
@T TT1 readonly
as
select * from @T
go
create database DB2
go
use DB2
go
create type TT1 as table (ID int not null)
go
declare @T TT1
insert into @T(ID) values (1)
exec DB1..Echo @T
Result:
(1 row(s) affected)
Msg 206, Level 16, State 2, Procedure Echo, Line 0
Operand type clash: TT1 is incompatible with TT1
So far as I'm aware, there is no way to declare a variable in a database using a table type definition from another database. (e.g. anywhere where you see a user defined table type can be used, it can only be named as <table type>
or <schema>.<table type>
. 3 or 4 part names are not allowed)
(The above is true for 2008 and 2012; obviously, future versions may do something to address this)
As a work-around, you can do it the "poor man's" way - have your master defined stored procedure work against a temp table, rather than a user defined table type:
use Master
go
create procedure sp_Echo
as
select DB_NAME(),* from #t
go
create database DB1
go
use DB1
go
create table #t (ID int not null)
insert into #t (ID) values (1),(2)
exec sp_Echo
Results:
------------- -----------
DB1 1
DB1 2