DECLARE **cusname** varchar;
IF(New.BecomeVolunteer = 'Yes')
THEN
SELECT CusName INTO cusname FROM customer WHERE CusID = NEW.CusID;
INSERT INTO staff (StaffName, StaffTel, StaffDOB, Availability, Duties, [enter image description here][1]StaffType)
VALUES
(**cusname**, '07589634598', '1965-05-13', 'Tue,Wed,SatPM','Collecting Tickets,Ushering Customers to Seats,Looking after the Artists', 'Volunteer');
END IF
我有一个表职员,我想从“客户”表中获取“ CusName”并将其设置为变量“ cusname”。然后,我想在我的Insert语句中将该变量用作“ StaffName”属性的值
我对触发器的使用还不多,但是我了解使简单语句起作用的基本语法,但是该语句不断出现错误。如果有人可以帮助我弄清楚这里出了什么问题,我将不胜感激。
IF(New.BecomeVolunteer = 'Yes')
THEN
INSERT INTO staff (StaffName, StaffTel, StaffDOB, Availability, Duties, StaffType)
VALUES
('Jaamees', '07589634598', '1965-05-13', 'Tue,Wed,SatPM','Collecting Tickets,Ushering Customers to Seats,Looking after the Artists', 'Volunteer');
END IF
下面的语句工作得很好,但是上面的语句却不行(当我想添加变量时)。
答案 0 :(得分:0)
IF
默认情况下仅使用一条语句。如果要在IF
中执行多个语句,请使用BEGIN...END
:
IF (New.BecomeVolunteer = 'Yes')
BEGIN
SELECT CusName INTO cusname FROM customer WHERE CusID = NEW.CusID;
INSERT INTO staff (StaffName, StaffTel, StaffDOB, Availability, Duties, StaffType)
VALUES
(cusname, '07589634598', '1965-05-13', 'Tue,Wed,SatPM',
'Collecting Tickets,Ushering Customers to Seats,Looking after the Artists',
'Volunteer');
END;
答案 1 :(得分:0)
您可以在一条语句中完成所有操作:
IF (New.BecomeVolunteer = 'Yes') THEN
INSERT INTO staff (StaffName, StaffTel, StaffDOB, Availability, Duties, StaffType)
SELECT c.cusname, '07589634598', '1965-05-13', 'Tue,Wed,SatPM', 'Collecting Tickets,Ushering Customers to Seats,Looking after the Artists', 'Volunteer')
FROM customer c
WHERE c.CustId = NEW.CusID
而且,您甚至可以删除IF
:
INSERT INTO staff (StaffName, StaffTel, StaffDOB, Availability, Duties, StaffType)
SELECT c.cusname, '07589634598', '1965-05-13', 'Tue,Wed,SatPM', 'Collecting Tickets,Ushering Customers to Seats,Looking after the Artists', 'Volunteer')
FROM customer c
WHERE c.CustId = NEW.CusID AND
New.BecomeVolunteer = 'Yes';