具有日期约束的SQL Server 2014触发器

时间:2018-11-20 17:00:30

标签: sql-server date triggers

我有下面的代码不起作用,因为它无法识别表中的列

代码:

create trigger cerinta1 
on Factura
for insert 
as
    if YEAR(DataFactura) < 2010
    begin
        raiserror ('Can t add the row!',15,2)
    end

1 个答案:

答案 0 :(得分:3)

在引用列时,您仍然需要说出它属于哪个对象。 DataFactura本身没有任何意义。试试:

CREATE TRIGGER dbo.cerinta1
ON dbo.Factura
FOR INSERT
AS
    IF EXISTS (SELECT 1 FROM inserted WHERE DataFactura < '20100101') --Rather than using YEAR, pass an actual date
    BEGIN
        RAISERROR('Can''t add data before 2010!', 15, 2); --A more descriptive error is better.
    END;