请问用户定义表类型能跨数据库使用吗?

TIGER0579 2019-06-27 09:07:36
use r5
create type t_table_type as table
(
id int,
name varchar(32),
sex varchar(2)
)

执行下列语句会报错:类型 名称 'r5.dbo.t_table_type' 包含的前缀超出了最大限值。最多只能有 1 个。

use r6
declare @a1 as r5.dbo.t_table_type

这个该怎么调用?
...全文
180 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
唐诗三百首 2019-06-27
  • 打赏
  • 举报
回复

use r5  


-- 建存储过程
create proc dbo.Call_r6test1
(@z1 as t_table_type readonly) 
as
begin
 if object_id('tempdb..#z1') is not null
    drop table #z1

 select * into #z1 from @z1

 declare @tsql nvarchar(max)
 
 select @tsql=N'use r6;
                declare @z1 as t_table_type;
                insert into @z1 select * from #z1;
                exec test1 @z1;'

 exec(@tsql)
end


-- 测试
declare @z1 AS t_table_type

insert into @z1
 select 1,'2','3' union all
 select 4,'5','6'

exec dbo.Call_r6test1 @z1

/*
id          name                             sex
----------- -------------------------------- ----
1           2                                3
4           5                                6

(2 行受影响)
*/
TIGER0579 2019-06-27
  • 打赏
  • 举报
回复
use r6
create type t_table_type as table
(
id int,
name varchar(32),
sex varchar(2)
)
go
create proc test1(@z1 AS t_table_type READONLY) as
select * from @z1
return

use r5
create type t_table_type as table
(
id int,
name varchar(32),
sex varchar(2)
)
declare @z1 AS t_table_type
exec r6.dbo.test1 @z1

--操作数类型冲突: t_table_type 与 t_table_type 不兼容
唐诗三百首 2019-06-27
  • 打赏
  • 举报
回复
请把r6数据库里的这个存储过程的代码 和 r5中调用的代码贴一下?
TIGER0579 2019-06-27
  • 打赏
  • 举报
回复
这种跨数据库且带表参数的存储过程该怎么调用?
TIGER0579 2019-06-27
  • 打赏
  • 举报
回复
r6数据库里有个存储过程也有t_table_type, 但在r5中调用这个存储过程会出错:操作数类型冲突: t_table_type 与 t_table_type 不兼容, 2个用户定义表类型结构完全一样.
TIGER0579 2019-06-27
  • 打赏
  • 举报
回复
r6数据库里有个存储过程也有t_table_type, 但在r5中调用这个存储过程会出错:操作数类型冲突: t_table_type 与 t_table_type 不兼容, 2个用户定义表类型结果完全一样.
唐诗三百首 2019-06-27
  • 打赏
  • 举报
回复
table type无法跨库使用, 需在r6数据库里也定义t_table_type.

34,588

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧