简体   繁体   English

如何从输出子句中的两个表中获取列

[英]How to get the column from two tables in output clause

I have two tables 我有两张桌子

TableA (Col1 Pk identity, Col2, Col3)
TableB (Col2,Col3, Col4)

Now, I want to get the the combination of Col1, Col4 . 现在,我想获得Col1和Col4的组合。

I am using this 我正在用这个

INSERT INTO TableA (Col2, Col3)
OUTPUT inserted.*
SELECT DISTINCT Col2,Col3
FROM TableB

But below will give me only Col1, Col2 & Col3 of tableA.. if I am not wrong. 但是下面如果我没记错的话,只会给我tableA的Col1,Col2和Col3。 Here I want Col1 (TableA) & Col4 (TableB) Now, how can I get Col4 and the respected indentity row in TableA. 在这里,我想要Col1(TableA)和Col4(TableB)现在,如何获取Col4和TableA中受人尊敬的标识行。

Edit 编辑

Below is a sample scenrio which may help you 以下是示例场景,可能对您有帮助

    CREATE TABLE [dbo].[A](
    [Col1] [int] IDENTITY(1,1) NOT NULL,
    [Col2] [varchar](50) NULL,
    [Col3] [varchar](50) NULL,
 CONSTRAINT [PK_A] PRIMARY KEY CLUSTERED 
(
    [Col1] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

CREATE TABLE [dbo].[B](
    [Col2] [varchar](50) NULL,
    [Col3] [varchar](50) NULL,
    [Col4] [varchar](50) NULL
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO



INSERT INTO A (Col2,Col3)
OUTPUT INSERTED.*
SELECT COL2, Col3, Col4 
FROM B

Can I do it with Checksum? 我可以使用Checksum吗?

merge A as T 
using B as S 
on 0=1 
when not matched then 
insert (Col2, Col3) values (S.Col2,S.Col3) 
output inserted.Col1, S.Col4;

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

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