简体   繁体   English

将数据从SQL Server表复制到同一表

[英]Copy data from SQL Server table to same table

I have a database called Customer which has a table called CustDetails . 我有一个名为Customer的数据库,其中有一个名为CustDetails的表。 The data in the table is similar to below (I have removed some of it for brevity). 表中的数据与下面类似(为简洁起见,我已删除了其中的一些数据)。 ID is the PK. ID是PK。

CustDetails : 客户详情

ID  CustNo  Year  StatusId
--------------------------
1   1231    2015    1
2   1232    2015    2
3   1233    2015    2
4   1234    2014    1
5   1235    2014    2

I have another database called Claim on the same server, which has a table called ClaimDetails (don't ask me why the tables aren't in the same DB but the design decision was made before I came on board and isn't something I can change.) 我还有一个数据库,称为Claim在同一台服务器上,其中有一个表中调用ClaimDetails (不要问我为什么表是不是在同一个数据库,但设计作出决定之前,我在船上来了,是不是我可以换。)

The data is similar to below (again some of it removed for brevity) 数据类似于下面的内容(为简洁起见,其中一些也被删除了)

ClaimDetails : ClaimDetails

ID  ClaimNumber CustNo
----------------------
1   1           1231
2   2           1232
3   3           1236
4   4           1237

I have a few things to accomplish: 我要完成几件事:

  1. Copy all rows from CustDetails where year is 2015 and change the year to 2016 复制CustDetails中年份为2015的所有行, CustDetails年份更改为2016
  2. If status in the result from part 1 is 2 set it to 1 for 2016 如果第1部分的结果中的状态为2,则将其设置为2016年的1
  3. Create a negative list from to find CustNo 's which are in the ClaimDetails table but are not in the CustDetails table - for these create a new row in the CustDetails table and set the status to 3 and the year to 2016. 从创建找否定列表CustNo的这些都是ClaimDetails表,但不是在CustDetails表-这些创造了一个新的行CustDetails表和状态,以3年制定到2016年。

So for the data I have listed above the end result I want to achieve is: 因此,对于我上面列出的数据,我想要获得的最终结果是:

CustDetails : 客户详情

    ID  CustNo  Year  StatusId
    --------------------------
    1   1231    2015    1
    2   1232    2015    2
    3   1233    2015    2
    4   1234    2014    1
    5   1235    2014    2
    6   1231    2016    1
    7   1232    2016    1
    8   1233    2016    1
    9   1236    2016    3
    10  1237    2016    3

So far I have this query: 到目前为止,我有这个查询:

insert into CustDetails (CustNo, Year, StatusId)
    select 
        CustNo, 2016, StatusId
    from 
        CustDetails
    where 
        Year = 2015

This covers part 1. I don't have the logic in place for part 2 in the query above and I guess I could just do: 这涵盖了第1部分。我在上面的查询中没有第2部分的逻辑,我想我可以这样做:

UPDATE CustDetails 
SET StatusId = 1  
WHERE Year = 2016 AND StatusId = 2

Again I don't have the query done for part 3 but I am thinking I could get the CustNo 's that are in ClaimDetails but not in CustDetails by doing something like: 同样,我没有对第3部分进行查询,但是我想我可以通过执行以下操作来获取ClaimDetailsCustNo ,而不是CustDetailsCustNo

SELECT CustNo 
FROM ClaimDetails
WHERE CustNo NOT IN (SELECT CustNo FROM CustDetails)

And then do a create on the IDs for each CustNo individually. 然后分别为每个CustNo的ID进行创建。

Question is this the best way to do this or should I do something like copy to a temp table first. 问题是这是执行此操作的最佳方法,还是我应该先执行诸如复制到临时表的操作。 And will the query for the 3rd part work ok even though they are two separate DB's - I guess if they are on the same server as long as I fully qualify the DB's it should work ok. 而且即使它们是两个单独的数据库,对第三部分的查询也可以正常工作-我猜只要它们完全符合数据库要求,它们是否在同一台服务器上就可以正常工作。

Actually with your 2nd query, you will also be updating the old data which already existed before the execution of the 1st query. 实际上,对于第二个查询,您还将更新在执行第一个查询之前已经存在的旧数据。 So I suggest using CASE WHEN like this: 所以我建议像这样使用CASE WHEN:

INSERT INTO CustDetails (CustNo, Year, StatusId)
SELECT CustNo, 2016, CASE WHEN StatusId = 2 THEN 1 ELSE StatusId END
FROM CustDetails
WHERE Year = 2015

The 3rd query can also be done similarly using an INSERT INTO SELECT : 第三个查询也可以使用INSERT INTO SELECT类似地完成:

INSERT INTO CustDetails (CustNo, Year, StatusId)
SELECT CustNo, 2016, 3
FROM ClaimDetails
WHERE CustNo NOT IN (SELECT CustNo FROM CustDetails)

You will have to fully qualify the DBs including the schema name as DatabaseName.SchemaName.TableName for a multi database query. 对于多数据库查询,您必须完全限定包括模式名称在内的DatabaseName.SchemaName.TableNameDatabaseName.SchemaName.TableName

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

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