简体   繁体   中英

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

I'm trying to validate the columns in the table 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

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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