我有一个存储过程,应检查正在添加的艺术家是否在Allowed Nationality
表中,但它不起作用。代码插入艺术家是否在Allowed Nationality
表中。谁能告诉我我做错了什么?
DELIMITER //
CREATE PROCEDURE `InsertNewArtistCheck`
(IN newLastName Char(25),
IN newFirstName Char(25),
IN newNationality Char(30),
IN newDateOfBirth Numeric(4),
IN newDateDeceased Numeric(4))
BEGIN
DECLARE varRowCount Int;
SELECT Nation INTO varRowCount
FROM ALLOWED_NATIONALITY
WHERE Nation = newNationality;
IF (varRowCount < 0)
THEN
ROLLBACK;
SELECT 'Nationality Not Allowed' AS ErrorMessage;
END IF;
INSERT INTO ARTIST (LastName, FirstName, Nationality,
DateOfBirth, DateDeceased)
VALUES (newLastName, newFirstName, newNationality,
newDateOfBirth, newDateDeceased);
SELECT 'New artist data added to database' AS InsertStatus;
END//
DELIMITER ;
答案 0 :(得分:1)
尝试以下更改:
DECLARE varRowCount Int;
SELECT count(*) INTO varRowCount
FROM ALLOWED_NATIONALITY
WHERE Nation = newNationality;
IF (varRowCount < 1)
THEN
ROLLBACK;
SELECT 'Nationality '+newnationality+' not Allowed' AS ErrorMessage;
RETURN
END IF;
您正在尝试将字符值(NATION)放入数字变量(varRowCount)。你真正想要的是确定与新艺术家国家相匹配的国家数量(希望是1)。您也不需要ROLLBACK语句,因为存储过程没有完成“撤消”所需的任何操作
答案 1 :(得分:0)
将被关闭,仍然......
见这一行:
IF(varRowCount&lt; 0)
告诉我你认为SQL Server会在什么条件下返回负数行?
应该等于0,不小于。