select * FROM openquery (ORACLE, 'select SFAB002 FROM DSDATA.VIEW_MO WHERE SFAADOCNO = ''A05-30D-2007000632''' )
-------------------------------------------------------------------
下面是一个很好的example,可以直接拿来用。我也经常把它当一个template。
DECLARE @Sql VARCHAR(1000)
DECLARE @organizationID VARCHAR(10)
SELECT @organizationID = (SELECT ORGANIZATION_ID FROM MYORGS WHERE ORGANIZATION_NAME = 'MMT')
SET @Sql = 'SELECT * from tableName where organization_id ='+@organizationID
SET @Sql = 'SELECT * FROM OPENQUERY(LinkedServerName, ''' + REPLACE(@Sql, '''', '''''') + ''')'
EXEC(@Sql)
-------------------------------------------------------------------
针对一些提问,我再补充说明一下为神马会有那么多的单引号。
--如果我们不把执行语句放入字符串,那么语句应该是: SELECT * from tableName where name = 'dan' --但是,我们需要在Openquery中使用参数,所以,我们必须保留单引号。单变双实现字符转义。 SELECT * FROM OPENQUERY(BETSYVALVES,'SELECT * from tableName where name = ''dan''') --最后,因为,我们需要使用Exec去执行已经形成的Command string,把上述语句变成字符串语句。我们必须对''进行转义,所以'' 就变成了'''',这就是Replace的主要工作,最后语句成了: SELECT * FROM OPENQUERY(BETSYVALVES,'SELECT * from tableName where name = ''''dan''''')