在 OPENQUERY 中使用查询结果
我有一个链接到 Oracle 数据库的 SQL Server 2005 数据库。 我想要做的是运行一个查询,从中提取一些 ID 号,然后找出 Oracle 中的 ID 号。
所以我想获取此查询的结果:
SELECT pidm
FROM sql_server_table
并执行类似的操作来查询 Oracle 数据库(假设上一个查询的结果存储在 @pidms 中):
OPENQUERY(oracledb,
'
SELECT pidm
FROM table
WHERE pidm IN (' +
@pidms + ')')
GO
但我无法想出一个好的方法这。 我想我可以对类似于这两个的查询进行内部联接。 不幸的是,在有限的时间内需要提取大量记录,因此我认为这不是一个非常高效的选择。
有什么建议么? 理想情况下,我希望使用尽可能少的动态 SQL 来完成此操作。
I have a SQL Server 2005 database that is linked to an Oracle database. What I want to do is run a query to pull some ID numbers out of it, then find out which ones are in Oracle.
So I want to take the results of this query:
SELECT pidm
FROM sql_server_table
And do something like this to query the Oracle database (assuming that the results of the previous query are stored in @pidms):
OPENQUERY(oracledb,
'
SELECT pidm
FROM table
WHERE pidm IN (' +
@pidms + ')')
GO
But I'm having trouble thinking of a good way to do this. I suppose that I could do an inner join of queries similar to these two. Unfortunately, there are a lot of records to pull within a limited timeframe so I don't think that will be a very performant option to choose.
Any suggestions? I'd ideally like to do this with as little Dynamic SQL as possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
啊啊,pidms。 勾起了不好的回忆! :)
你可以进行连接,但你会这样做:
我不确定你是否可以编写一个 PL/SQL 过程来从 sql 中获取表变量......但也许......不,我对此表示怀疑。
Ahhhh, pidms. Brings back bad memories! :)
You could do the join, but you would do it like this:
I'm not sure if you could write a PL/SQL procedure that would take a table variable from sql...but maybe.....no, I doubt it.
将 openquery 结果存储在临时表中,然后在 SQL 表和临时表之间进行内部联接。
Store openquery results in a temp table, then do an inner join between the SQL table and the temp table.
我认为您不能进行连接,因为 OPENQUERY 需要纯字符串(如您上面所写)。
I don't think you can do a join since OPENQUERY requires a pure string (as you wrote above).
BG:实际上,通过 OpenQuery JOIN IN SQLServer 到 Oracle 是有效的,避免了 #tmp 表并允许在没有 Param* 的情况下 JOIN 到 SQL - 例如。
干杯,比尔·吉布斯
BG: Actually JOIN IN SQLServer to Oracle by OpenQuery works, avoiding #tmp table and allowing JOIN to SQL without Param* - ex.
Cheers, Bill Gibbs