创建触发器,在更新表后自动更新列

时间:2015-05-13 16:47:39

标签: mysql database database-design triggers mysql-workbench

我有一张看起来像这样的表:

| idAvailability | SumRooms | AvailableRooms | AvailabilityRate |

除了AvailabilityRate(浮点数)之外,一切都是int。 我想创建一个触发器,当更新AvailableRooms列时,由于触发器的存在而自动更新了AvailabilityRate列。

这是我到目前为止所做的。

CREATE TRIGGER availability_BEFORE_UPDATE
BEFORE UPDATE ON availability
FOR EACH ROW
BEGIN
  update availability
    set new.AvailabilityRate = new.AvailableRooms/new.SumRooms;
END

但是它让我想到了这个错误:

执行: UPDATE ergasia.availability SET AvailableRooms=15 WHERE idAvailability=1;

错误1442:1442:Can't update table availability in stored function/trigger because it is already used by statement which invoked this stored function/trigger. SQL Statement: UPDATE ergasia.availability SET AvailableRooms=15 WHERE idAvailability=1

2 个答案:

答案 0 :(得分:0)

你应该只需要这个部分:

Private Sub Workbook_Open() Dim wb As Workbook Dim wb2 As Workbook Set wb = ActiveWorkbook Application.DisplayAlerts = False Cells.ClearContents Cells.UnMerge Cells.ClearContents Workbooks.Open Filename:= _ "Photo\Studio\\DAILY_REPORT_BACKUPS\DIGI_Review_Terry.xlsm" Set wb2 = ActiveWorkbook wb2.ActiveSheet.Range("A1:XFD70").Copy wb.Sheets("Terry").Range("A1").PasteSpecial Paste:=xlPasteAll wb2.Close wb.Save End Sub

您没有更新行,您正在修改正在更新行的值。

答案 1 :(得分:0)

您无法更新执行触发器的同一个表。由于您使用的是before update,因此需要设置new的{​​{1}}值

因此触发器变为

AvailabilityRate