查询我正在寻找不匹配的3个表

时间:2013-04-12 18:05:20

标签: sql-server tsql

我有三个表:LitHold,LitHoldDetails和EmailTemplate。定义如下。

CREATE TABLE [dbo].[LitHold](
[LitholdID] [int] IDENTITY(1,1) NOT NULL,
[LitHoldStatusID] [tinyint] NOT NULL,
[EmailReminderID] [tinyint] NULL,
[ApprovedDate] [datetime] NULL,
[TerminatedDate] [datetime] NULL,
CONSTRAINT [PK_Lithold] PRIMARY KEY CLUSTERED 
(
[LitholdID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]


CREATE TABLE [dbo].[LitHoldDetails](
[LitHoldDetailsID] [int] IDENTITY(1,1) NOT NULL,
[LitholdID] [int] NOT NULL,
[VersionID] [int] NOT NULL,
[Description] [varchar](300) NULL,
[ResAttorneyID] [varchar](10) NOT NULL,
[Comments] [varchar](1000) NULL,
[HoldStartDate] [datetime] NULL,
[HoldEndDate] [datetime] NULL,
[CreatedDate] [datetime] NOT NULL,
[CreatedByLogin] [varchar](10) NULL,
CONSTRAINT [PK_LitholdDetails] PRIMARY KEY CLUSTERED 
(
[LitHoldDetailsID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]


CREATE TABLE [dbo].[EmailTemplate](
[TemplateID] [int] IDENTITY(1,1) NOT NULL,
[LitHoldDetailsID] [int] NOT NULL,
[From] [varchar](50) NULL,
[To] [varchar](2000) NULL,
[CC] [varchar](500) NULL,
[BCC] [varchar](500) NULL,
[Subject] [nvarchar](200) NULL,
[MessageBody] [nvarchar](max) NULL,
[SendDate] [datetime] NULL,
[IsDefault] [bit] NOT NULL,
CONSTRAINT [PK_EmailTemplate] PRIMARY KEY CLUSTERED 
(
[TemplateID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

对于每个LitHold,可以有多个LitHoldDetails。对于每个LitHoldDetail,应该有一个EmailTemplate。我最近发现一些LitHoldDetails没有EmailTemplates。我们仍然在开发这个项目,所以这不是什么大问题。但是,我们希望将EmailTemplate数据导入数据库。情况是,对于每个LitHold,至少有一个具有EmailTemplate的LitHoldDetail。我想复制所有LitHoldDetails的EmailTemplate数据,a)具有相同的LitHoldID和b)没有EmailTemplate。我尝试过的方法之一是:

insert into EmailTemplate
(LitHoldDetailsID, [From], [To], CC, BCC, Subject, MessageBody, SendDate, IsDefault)
(select (select top 1 LitHoldDetailsID from LitHoldDetails where LitholdID = d.LitholdID and LitHoldDetailsID <> e.LitHoldDetailsID), [To], CC, BCC, Subject, MessageBody, SendDate, IsDefault from 
EmailTemplate e inner join LitHoldDetails d on e.LitHoldDetailsID = d.LitHoldDetailsID)

但这会为一些LitHoldDetails带来多行,使用不同的EmailTemplate数据,以及LitHoldDetails为NULL的一些行。我怎么能做到这一点?我正在使用SQL Server 2008。

1 个答案:

答案 0 :(得分:1)

尝试插入此内容:

SELECT lhd.LitHoldDetailsID, CloneEmailTemplate.[From], ... 
FROM LitHoldDetails lhd 
CROSS APPLY (SELECT TOP 1 et.*
             FROM EmailTemplate et
             JOIN LitHoldDetails lhd2 ON lhd2.LitHoldDetailsID = et.LitHoldDetailsID
             WHERE lhd2.LitHoldID = lhd.LitHoldID
            ) AS CloneEmailTemplate 
WHERE NOT EXISTS (SELECT 1 
                  FROM EmailTemplate et2 
                  WHERE et2.LitHoldDetailsID = lhd.LitHoldDetailsID
                 )