有可能,在openrowset中创建一个temp #table吗?

时间:2012-11-27 15:38:50

标签: sql openrowset temp-tables

我正在尝试执行此查询,但我收到错误:

Msg 8180, Level 16, State 1, Line 1
Statement(s) could not be prepared.
Msg 208, Level 16, State 1, Line 11
Invalid object name '#test1'.

我的代码:

select * from openrowset ('SQLOLEDB','DRIVER={SQL Server};SERVER=10.12.131.58;UID=sa;PWD=desarrollo','



create table #test1
(
id int,
name1 varchar(50)
)

insert into #test1
select cliente,nomcli from opepte.dbo.clientes

select * from #test1

/*this is a example but in real query i need manipulate this information and return 
a resulset with few rows
*/


')

但是这个其他查询工作正常。

select * from openrowset ('SQLOLEDB','DRIVER={SQL Server};SERVER=10.12.131.58;UID=sa;PWD=desarrollo','



create table #test1
(
id int,
name1 varchar(50)
)

--insert into #test1
select cliente,nomcli from opepte.dbo.clientes

--select * from #test1

/*this is a example but in real query i need manipulate this information and return 
a resulset with few rows
*/


')

注意:插入#test1并从#test1中选择*是coment

2 个答案:

答案 0 :(得分:2)

  1. 使用FMTONLY& NOCOUNT
  2. 为什么不使用tabled-variable而不是temp?由于您通过此代码显式返回数据,因此没有人会再次使用您的临时表。
  3. 还要考虑更健壮和安全的提供者和连接字符串:'SQLNCLI','Server = localhost; Integrated Security = SSPI; Trusted_Connection = yes;'

    select * from openrowset ('SQLOLEDB','DRIVER={SQL Server};SERVER=10.12.131.58;UID=sa;PWD=desarrollo', N'
    
    SET FMTONLY OFF
    SET NOCOUNT ON
    
    declare @q int = 1
    
    declare @test1 table
    (
    id int,
    name1 varchar(50)
    )
    
    insert into @test1
    select 1,''q''
    
    insert into @test1
    select 1,''q''
    
    select * from @test1
    
    /*this is a example but in real query i need manipulate this information and return 
    a resulset with few rows
    */
    ')
    

答案 1 :(得分:0)

我认为你不能因为openquery / rowset接口非常有限。鉴于远程服务器是SQL服务器,您可以使用基于表的函数来提供所需的功能。否则,您可以使用远程执行存储过程或链接服务器来执行此操作。