我想防止将数据插入表中,并在“OrderDate”列大于“2021-01-01”时返回“错误”。如何通过在 sql-server v.2019 中使用 TRIGGER 和 TRY...CATCH 来做到这一点
订单ID | 订购日期 |
---|---|
10015 | 2019-05-06 |
10016 | 2020-06-01 |
10017 | 2020-06-02 |
CREATE TRIGGER date_check ON Sales.Orders AFTER INSERT
AS
BEGIN TRY
INSERTED.orderdate<'2021-01-01'
PRINT 'NO ERROE'
END TRY
BEGIN CATCH
PRINT 'ERROR'
END CATCH
GO
IF INSERTED.orderdate>='2021-01-01'
BEGIN
RAISERROR ('condition doesn't satisfy something', 16, 1)
ROLLBACK TRANSACTION
END