A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
this is the trigger i created its calculating expiry date and buydate as soon as PackageStatus changes to true along with this i also calculated refEarning which was my need to calculate and insert as soon as package status changes to true
CREATE TRIGGER MyTrigger ON UserPackages
after update
as
DECLARE @PId INT
DECLARE *anonymous user* INT
declare @Val INT
SELECT @PId = inserted.P_ID ,*anonymous user*=inserted.U_ID
FROM INSERTED
select @Val=Packages.Validitiy from Packages where Packages.PID=@PId
update UserPackages
set BuyDate=GETDATE() , Expirydate = dateadd(dd,@Val,getdate())
where P_ID=@PId and U_ID=*anonymous user* and PackageStatus='True'
;
with firstCte(Name,UID,PName,Price,ReferComission,ReferredBy)
as
(
select distinct Users.Name,Users.ID,Packages.PName,Packages.Price,Packages.ReferCommission,(
select distinct Users.ID from users inner join Refers on Users.UserRef_No=Refers.RefOf
)as referedby
from Users inner join UserPackages on UserPackages.U_ID=Users.ID
inner join Packages on Packages.PID=UserPackages.P_ID
inner join Refers on Users.Ref_No=Refers.RefOf
where U_ID=*anonymous user* and UserPackages.PackageStatus='true'
)
--select Name,PName,Price,ReferComission,ReferredBy,RefEarning=Price*ReferComission/100 from firstCte
-- Define the outer query referencing the CTE name.
update Users set RefEarning+= Price*ReferComission/100 from firstCte where ID=ReferredBy ;
--with secondCte(UID,Name,Active)
--as
--(select ID,Name,Active from Users where ID=*anonymous user* and Active='False')
update Users set Active='True' where ID=*anonymous user* and Active='False'
Please share your point of view about the above trigger efficiency or inefficiency
