I want to check table PREMIUM_SERVICE_USER
if any records exists for
strClientID
update timeValid
for +30 if no records for strClientID
insert to premium_service_user
table.
What am I doing wrong?
It increases timeValid
for +30 days but inserts another row too.
SELECT @pre_var = count(*)
FROM PREMIUM_SERVICE_USER
WHERE strClientID = @strClientID
/* bronze premium - 200 cash */
IF @Premium = 1
BEGIN
INSERT INTO PREMIUM_SERVICE_USER
(strClientID, timeReg, timeValid, bCurrent, durum)
VALUES
(@strClientID,getdate(),getdate() + 30,'1','1')
UPDATE TB_USER
SET cash = cash+200
WHERE strAccountID = @strClientID
END
IF @Premium = 1 AND @pre_var = 1
BEGIN
UPDATE PREMIUM_SERVICE_USER
SET timevalid = timevalid+30 where strClientID = @strClientID
UPDATE PREMIUM_SERVICE_USER
SET bCurrent = 1 where strClientID = @strClientID
UPDATE TB_USER
SET cash = cash+200 WHERE strAccountID = @strClientID
END
Your problem was running the first
if
without regard to the value of@pre_var
.This is a slightly different way of doing it which will be slightly more efficient if
PREMIUM_SERVICE_USER
is large.Looks like you are checking the wrong variable in the first IF Statement. If @Premium = 1 then you will see this behavior.
You're counting the rows, but not using it in your decision making. Here is a decision structure that may be useful
Or this one..