尝试检查插入的表列level_name中输入的值是否与Levels
表列level_name匹配。我的查询返回错误Subquery returned more than 1 value.
。试着按照Dan Guzman的那个编写代码。
Declare @levelname nvarchar(MAX)=
(
SELECT [LEVEL_NAME] + ' '
FROM inserted
WHERE (select LEVEL_NAME from levels where level_name=(select level_name
from inserted)) IS NULL
FOR XML PATH('')
);
if @levelname is not null
BEGIN
RAISERROR();
END
答案 0 :(得分:0)
我建议您在查询中使用Top 1或使用以下格式:
IF Exists(SELECT [LEVEL_NAME]
FROM inserted i
INNER JOIN levels l ON l.Level_name = i.Level_name) Begin
RAISERROR('Your error message',16,1);
END
OR
IF (SELECT COUNT(*)
FROM inserted i
INNER JOIN levels l ON l.Level_name = i.Level_name)=0 Begin
RAISERROR('Your error message',16,1);
END
我建议在level_name列上使用UNIQUE INDEX。为了限制用户插入duplication level_name值。
答案 1 :(得分:0)
试试这个:
DECLARE @levelname NVARCHAR(MAX)=
(
SELECT [LEVEL_NAME] + ' '
FROM inserted
WHERE EXISTS (SELECT LEVEL_NAME FROM levels
WHERE level_name IN (SELECT level_name FROM inserted))
FOR XML PATH('')
);
IF @levelname IS NOT NULL
BEGIN
RAISERROR();
END