4

Guys i can not find solution for this problem , its always giving syntax error what ever i have tried ... can you just cast a look for me , thanks

create procedure SP_Insert(in MatchIDP int,in TipID int, in User int)
begin

if exists(
select BetSlipID from betslips where MatchID = MatchIDP and UserID = User)
(
   update Betslips set TipID = 2
)
else
(
   insert into Betslips (MatchID,TipID , UserID) value (MatchIDP,TipID,User)
)
end if
end

I just want to check if data exists in table before i do insert , and i can not use "On duplicate key update" because my primary key does not mean anything , its table where i put in 2-3 foreign keys ....

2 Answers 2

2

Your IF syntax is incorrect. It should be:

delimiter ;;

create procedure SP_Insert(in MatchIDP int,in TipID int, in User int)
begin

if exists(
  select * from betslips where MatchID = MatchIDP and UserID = User
) then
  update Betslips set TipID = 2; -- where ?
else
  insert into Betslips (MatchID,TipID , UserID) values (MatchIDP, TipID, User);
end if;

end;;

However, if you will never permit duplicate (MatchID, UserID) entries in your Betslips, why not define a UNIQUE constraint across those columns and then use INSERT ... ON DUPLICATE KEY UPDATE:

ALTER TABLE Betslips ADD UNIQUE INDEX (MatchID, UserID);

INSERT INTO Betslips (MatchID, TipID, UserID) VALUES (?, ?, ?)
ON DUPLICATE KEY UPDATE TipID = 2;
Sign up to request clarification or add additional context in comments.

5 Comments

thanks for answer mate , idea is that i never have one user to bet on same game more then once , that is reason why i am checking if that data exists
@Veljko89: Then I'd go with my second suggestion, of defining an appropriate UNIQUE constraint and then using INSERT ... ON DUPLICATE KEY UPDATE.
But INSERT ... ON DUPLICATE KEY UPDATE i can not check MatchID and UserID combo exists , and that is thing what matters to me... right?
@Veljko89: That's exactly what it will do if you define the UNIQUE constraint as advised in my answer.
hmm in that case ill google a bit more about it ... thanks a lot mate
0
CREATE PROCEDURE SP_Insert (IN MatchIDP INT, IN TipID INT, IN USER INT)
BEGIN
        DECLARE existing INT DEFAULT NULL;

        SELECT BetSlipID
        INTO   existing
        FROM   betslips
        WHERE  MatchID = MatchIDP
        AND    UserID = USER;

        IF existing is not null THEN
            UPDATE Betslips SET TipID = 2;
        ELSE
            INSERT INTO Betslips (MatchID, TipID, UserID)
            VALUES (MatchIDP, TipID, USER);
        END IF;

END

2 Comments

@eggyal Then you can easily add an equal sign to existing > 0 => existing >= 0. I changed the condition, checking against null.
Okay, what if there's a matching record with BetSlipID=NULL (perhaps not possible in the OP's data model, but nevertheless this is a bit sloppy... surely EXISTS was a more correct test?)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.