create trigger update_purchase_warehouse after insert on purchase for each row Begin set @count; select count(*) into count from warehouse where com_no=NEW.com_no; IF (@count>0) THEN update warehouse set com_num=com_num+NEW.com_gnum where com_no=NEW.com_no; ELSE insert into warehouse(com_no,com_name,com_overdue,com_num) values(NEW.com_no,NEW.com_name,NEW.com_overdue,NEW.com_gnum); END if; END;
时间: 2024-03-22 20:42:19 浏览: 53
根据你提供的 SQL 语句,我注意到以下问题:
1. 在 `Set @count;` 这行代码中,变量 @count 没有被初始化。应该将其设置为0或其他初始值。
2. 在 `select count(*) into count from warehouse where com_no=NEW.com_no;` 这行代码中,应该将 `into count` 改为 `into @count`,以便将查询结果赋值给 @count 变量。
3. 在 `END if; END;` 这行代码中,应该将 `END if;` 改为 `END IF;`,因为 IF 语句应该用大写。
下面是修正后的 SQL 语句:
```
CREATE TRIGGER update_purchase_warehouse
AFTER INSERT ON purchase
FOR EACH ROW
BEGIN
SET @count = 0;
SELECT COUNT(*) INTO @count FROM warehouse WHERE com_no = NEW.com_no;
IF (@count > 0) THEN
UPDATE warehouse SET com_num = com_num + NEW.com_gnum WHERE com_no = NEW.com_no;
ELSE
INSERT INTO warehouse (com_no, com_name, com_overdue, com_num)
VALUES (NEW.com_no, NEW.com_name, NEW.com_overdue, NEW.com_gnum);
END IF;
END;
```
希望这次修改对你有帮助!
阅读全文