0

我正在尝试执行此查询,但出现错误:

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 中选择 * 是注释

4

2 回答 2

2
  1. 使用 FMTONLY & NOCOUNT
  2. 只是为什么不使用表变量而不是 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
    */
    ')
    
于 2013-03-13T05:06:18.560 回答
0

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

于 2012-11-27T20:09:36.550 回答