openrowset的问题

本文探讨了使用SQL删除语句时遇到的问题,特别是在涉及到跨数据库操作时出现的语法错误及解决方法。通过对比不同形式的SQL查询语句,分析了正确的写法。
http://community.youkuaiyun.com/Expert/topic/3085/3085239.xml?temp=1.107424E-02  
已结帖,由于问题还在,重开一个再问!!!  

谢谢 zjcxc(邹建)和netcoder(朱二)两位的回复!  

问题还是存在的!!!!  

delete from dsfinger a  
where exists(  
select 1 from opendatasource( 'sqloledb ', 'Integrated Security=SSPI;Data Source=ch ').idverify.dbo.dsfinger where usid=a.usid)  
执行上面的命令时提示错误:[第 1 行: 'a ' 附近有语法错误。]  

select * from dsfinger a  
where exists(  
select 1 from opendatasource( 'sqloledb ', 'Integrated Security=SSPI;Data Source=ch ').idverify.dbo.dsfinger where usid=a.usid)  
执行上面的命令成功  

delete dsfinger from dsfinger a  
where exists(  
select 1 from opendatasource( 'sqloledb ', 'Integrated Security=SSPI;Data Source=ch ').idverify.dbo.dsfinger where usid=a.usid)  
执行上面的命令提示错误:[在 sysservers 中未能找到服务器 ' "idverify ". "dbo ". "dsfinger " '。请执行 sp_addlinkedserver 以将服务器添加到 sysservers。]  

select * from dsfinger a,(select * from opendatasource( 'sqloledb ', 'Integrated Security=SSPI;Data Source=ch ').idverify.dbo.dsfinger) as b  
where a.usid=b.usid  
执行上面的命令成功  

delete from dsfinger a,(select * from opendatasource( 'sqloledb ', 'Integrated Security=SSPI;Data Source=ch ').idverify.dbo.dsfinger) as b  
where a.usid=b.usid  

执行上面的命令时提示错误:[第 1 行: 'a ' 附近有语法错误。]




delete dsfinger from dsfinger a   
where exists(   
select 1 from opendatasource( 'sqloledb ', 'Integrated Security=SSPI;Data Source=ch ').idverify.dbo.dsfinger where usid=a.usid)   

OPENROWSET 是 SQL Server 提供的一个用于连接到外部数据源的函数,可以通过它来查询、导入、导出外部数据源的数据。OPENROWSET 语法如下: ``` OPENROWSET ( provider_name, init_string, { query | 'query' } ) ``` 其中,provider_name 是外部数据源的 OLE DB 提供程序名称,init_string 是用于初始化外部数据源连接的字符串,可以是一个连接字符串或者其他初始化选项,query 是要执行的查询语句。 如果要使用 OPENROWSET 函数查询外部数据源的数据,可以使用以下语法: ``` SELECT * FROM OPENROWSET('provider_name', 'init_string', 'SELECT * FROM external_table') ``` 其中,'provider_name' 为外部数据源的 OLE DB 提供程序名称,'init_string' 为连接字符串,'external_table' 为外部数据源的表名或查询语句。这样,就可以将外部数据源的数据查询到 SQL Server 中。 如果要使用 OPENROWSET 函数将 SQL Server 中的数据导出到外部数据源中,可以使用以下语法: ``` INSERT INTO OPENROWSET('provider_name', 'init_string', 'SELECT * FROM external_table') SELECT * FROM local_table ``` 其中,'provider_name' 为外部数据源的 OLE DB 提供程序名称,'init_string' 为连接字符串,'external_table' 为外部数据源的表名或查询语句,local_table 为 SQL Server 中的表名。这样,就可以将 SQL Server 中的数据导出到外部数据源中。 需要注意的是,使用 OPENROWSET 函数需要在 SQL Server 中启用 Ad Hoc 分布式查询,否则会出现以下错误: ``` Msg 15281, Level 16, State 1, Line 1 SQL Server blocked access to STATEMENT 'OpenRowset/OpenDatasource' of component 'Ad Hoc Distributed Queries' because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of 'Ad Hoc Distributed Queries' by using sp_configure. ``` 要启用 Ad Hoc 分布式查询,可以使用以下命令: ``` EXEC sp_configure 'show advanced options', 1; GO RECONFIGURE; GO EXEC sp_configure 'Ad Hoc Distributed Queries', 1; GO RECONFIGURE; GO ``` 启用后,可以使用 OPENROWSET 函数查询、导入、导出外部数据源的数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值