0

tblForm10Objectives を更新しようとしています。ObjectiveNumber 列を、tblForm10Goals、tblForm10GoalsObjectives、および tblForm10Objectives の結合から返された行数に基づいて 1 から始まる増分値に設定します。ここで、ID_Agency = tblForm10Goals にあるエージェンシー ID です。

これまでのクエリの例:

UPDATE dbo.tblForm10Objectives
SET ObjectiveNumber = rn
FROM tblForm10Goals As g
    Left Join tblForm10GoalsObjectives gobs ON g.ID_Form10Goal = gobs.ID_Form10Goal
    Right Join 
(
    SELECT
        ROW_NUMBER() OVER (PARTITION BY g.ID_Agency
                            ORDER BY OB.ID_Form10Objective) AS rn,
        OB.ID_Form10Objective

    FROM tblForm10Goals g
    LEFT JOIN dbo.tblForm10GoalsObjectives gobs ON g.ID_Form10Goal = gobs.ID_Form10Goal
    RIGHT JOIN dbo.tblForm10Objectives OB ON gobs.ID_Form10Objective = OB.ID_Form10Objective
    Where g.ID_Agency = 2
) rns ON dbo.tblForm10Objectives.ID_Form10Object = rns.ID_Form10Objective

内部の select ステートメントは、正しくインクリメントされた ObjectiveNumber 値を返します。必要なのは、ObjectiveNumber = rn で dbo.tblForm10Objectives を更新することだけです。ご協力いただきありがとうございます。

スクリプト化されたテーブル

最初のテーブル

 CREATE TABLE [dbo].[tblForm10Goals](
    [ID_Form10Goal] [int] IDENTITY(1,1) NOT NULL,
    [ID_Agency] [int] NOT NULL,
    [GoalNumber] [int] NULL,
    [GoalDescription] [varchar](1200) NULL,
    [Locked] [bit] NULL,
 CONSTRAINT [PK_tblForm10Goals] PRIMARY KEY CLUSTERED 
(
    [ID_Form10Goal] 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

セカンドテーブル

CREATE TABLE [dbo].[tblForm10GoalsObjectives](
    [ID_Form10GoalObjective] [int] IDENTITY(1,1) NOT NULL,
    [ID_Form10Goal] [int] NOT NULL,
    [ID_Form10Objective] [int] NOT NULL,
 CONSTRAINT [PK_tblForm10GoalsObjectives] PRIMARY KEY CLUSTERED 
(
    [ID_Form10GoalObjective] 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

サードテーブル

CREATE TABLE [dbo].[tblForm10Objectives](
    [ID_Form10Objective] [int] IDENTITY(1,1) NOT NULL,
    [ObjectiveNumber] [int] NULL,
    [ObjectiveDescription] [varchar](1200) NULL,
    [UnitofMeasure] [varchar](100) NULL,
    [Q1Target] [varchar](50) NULL,
    [Q1Actual] [varchar](50) NULL,
    [Q1ActualLocked] [bit] NULL,
    [Q2Target] [varchar](50) NULL,
    [Q2Actual] [varchar](50) NULL,
    [Q2ActualLocked] [bit] NULL,
    [Q3Target] [varchar](50) NULL,
    [Q3Actual] [varchar](50) NULL,
    [Q3ActualLocked] [bit] NULL,
    [Q4Target] [varchar](50) NULL,
    [Q4Actual] [varchar](50) NULL,
    [Q4ActualLocked] [bit] NULL,
    [AnnualTarget] [varchar](50) NULL,
    [AnnualActual] [varchar](50) NULL,
    [AnnualActualLocked] [bit] NULL,
    [TargetsLocked] [bit] NULL,
    [Locked] [bit] NULL,
 CONSTRAINT [PK_tblForm10Objectives] PRIMARY KEY CLUSTERED 
(
    [ID_Form10Objective] 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
4

2 に答える 2