Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I have two tables

table1:
_____________________
id |enddate|userid
_____________________
1 |27/10/2010|1
2 |28/3/2011 |2
3 |30/3/2011 |1
4 |30/4/2011 |2
_____________________

table2 :
_______________________
userid | statues |value
_______________________
1 |active |0
2 |active |0
_______________________


I want to get last inserted date for user from table1 and if datenow>= lastdate
update table2 and change statues from active to suspended .

If datenow < lastdate then update teble2 and change value to 1 and next day change to 2 until datenow=lastdate and change statues.
Posted
Updated 31-Mar-11 4:46am
v2
Comments
Dalek Dave 31-Mar-11 10:47am    
Edited for Grammar, Syntax and Readability.

You can get userid and last inserted enddate using the following query:

SQL
SELECT DISTINCT userid, MAX(enddate) AS enddate FROM table1
GROUP BY userid


Now write your logic to update table2 as you like.
 
Share this answer
 
v2
Comments
Dalek Dave 31-Mar-11 10:47am    
Good Call.
I write one SP that meets your requirement it takes userid as input parameter. In the SP it picks latest enddate of that user and compare with current date. According to the condition it update status also.

create procedure spCheckStatus @userid int
as
begin
declare @EndDate date
declare @CurrentDate date

select @EndDate=max(enddate) from table1 where userid=@userid
select @CurrentDate=getdate()

if @CurrentDate >= @EndDate
begin
update table2 set status='suspend' where userid=@userid
end

if @CurrentDate < @EndDate
begin
update table2 set status='active' where userid=@userid
end

end


try it....Hope its work for you.
 
Share this answer
 
Comments
shms_rony 31-Mar-11 10:11am    
hanx all

Ratnesh N Bharos

1-i have one problem of ths code
getdate() return date with time ..i want to format it as dd/M/yyyy

because currentdate inserted as this form dd/M/yyyy without hours
2-and how to call this procedure in class

thank you
Dalek Dave 31-Mar-11 10:47am    
Good Answer.

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