我需要你的帮助。
我有两张表MasterEntries
和Awardlist
。 MasterEntries
有某些事件的排名结果,例如1,2,3,4,5,6。我想使用T-SQL触发器从MasterEntries
NABELY 1,2,3中选择并插入排名,并将其插入{{1}中的类似列(排名)中} table ONLY WHEN 与相应事件相关的记录数达到6。
以下是我的表结构:
AwardList
表
MasterEntries
[EntryID] INT IDENTITY(1, 1) NOT NULL,
[CompetitiveEventCode] NVARCHAR(5) NULL,
[CompetitiveEventName] NVARCHAR(40) NULL,
[CategoryDescription] NVARCHAR(25) NULL,
[YearOfEvent] NVARCHAR(50) NULL,
[CompetitorID] INT NOT NULL,
[FirstName] NVARCHAR(20) NULL,
[LastName] NVARCHAR(20) NULL,
[Institution] NVARCHAR(50) NULL,
[ParishName] NVARCHAR(15) NULL,
[Region] NVARCHAR(12) NULL,
[Participated] NVARCHAR(3) NULL,
[AccumulatedMarks] DECIMAL(5, 2) NULL,
[AverageMark] DECIMAL(5, 2) NULL,
[Ranked] INT NULL,
[PointsEarned] INT NULL,
[AwardPlacing] CHAR(4) NULL,
CONSTRAINT [PK_MasterEntries] PRIMARY KEY CLUSTERED ([EntryID] ASC)
表
Awardlist
我尝试了以下查询
[AwardID] INT IDENTITY (1, 1) NOT NULL,
[CompetitiveEventName] NVARCHAR(40) NULL,
[CategoryDescription] NVARCHAR(255) NULL,
[FirstName] NVARCHAR(20) NULL,
[LastName] NVARCHAR(20) NULL,
[Institution] NVARCHAR(50) NULL,
[ParishName] NVARCHAR(15) NULL,
[Region] NVARCHAR(12) NULL,
[AverageMark] DECIMAL(5, 2) NULL,
[Ranked] INT NULL,
[AwardPlacing] CHAR(4) NULL,
[CompetitiveEventCode] NVARCHAR(5) NULL
还
CREATE TRIGGER Insert_Award_list_FROM_MasterEntries
ON MasterEntries
AFTER INSERT, UPDATE
AS
DECLARE @rankedcount INT,
@CompetitiveEventCode NVARCHAR(5),
@CompetitiveEventName NVARCHAR(40),
@CategoryDescription NVARCHAR(25),
@FirstName NVARCHAR(2),
@LastName NVARCHAR(25),
@ParishName NVARCHAR(15),
@Region NVARCHAR(12),
@Institution NVARCHAR(50),
@AverageMark INT,
@Ranked INT,
@AwardPlacing NVARCHAR(4)
SELECT @rankedcount = Count(CompetitiveEventCode)
FROM MasterEntries
WHERE CompetitiveEventCode = 'AGPRO'
AND Ranked = 1 AND Ranked = 2 And Ranked = 3;
IF @rankedcount = 6
BEGIN
INSERT INTO AwardList(CompetitiveEventCode, CompetitiveEventName, CategoryDescription, FirstName, LastName, ParishName,
Region, Institution, AverageMark, Ranked, AwardPlacing)
VALUES (@CompetitiveEventCode, @CompetitiveEventName, @CategoryDescription, @FirstName, @LastName, @ParishName,
@Region, @Institution, @AverageMark, @Ranked, @AwardPlacing);
END;
请帮我解决这个问题,因为上述触发器都没有给我结果,请
答案 0 :(得分:0)
我认为问题在于这段代码
WHERE CompetitiveEventCode = 'AGPRO' AND Ranked = 1 AND Ranked = 2 And Ranked = 3;
是否可以同时在1,2和3级中使用相同的CompetitiveEventCode(即'AGPRO')? 根据条件 @rankedcount 将在满足所有条件时计算。