问题描述
我正在使用sql中的match和merge语句。它在2个表之间很有用,这是它最初设计的。但是,我希望在匹配第三个可能的第4个表时插入数据。
这是合并声明:
I am using the match and merge statement from sql. It works great between 2 tables, which is what it was originally designed for. However, I am looking to insert data when matched to a third possibly 4th table.
Here is the merge statement:
Merge into [table1] as T using [table2] as S on T.Last_name = S.Last_name and T.First_name = S.First_name
When matched then update set T.Last_name = S.Last_name, T.First_name = S.First_name;
我想要的是将所有匹配行的ID和引用列值插入表3.这样我可以通过唯一的ID号引用表。
我是什么ave尝试过:
我试图在匹配时添加另一个,但它没有做任何事情。请注意,此代码与我正在运行的vb.net代码结合使用。
What I want is to insert the ID and reference column value of all matched rows to table 3. that way I can reference the table by the ID number which is unique.
What I have tried:
I have tried to add another when matched but it does not do anything. Mind you this code is in combination with vb.net code that I am running.
推荐答案
-----Inserting data when no match found.
MERGE [dbo].[Department_Target] AS tar
USING [dbo].[Department_Source] AS src
ON tar.[DepartmentID] = src.[DepartmentID]
WHEN NOT MATCHED THEN
INSERT
(
[DepartmentID],
[Name],
[GroupName],
[ModifiedDate]
)
VALUES
(
src.[DepartmentID], src.[Name], src.[GroupName], src.[ModifiedDate]
)
OUTPUT
这篇关于如何在匹配时添加其他内容或如何将某些列信息导出到第三个表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!