2

Dear i already create one table for employee information with ASP.net interface now i show add one operation in my table as below,

i have four columns( WorkDatey,WorkDatem,WorkDated,absentday) all the columns from integer type in table name employee. now i should add new column (realworkdays) and the result here should be automatic save after entering previous data as example:

    WorkDatey= 2011 ,
    WorkDatem= 2  ,
    WorkDated=14 ,
    absentday=6

the operation should be : (2011*365)+(2*30)+(14)-(6) and result will store in column (realworkdays).

i already try it as trigger but i have some thing wrong:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER daysResult
   ON  [dbo].[employee]
   AFTER insert
As
DECLARE @realworkdays int

BEGIN
    SET NOCOUNT ON;


select WorkDatey,WorkDatem,WorkDated,absent from [dbo].[employee]
@realworkdays= (workdatey*350)+(workdatem*29)+(workdated)-(absent)

insert into [dbo].[employee] (totaldays)values (@realworkdays)

END
GO
3
  • 3
    You example maths and your trigger code disagree on whether you're multiplying the months value by 30 or 29, and years by 365 or 350. Is there a reason why you're storing what's obviously a date in something other than a datetime column? If you could add more information about what this calculation is meant to mean, we may be able to offer better suggestions for the schema as a whole. Commented Feb 22, 2011 at 7:53
  • 1
    Are you actually aware of the datetime data type in SQL? I mean, it's alright if you didn't use it because you didn't know about it, but I've just made it certain that you do know, and maybe now it's about time to consider redesigning your data model. Commented Feb 22, 2011 at 8:09
  • Dear Andriy: i know the the better is using datetime but i'm work in goverment and i start my work with them from 2 weeks and the design alreday exists from 3 years (the old database devlober was not using datetime at all and he just using int, smallint,tinyint- i don't know why) anyway now it's not possible to chage the disign:( Commented Feb 22, 2011 at 18:26

2 Answers 2

4

Since you are using sql server 2005, i would suggest you use a computed column

ALTER TABLE employee DROP COLUMN realworkdays;
ALTER TABLE employee ADD COLUMN realworkdays AS (
    (WorkDatey * 365) +
    (WorkDatem * 30) +
    (WorkDated) -
    (absentday)
)

You can add the PERSISTED so it will store the data, optionally indexing it.

On a side note, your trigger is inserted a new row, instead of updating the value of the current row.

Sign up to request clarification or add additional context in comments.

2 Comments

Computed column was available in SQL Server 2000. I can't recall if it was in SQL Server 7.0
thanks Meister, actullay i couldn't recall computed column in SQL server 2005 , but the trigger was going ok with some modifiy
0

You are supposed to update the inserted records, not insert new ones.

So, it should be the UPDATE statement, and you can do both the calculations and the updates in one go.

CREATE TRIGGER daysResult
ON [dbo].[employee]
AFTER insert
AS
BEGIN
  UPDATE employee
  SET realworkdays = (inserted.workdatey*350)+(inserted.workdatem*29)+
                     (inserted.workdated)-(inserted.absent)
  FROM inserted
  WHERE employee.ID = inserted.ID
END

Still it would be better to use a computed column, as @The Scrum Meister has suggested.

1 Comment

thanks Andriy, i fixed the trigger to update statment, but just to make the code workable for any one wanted, we should add inserted before each column as below: CREATE TRIGGER daysResult ON [dbo].[employee] AFTER insert AS BEGIN UPDATE employee SET realworkdays = (inserted.workdatey*350)+(inserted.workdatem*29)+(inserted.workdated)-(inserted.absent) FROM inserted WHERE employee.ID = inserted.ID END

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.