简体   繁体   English

如何验证这些列,我是否需要更复杂的触发器?

[英]How do I validate these columns, do i need more complex triggers?

I'm trying to validate the columns in the table ProjectDetails.TimeCards 我正在尝试验证表ProjectDetails.TimeCards中的列

create table ProjectDetails.TimeCards(
    Time_Card_ID int identity (55,15),
    Employee_ID int foreign key references HumanResources.Employees(Employee_ID),
    Date_Issued date, --DateIssued should be greater than the current date and the project start date (Project start date is from another table). 
    Days_Worked int constraint chk_Days_Worked check(Days_Worked > '0'),
    Project_ID int foreign key references ProjectDetails.Projects(Project_ID),
    Billable_hours int constraint chk_Billable_Hours check (Billable_Hours > '0'),
    Total_Cost money, -- should be automatically calculated by using the following formula: TotalCost=Billable Hours * BillingRate (billing rate is from another table) 
    Work_Code_ID int foreign key references ProjectDetails.WorkCodes(Work_Code_ID)
    );

I tried building a trigger, but was only able to get the trigger to fire if the Date_Issued was less than the current date 我尝试构建触发器,但是只有在Date_Issued小于当前日期时,才能触发触发器

CREATE TRIGGER dateissued
    ON ProjectDetails.TimeCards
    FOR INSERT
    AS
      DECLARE @ModifiedDate date
      SELECT @ModifiedDate = Date_Issued FROM Inserted
          IF (@ModifiedDate < getdate())
          BEGIN
            PRINT 'The modified date should be the current date. Hence, cannot insert.'
            ROLLBACK TRANSACTION -- transaction is to be rolled back
          END

i need the trigger to fire if the Date issued is less than the current date and also if date issued is less than the start_date. 如果发布日期小于当前日期,并且发布日期小于start_date,则需要触发触发器。 As for the billing rate calculation i'm lost there. 至于计费率的计算,我迷路了。 This is the other table 这是另一张桌子

create table ProjectDetails.Projects(
    Project_ID int identity (0100, 01) primary key, -- Primary key
    Project_Name char (50) not null, 
    Start_Date date not null, -- the start date i'm referring to
    End_Date date not null,
    constraint CheckEndLaterThanStart check (End_Date > Start_Date),
    Billing_Estimate money constraint chk_Billing_Estimate check (Billing_Estimate > '1000'),
    Client_ID int Foreign Key references CustomerDetails.Clients(Client_ID)
);

I believe this provides the logic you are after. 我相信这提供了您追求的逻辑。 There are a couple of comments in the code for you: 代码中为您提供了一些注释:

CREATE TRIGGER trg_chk_Date_Issued ON ProjectDetails.TimeCards
FOR INSERT, UPDATE --I assume on need on UPDATE as well, as otherwise this won't validate
AS BEGIN

    IF EXISTS(SELECT 1
              FROM inserted i
                   JOIN ProjectDetails.Projects P ON i.Project_ID = P.Project_ID
              WHERE i.Date_Issued < CONVERT(date,GETDATE()) 
                 OR i.Date_Issued < P.Start_Date) BEGIN
       RAISERROR(N'Date Issued cannot be less than the current date or the Project Start Date.', 10,1); --Raised an error, rather than using PRINT
       ROLLBACK;
    END
END

Note that you may want a different trigger for UPDATE , as if you are updating the row, and Date_Issued has a value lower than the date of the UPDATE , the statement will fail. 请注意,您可能需要一个不同的UPDATE触发器,就像您要更新该行一样,并且Date_Issued的值小于UPDATE的日期,该语句将失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM