简体   繁体   English

使用AS400链接服务器将子查询传递到OpenQuery中

[英]Passing SubQuery into OpenQuery with AS400 Linked Server

I have a Linked Server to the AS400 here. 我在这里有一个到AS400的链接服务器。 I'm trying to pull data from a field, but it is pulling over a million records when I only need about 20k. 我试图从一个字段中提取数据,但是当我只需要约2万个记录时,它就提取了超过一百万条记录。

I have a list of IDs that I need, and I'm trying to figure out how to pass that list into the OpenQuery. 我有一个需要的ID列表,并且我试图找出如何将该列表传递到OpenQuery中。

Here is my OpenQuery: 这是我的OpenQuery:

SELECT * FROM OPENQUERY(AS400, '
    SELECT 
        IMITNO, IMITD1, IMITD2, IMMFNO, IMBMTP, IMDSCO 
      FROM AS400.APLUS2FLE.ITMST 
      WHERE IMDSCO != ''Y''
')

I want to add WHERE IMITNO IN (SELECT item_id FROM as400_item_scope) but the as400_item_scope table is on the SQL machine, not the AS400. 我想添加WHERE IMITNO IN (SELECT item_id FROM as400_item_scope)但是as400_item_scope表位于SQL机器上,而不是AS400上。 I looked at several examples and I can pass in a single variable, but I don't understand how to pass in a list/query like this. 我看了几个示例,可以传递一个变量,但是我不明白如何传递这样的列表/查询。

I'm using latest SSMS, but SQL Server Version is 2008 我正在使用最新的SSMS,但是SQL Server版本是2008

You can't as far as I know... 据我所知,你不可能...

Two options.. 两种选择

  1. Insert the results of SELECT item_id FROM as400_item_scope into a (temporary?) table on the IBM i and then reference that table in your openquery. SELECT item_id FROM as400_item_scope的结果插入IBM i上的(临时?)表中,然后在openquery中引用该表。
  2. build a comma delimited string with from the results of SELECT item_id FROM as400_item_scope and include that in your openquery string. 根据SELECT item_id FROM as400_item_scope的结果构建一个逗号分隔的字符串,并将其包含在您的openquery字符串中。

Depending on how many records are returned, you might run into issues with statement size trying to use option #2. 根据返回的记录数,您可能会遇到尝试使用选项#2的语句大小问题。 Db2 for IBM i supports SQL statement of up to 2,097,152 bytes... IBM i的db2支持最大2097152字节的SQL语句...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM