Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
i want update status based on dates for that one

Example

SQL
create trigger emp_trigger on emp
after insert
as
begin
declare @todate date
declare @fromdate Date
declare @dt Date
Set @todate='2011-12-01'
set @fromdate='2011-12-06'
set @dt='2011-12-08'
if @dt not between  @todate and @fromdate
begin
update emp set status='expire' where empid=1
end
End

I am writting this trigger, but i am not able to update this check it
Posted
Updated 8-Dec-11 19:34pm
v3
Comments
Al Moje 9-Dec-11 1:33am    
Replace 'aable' with 'not able'
Karthik Harve 9-Dec-11 1:34am    
Added pre tags.

Hello

This trigger is a bit weird as it will only ever update one row where empid=1! so if the row does not exist it will do nothing.

If you want to update the row you are currently inserting you should remove the where clause from the trigger statement.

SQL
create trigger emp_trigger 
on emp
after insert
as
begin
    declare @todate date
    declare @fromdate Date
    declare @dt Date
    Set @todate='2011-12-01'
    set @fromdate='2011-12-06'
    set @dt='2011-12-08'
    if @dt not between  @todate and @fromdate
    begin
        update emp set status='expire' 
    end
End


Valery.
 
Share this answer
 
Comments
RaviRanjanKr 11-Dec-11 16:35pm    
5!
In your trigger you only updated the row with ID of 1 ! So what about other conditions ?

You can access inserted or deleted rows from the inserted and deleted tables.
So have look at here :
http://msdn.microsoft.com/en-us/library/aa214435%28v=sql.80%29.aspx[^]

Another thing is that you don't want constant dates in your trigger, if so it's not necessary to have a trigger !

So your trigger can be like this :
SQL
create trigger emp_trigger on emp
after insert
as
begin
declare @todate date
declare @fromdate Date
declare @dt Date
Set @todate='2011-12-01'
set @fromdate='2011-12-06'


update emp set status= 'expired'      where Id in (select ID from inserted i where not i.dt between @fromdate and @todate )
update emp set status= 'not expired'  where Id in (select ID from inserted i where     i.dt between @fromdate and @todate )	

End


In this trigger its assumed that there is a dt field in your emp table and it decides on that field that what should be in status field.

Hope it helps.
 
Share this answer
 
v2
Comments
RaviRanjanKr 11-Dec-11 16:34pm    
5!
Amir Mahfoozi 11-Dec-11 23:56pm    
Thanks :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900