0

LitHold、LitHoldDetails、EmailTemplate の 3 つのテーブルがあります。定義は次のとおりです。

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 ごとに、1 つの EmailTemplate が必要です。最近、一部の LitHoldDetails に EmailTemplates がないことがわかりました。私たちはまだこのプロジェクトの開発に取り組んでいるので、これは大したことではありません. ただし、EmailTemplate データをデータベースに取得する必要があります。各 LitHold に対して、EmailTemplate を持つ LitHoldDetail が少なくとも 1 つあるという状況です。a) 同じ LitHoldID を持ち、b) EmailTemplate を持たないすべての LitHoldDetails に対して、この EmailTemplate データを複製したいと思います。私が試したアプローチの1つは次のとおりです。

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 を使用しています。

4

1 に答える 1

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
                 )
于 2013-04-12T18:31:07.827 に答える