Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 从多个不带笛卡尔积的表中选择_Sql Server_Join - Fatal编程技术网

Sql server 从多个不带笛卡尔积的表中选择

Sql server 从多个不带笛卡尔积的表中选择,sql-server,join,Sql Server,Join,我试图创建一个命令,将客户机成员身份的详细信息从一个数据库复制到另一个具有相同结构的数据库。为了回答这个问题,我把它简化为基本要素,所以要复制的四个项目是到期日、订阅ID、客户ID和组成订阅的服务项目ID。 客户端在两个基中都有一个通用的GUID。订阅ID是一个唯一的long int,它在两个基中应该相同,到期日期只是一个日期。到目前为止,很容易。棘手的是,每个数据库中的项目id不一定相同。我需要用where语句从一个映射到另一个,我知道怎么做 我的问题是,我需要从目标数据库自己的ITEM表IT

我试图创建一个命令,将客户机成员身份的详细信息从一个数据库复制到另一个具有相同结构的数据库。为了回答这个问题,我把它简化为基本要素,所以要复制的四个项目是到期日、订阅ID、客户ID和组成订阅的服务项目ID。 客户端在两个基中都有一个通用的GUID。订阅ID是一个唯一的long int,它在两个基中应该相同,到期日期只是一个日期。到目前为止,很容易。棘手的是,每个数据库中的项目id不一定相同。我需要用where语句从一个映射到另一个,我知道怎么做

我的问题是,我需要从目标数据库自己的ITEM表ITEM_0中进行选择,以获取并插入正确的ITEM_id,当我这样做时,返回了数千个重复的行。我假设我需要使用联接来避免这种情况,但是由于我没有任何有意义的东西来联接项0,所以我无法得到任何结果

insert into DestDB..subscription (expiry_date,id,client_id,item_id)
select 
    sub_1.expiry_date,
    sub_1.id,
    cli_0.id as client_id,
    item_0.id as item_id,
from SourceDB..subscription sub_1,
    DestDB..item item_0,
    DestDB..client cli_0
inner join SourceDB..client cli_1
   on cli_1.[guid] = cli_0.[guid] 
where sub_1.id not in (select id from DestDB..subscription)  
and item_0.id = 
     (select id from DestDB..collectiondetails 
             where service_ID =
              (select id from DestDB..service s_0 where s_0.code = 
                 (select code from SourceDB..service s_1 where s_1.id = 
                    (select service_ID from Source..collectiondetails item_1         where item_1.id = sub_1.item_id)))
             and collection_ID =
               (select id from DestDB..collection col_0
                  where col_0.code = 
                    (select code from SourceDB..collection col_1 where col_1.id =
                       (select collection_ID from SourceDB..collectiondetails item_1 where item_1.id = sub_1.collection_ID)))

     )         

恐怕最新的问题更令人困惑。这个Select in Where子句是保证只返回一条记录,还是应该是in而不是=

如果没有规则从匹配列表中标识特定的DestDB..item,那么顶部的也应该这样做。似乎仍然可以完全忽略第0项:

insert into DestDB..subscription (expiry_date,id,client_id,item_id)
select 
    sub_1.expiry_date,
    sub_1.id,
    cli_0.id as client_id,
    (select Top 1 id from DestDB..collectiondetails   --<- Limit to top 1 only
             where service_ID =
              (select id from DestDB..service s_0 where s_0.code = 
                 (select code from SourceDB..service s_1 where s_1.id = 
                    (select service_ID from SourceDB..collectiondetails item_1 where item_1.id = sub_1.item_id)))
             and collection_ID =
               (select id from DestDB..collection col_0
                  where col_0.code = 
                    (select code from SourceDB..collection col_1 where col_1.id =
                       (select collection_ID from SourceDB..collectiondetails item_1 where item_1.id = sub_1.collection_ID)))
     ) as item_id,
from SourceDB..subscription sub_1,
    DestDB..client cli_0
inner join SourceDB..client cli_1
   on cli_1.[guid] = cli_0.[guid] 
where sub_1.id not in (select id from DestDB..subscription)  
请注意:如果DestDB..item为空,则问题示例语句将不会插入任何内容,但此答案-会将item_id设置为NULL

我个人会尝试在一个事务中将此任务拆分为两个单独的语句:

插入到项目id为空的目标表中。 使用新的item_id更新目标表,其中item_id为空。 在找不到适当的项目id时,可选择删除不需要的记录。
恐怕最新的问题更令人困惑。这个Select in Where子句是保证只返回一条记录,还是应该是in而不是=

如果没有规则从匹配列表中标识特定的DestDB..item,那么顶部的也应该这样做。似乎仍然可以完全忽略第0项:

insert into DestDB..subscription (expiry_date,id,client_id,item_id)
select 
    sub_1.expiry_date,
    sub_1.id,
    cli_0.id as client_id,
    (select Top 1 id from DestDB..collectiondetails   --<- Limit to top 1 only
             where service_ID =
              (select id from DestDB..service s_0 where s_0.code = 
                 (select code from SourceDB..service s_1 where s_1.id = 
                    (select service_ID from SourceDB..collectiondetails item_1 where item_1.id = sub_1.item_id)))
             and collection_ID =
               (select id from DestDB..collection col_0
                  where col_0.code = 
                    (select code from SourceDB..collection col_1 where col_1.id =
                       (select collection_ID from SourceDB..collectiondetails item_1 where item_1.id = sub_1.collection_ID)))
     ) as item_id,
from SourceDB..subscription sub_1,
    DestDB..client cli_0
inner join SourceDB..client cli_1
   on cli_1.[guid] = cli_0.[guid] 
where sub_1.id not in (select id from DestDB..subscription)  
请注意:如果DestDB..item为空,则问题示例语句将不会插入任何内容,但此答案-会将item_id设置为NULL

我个人会尝试在一个事务中将此任务拆分为两个单独的语句:

插入到项目id为空的目标表中。 使用新的item_id更新目标表,其中item_id为空。 在找不到适当的项目id时,可选择删除不需要的记录。
表DestDB..item和DestDB..client是如何连接的,是否有FK?它们之间没有直接连接。两者都通过subscription.client_id和subscription.item_id连接到DestDB.subscription。如果您没有任何有意义的加入项目_0,您希望如何为给定订阅选择正确的项目。一定有什么事。否则你也可以选择一个随机项目。我不想给出完整的结构,因为它有点复杂,但我想我需要把它弄清楚一点!“项”是项集合的一部分,此集合存在于两个数据库中,具有单独的ID,但有一个公共代码字段。subscription表中的item_ID实际上是指其集合中的item的ID。因此item_ID 1告诉您查看集合表中的item 1,在那里可以找到指向item本身ID的链接。我在结尾的where声明中进行了这场比赛。我将用完整的信息来修改它。DestDB..item和DestDB..client表是如何连接的,是否有FK?它们之间并没有直接连接。两者都通过subscription.client_id和subscription.item_id连接到DestDB.subscription。如果您没有任何有意义的加入项目_0,您希望如何为给定订阅选择正确的项目。一定有什么事。否则你也可以选择一个随机项目。我不想给出完整的结构,因为它有点复杂,但我想我需要把它弄清楚一点!“项”是项集合的一部分,此集合存在于两个数据库中,具有单独的ID,但有一个公共代码字段。subscription表中的item_ID实际上是指其集合中的item的ID。因此item_ID 1告诉您查看集合表中的item 1,在那里可以找到指向item本身ID的链接。我在结尾的where声明中进行了这场比赛。我会用完整的信息来修正它。见上文。对不起,原来的问题含糊不清,我试图通过发布太多信息来限制混乱,但我意识到我不清楚。见上文。对不起,原来的问题含糊不清,我试图通过发布太多信息来限制混乱,但我意识到我不清楚。