我以前曾获得过这段代码的帮助,但是现在我想将结果集添加到单独的表变量 @check 中,但由于无法确定在何处添加Insert语句而已 Union 语句?
DECLARE @customer_dist TABLE
(
[id] [INT] NOT NULL ,
[First_Name] [VARCHAR](50) NULL ,
[Last_Name] [VARCHAR](50) NULL ,
[DoB] [DATE] NULL ,
[post_code] [VARCHAR](50) NULL ,
[mobile] [VARCHAR](50) NULL ,
[Email] [VARCHAR](100) NULL
);
INSERT INTO @customer_dist ( id ,
First_Name ,
Last_Name ,
DoB ,
post_code ,
mobile ,
Email )
select id, first_name, last_name, dob, post_code, mobile, email from checky where NID = 148575
DECLARE @check TABLE
(
[id] [INT] NOT NULL ,
[NID] [INT] NOT NULL
)
;with PairedRows as (
select CDL.id as NId, CDR.id as Id
from @customer_dist as CDL inner join
@customer_dist as CDR on CDR.Id > CDL.Id and
CDR.First_Name = CDL.First_Name and CDR.Last_Name = CDL.Last_Name and CDR.DoB = CDL.DoB and
( CDR.post_code = CDL.post_code or CDR.mobile = CDL.mobile or CDR.Email = CDL.Email )
where not exists (
select 42 from @customer_dist as NE where NE.ID < CDL.Id and
NE.First_Name = CDL.First_Name and NE.Last_Name = CDL.Last_Name and NE.DoB = CDL.DoB and
( NE.post_code = CDL.post_code or NE.mobile = CDL.mobile or NE.Email = CDL.Email ) ) )
select NId, Id
from PairedRows
union all
select Min( NId ) as NID, Min( NId ) as Id
from PairedRows
group by NId
union all
select id, id
from @customer_dist as CD
where not exists ( select 42 from PairedRows as PR where PR.NId = CD.id or PR.Id = CD.id )
order by NID, Id
答案 0 :(得分:0)
WITH
bah AS
(
stuff
)
INSERT INTO
table
SELECT foo FROM bah
UNION ALL
SELECT foo FROM bah
UNION ALL
SELECT foo FROM bah
答案 1 :(得分:0)
我已经弄清楚了将其添加到何处,谢谢您的指点
;with PairedRows as (
select CDL.id as NId, CDR.id as Id
from @customer_dist as CDL inner join
@customer_dist as CDR on CDR.Id > CDL.Id and
CDR.First_Name = CDL.First_Name and CDR.Last_Name = CDL.Last_Name and CDR.DoB = CDL.DoB and
( CDR.post_code = CDL.post_code or CDR.mobile = CDL.mobile or CDR.Email = CDL.Email )
where not exists (
select 42 from @customer_dist as NE where NE.ID < CDL.Id and
NE.First_Name = CDL.First_Name and NE.Last_Name = CDL.Last_Name and NE.DoB = CDL.DoB and
( NE.post_code = CDL.post_code or NE.mobile = CDL.mobile or NE.Email = CDL.Email ) ) )
insert into @check
select NId, Id
from PairedRows
union all
select Min( NId ) as NID, Min( NId ) as Id
from PairedRows
group by NId
union all
select id, id
from @customer_dist as CD
where not exists ( select 42 from PairedRows as PR where PR.NId = CD.id or PR.Id = CD.id )
order by NID, Id