我正在尝试调试应用程序问题,我需要比较两个存储过程的结果。从挖掘到这里我发现了OPENROWSET,当params可以硬编码时效果很好但是当param是一个表值参数时,我得到一个错误。这是一个简单的例子,可以重现我的问题。
create type mytype as table ( id int )
go
create procedure myproctvp
@mytype mytype readonly
as
select * from sys.objects
inner join @mytype on id = object_id
go
create procedure myproc
@p1 int
as
select * from sys.objects
where @p1 = object_id
go
SELECT * INTO #mine FROM OPENROWSET('SQLNCLI', 'Server=.;Trusted_Connection=yes',
'EXEC mydb.dbo.myproc 846')
go
SELECT * INTO #minetvp FROM OPENROWSET('SQLNCLI', 'Server=.;Trusted_Connection=yes;',
'use mydb; declare @tvp mytype; insert into @tvp values (345645); exec mydb.dbo.myproctvp @mytype=@tvp')
第一次查询的结果:
(0 row(s) affected)
第二个失败:
Msg 7357, Level 16, State 2, Line 2
Cannot process the object "use mydb; declare @tvp mytype; insert into @tvp values (345645); exec mydb.dbo.myproctvp @mytype=@tvp". The OLE DB provider "SQLNCLI10" for linked server "(null)" indicates that either the object has no columns or the current user does not have permissions on that object.
修改
这样做......
create proc doTVP as
declare @dt mytype
insert into @dt values (163)
exec myproctvp @dt
go
SELECT * INTO #minetvp FROM OPENROWSET('SQLNCLI', 'Server=.;Trusted_Connection=yes;',
'exec mydb.dbo.doTVP')
......也不起作用。我得到了同样的信息。
修改
采用这种方法更接近:
SELECT * INTO #minetvp FROM OPENROWSET('SQLNCLI', 'Server=.;Trusted_Connection=yes;',
'create type mydb..mytype as table (id int);
declare @tvp mytype;
insert into @tvp values (345645);
exec sp_executesql "mydb.dbo.myproctvp", "@tvp mytype readonly", @tvp')
现在我得到的错误是:
OLE DB provider "SQLNCLI10" for linked server "(null)" returned message "Deferred prepare could not be completed.".
Msg 8180, Level 16, State 1, Line 1
Statement(s) could not be prepared.
Msg 117, Level 15, State 2, Line 1
The type name 'mydb..mytype' contains more than the maximum number of prefixes. The maximum is 1.
有人有任何想法吗?