Libin TK
Libin TK

Reputation: 1487

Using SQL Server Trigger for updating old rows on Insert

I have users table containing all the user account details including a IsLoggedIn field with bit data type. I want to update any old rows with IsLoggedIn = 1 when a user starts new session before previous session expires. What is the best way to achieve this? Is it possible to do this with Trigger?

Thanks in advance..

Upvotes: 2

Views: 225

Answers (2)

NG.
NG.

Reputation: 6043

Trigger won't be the suggested solution as you need to handle login from UI. If there is going to be only a single login then Application_Start can be used. If there can be multiple logins then Session_Start is preferable. From there you can give a call to database and perform the respective actions.
Difference between the two is explained at : http://forums.asp.net/t/1230163.aspx/1?What+s+the+difference+between+Application_Start+and+Session_Start+in+Global+aspx+

Upvotes: 1

Jigar Pandya
Jigar Pandya

Reputation: 5977

You will have two things for sure.. 1) User ID and 2) Session Id in database.

Now when new session start (session_Start) event gets fire.. you can call one store procedure with logic and the logic will be

1) If there is not records with same userID do not do any update. 2) If there are any records with same userID update all of them with IsLoggedIn = 1.

You can extend this logic with TRIGGER as well and band it as per your need,.

Basically you need to track session_Start event and perform DB operations.

Upvotes: 1

Related Questions