我无法在 Microsoft SQL Server 2014 中创建正确的触发器
Posted
技术标签:
【中文标题】我无法在 Microsoft SQL Server 2014 中创建正确的触发器【英文标题】:I am having trouble creating a proper trigger in Microsoft SQL Server 2014 【发布时间】:2017-11-16 03:38:00 【问题描述】:我正在尝试在tplayoffStatus
上创建一个触发器,这样一来,团队就不能被插入到表中,并且损失超过一次。将值插入表 tplayoffStatus
时,我不断收到的错误是
子查询返回超过 1 个值。当子查询跟随 =、!=、、>= 或子查询用作表达式时,这是不允许的。
谁能帮我解决这个问题?我的 SELECT
语句在 loss 列中返回多个值是否错误?
SQL Server 2014 表架构:
CREATE TABLE tcollegeFootballTeams
(
TeamAbbreviation varchar(5) NOT NULL PRIMARY KEY,
TeamName varchar(50) NOT NULL,
City varchar(50) NOT NULL,
State varchar(2) NOT NULL,
Wins int NOT NULL,
Losses int NOT NULL
)
CREATE TABLE tplayoffStatus
(
TeamName varchar(50) NOT NULL PRIMARY KEY,
TeamAbbreviation varchar(5) NOT NULL FOREIGN KEY REFERENCES tcollegeFootballTeams(TeamAbbreviation)
);
CREATE TRIGGER [dbo].[trgNoPlayoffForYou]
ON [dbo].[tplayoffStatus]
AFTER INSERT
AS
DECLARE @Losses INT
DECLARE @ErrorMessage VARCHAR(30)
SELECT @Losses = (SELECT Losses
FROM dbo.tcollegeFootballTeams
WHERE (Losses > 1))
IF (@Losses = 'True')
BEGIN
SET @ErrorMessage = 'You may not enter the playoffs with more than 1 loss'
PRINT @ErrorMessage
ROLLBACK TRANSACTION
END
查询 1:
INSERT INTO tcollegeFootballTeams (TeamAbbreviation, TeamName, City, State, Wins, Losses)
VALUES ('OSU', 'THE Ohio State University', 'Columbus', 'OH', 10, 2)
[结果][2]
查询 2:
INSERT INTO tcollegeFootballTeams (TeamAbbreviation, TeamName, City, State, Wins, Losses )
VALUES ('BAMA', 'Alabama', 'Tuscaloosa', 'AL', 12, 0)
[结果][3]
查询 3:
INSERT INTO tcollegeFootballTeams (TeamAbbreviation, TeamName, City, State, Wins, Losses )
VALUES ('UGA', 'Georgia', 'Athens', 'GA', 11, 1)
[结果][4]
查询 4:
INSERT INTO tcollegeFootballTeams (TeamAbbreviation, TeamName, City, State, Wins, Losses)
VALUES ('CLEM', 'Clemson', 'Clemson', 'SC', 11, 1)
[结果][5]
查询 5:
INSERT INTO tcollegeFootballTeams (TeamAbbreviation, TeamName, City, State, Wins, Losses)
VALUES ('WIS', 'Wisconsin', 'Madison', 'WI', 11, 1)
[结果][6]
查询 6:
INSERT INTO tcollegeFootballTeams (TeamAbbreviation, TeamName, City, State, Wins, Losses)
VALUES ('ND', 'Notre Dame', 'South Bend', 'IN', 10, 2)
[结果][7]
查询 7:
INSERT INTO tcollegeFootballTeams (TeamAbbreviation, TeamName, City, State, Wins, Losses)
VALUES ('OKLA', 'Oklahoma', 'Norman', 'OK', 10, 2)
[结果][8]
查询 8:
INSERT INTO tcollegeFootballTeams (TeamAbbreviation, TeamName, City, State, Wins, Losses)
VALUES ('MIAF', 'Miami of Florida', 'Coral Gables', 'FL', 10, 2)
【问题讨论】:
【参考方案1】:你的触发器是错误的。您需要在inserted
表中查询插入的记录,并在tcollegeFootballTeams
表中加入Losses
CREATE TRIGGER [dbo].[trgNoPlayoffForYou] ON [dbo].[tplayoffStatus]
AFTER INSERT
AS
IF EXISTS
(
SELECT *
FROM inserted i
INNER JOIN dbo.tcollegeFootballTeams t
ON i.TeamAbbreviation = t.TeamAbbreviation
WHERE t.Losses > 1
)
BEGIN
SET @ErrorMessage = 'You may not enter the playoffs with more than 1 loss'
Print @ErrorMessage
Rollback Transaction
END
【讨论】:
这行得通。谢谢你。我什至没有想过要使用这样的选择语句,但这完全有道理。我显然需要处理我的 SQL 的这方面。以上是关于我无法在 Microsoft SQL Server 2014 中创建正确的触发器的主要内容,如果未能解决你的问题,请参考以下文章
无法使用 IntelliJ/Rider 连接到 Microsoft SQL Server
如何在 Microsoft SQL Server Management Studio 2014 中更新表
Microsoft SQL Server Management Studio 安装程序错误
错误= [Microsoft] [SQL Server的ODBC驱动程序13]无法打开BCP主机数据文件