简体   繁体   English

在合并过程中将选择用作源并插入到多个表中

[英]Use a select as source during merge and insert into multiple tables

I can do plenty with the basics of SQL, but I'm struggling with this more complex task. 我可以用SQL的基础知识做很多事情,但是我在这个更复杂的任务上苦苦挣扎。 I have three tables set up like this. 我有三个这样设置的表。

Person

  • identifier 识别码
  • attribute 属性
  • type 类型

Employee 雇员

  • identifier 识别码
  • hobby_id hobby_id

Hobby 爱好

  • hobby_id hobby_id
  • attribute 属性

I need to select rows from Person of specific types that exist in the employee table. 我需要从员工表中存在的特定类型的人员中选择行。 Then, if the employee table has a hobby_id (not null) then I need to update the Hobby row with the attribute from the corresponding person row. 然后,如果雇员表中有一个hobby_id(不为null),那么我需要使用对应人员行中的属性更新Hobby行。 If the employee row does NOT have a hobby_id, I need to create a new one with the person attribute and set the employee Hobby_id to the new id. 如果雇员行没有hobby_id,则需要使用person属性创建一个新的雇员,并将雇员Hobby_id设置为新的ID。

I've tried a lot of approaches. 我尝试了很多方法。 I started with an IF/ELSE syntax that was getting really messy so I tried a CASE statement, but it appears I can't do inserts or updates in those. 我从一个非常混乱的IF / ELSE语法开始,所以我尝试了CASE语句,但是看来我无法在其中进行插入或更新。 I'm currently trying to use a MERGE because it looked robust enough to work. 我目前正在尝试使用MERGE,因为它看起来足够强大,可以正常工作。 My current 'iteration' that I'm working with is: 我正在使用的当前“迭代”是:

USE MY_DB

SET @nextHobbyId = (SELECT MAX( HOBBY_ID ) FROM HOBBY)

MERGE DBO.HOBBY AS TARGET

USING (
    SELECT ATTRIBUTE, HOBBY_ID
    FROM AA.PERSONe 
        INNER JOIN DBO.EMPLOYEE v 
        ON e.IDENTIFIER= v.IDENTIFIER
) AS SOURCE (hobbyId, ATTRIBUTE)

ON (TARGET.HOBBY_ID = SOURCE.HOBBY_ID) 

WHEN MATCHED 
THEN UPDATE SET 
    TARGET.ATTRIBUTE= SOURCE.ATTRIBUTE

WHEN NOT MATCHED BY TARGET 
THEN INSERT (HOBBY_ID, ATTRIBUTE)
VALUES (@nextHobbyId + 1, ATTRIBUTE);

You can see I don't even attempt to update the employee table with the new hobby_id in this iteration because I currently get an error that "ATTRIBUTE is an invalid column name" on the very last line. 您可以看到我什至在此迭代中甚至都没有尝试用新的hobby_id更新employee表,因为我目前在最后一行上看到“ ATTRIBUTE是无效的列名”的错误。

Things to note, hobby ID does not have an identity but the code in our app that used to be the only thing to touch the table used something like : 需要注意的是,爱好ID没有身份,但是我们应用程序中曾经是唯一可以触摸表格的代码,其使用方式如下:

SELECT NEXT VALUE FOR...

when doing inserts so I try to mimic that by selecting max value + 1. While my code will likely never run at the same time as the original code, I'm worried about a possible race condition. 在执行插入操作时,我尝试通过选择最大值+ 1来模仿。尽管我的代码可能永远不会与原始代码同时运行,但我担心可能出现竞争状况。

How can I make this work? 我该如何进行这项工作? I'm not attached to a merge if its not the way to go. 如果没有合并的方法,我就不会被合并。

If I understood correctly, it can be done fairly simple: 如果我正确理解,可以相当简单地完成它:

;with CTE as (
    select * from  Person where identifier is in (select identifier from Employees
)

update Hobby set ... --some updates
where hobby_id in (select hobby_id from CTE where hobby_id is not null)

insert into Hobby
... -- here, through join most probably you can perform insert or any other updating operation

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

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