Error 1111 when calling a stored function - mysql

So I found a post that talked about putting certain logic tests behind HAVING instead of WHERE. What am I missing that's still tripping error 1111?
DROP PROCEDURE IF EXISTS ELO;
DELIMITER //
CREATE PROCEDURE ELO()
BEGIN
-- declares...
label1: WHILE xGame <= max(games.game_id) DO
SELECT games.game_id, games.game_type, games.date, games.home_team, games.away_team, games.runs0, games.runs1
FROM games
WHERE games.game_id = xGame
HAVING (games.game_type = 0 OR games.game_type = 3)
INTO #id, #ty, #d, #home, #away, #homeR, #awayR;
SET startHomeELO = (SELECT team_elo.team_id, team_elo.date, team_elo.elo FROM team_elo WHERE team_id = home HAVING min(d - team_elo.date));
SET startAwayELO = (SELECT team_elo.team_id, team_elo.date, team_elo.elo FROM team_elo WHERE team_id = away HAVING min(d - team_elo.date));
SET eloDiff = abs((startHomeELO + 25) - startAwayELO);
SET homeELO = (startHomeELO + (40*power(runDiff, (1/3)) * (homeWin - (1/(power(10, (eloDiff/400)) + 1)))));
SET awayELO = (startAwayELO + (40*power(runDiff, (1/3)) * (awayWin - (1/(power(10, (eloDiff/400)) + 1)))));
INSERT INTO team_elo (team_id, game_id, date, elo) VALUES (home, id, d, homeELO);
INSERT INTO team_elo (team_id, game_id, date, elo) VALUES (away, id, d, awayELO);
SET xGame = xGame + 1;
END WHILE label1;
END; //
CALL ELO();

You need to do a separate query to get the maximum ID:
INT maxId;
SELECT MAX(game_id) INTO maxId
FROM games;
label1: while xGame <= maxId DO
...
END WHILE label1;

Related

MySql Stored Procedure error in WHERE condition

I'm trying to write my first mySql stored procedure and keep on getting an error from the server that I am unable to understand, hope someone will be able to help me fixing it.
What I am doing
I collect some parameters from social networks, and I need to save this data in two different tables. I know that the table schema might not be optimal, but this is something I cannot change at the moment.
The idea is that I call the stored procedure from my server-side code passing in article ID and some other parameters, and the procedure:
Updates the "articles" table
Inserts anew record into the "popularity" tables with some values that are the result of the previous UPDATE
This is the stored procedure I wrote
BEGIN
UPDATE
articles2
SET
fb_shares = n_shares,
fb_comments = n_comments,
fb_reactions = n_reactions,
tw_tweets = #tweets :=(tw_tweets + n_tweets),
tw_retweets = #retweets :=(tw_retweets + n_retweets),
tw_favorites = #favorites :=(tw_favorites + n_favorites),
tw_reach = #reach :=(tw_reach + n_reach),
tw_since_id = n_since_id,
popularity = #popularity :=(
(n_shares * fb_shares_weight) +(
n_comments * fb_comments_weight
) +(
n_reactions * fb_reactions_weight
) +(#tweets * tw_tweets_weight) +(#retweets * tw_retweets_weight) +(
#favorites * tw_favorites_weight
) +(#reach * tw_reach_weight)
),
popularity_updated =(popularity_updated + 1)
WHERE
id = n_id ;
INSERT
INTO
popularity(
article_id,
added,
popularity,
tw_tweets,
tw_reach,
tw_favorites,
tw_retweets,
tw_since_id,
fb_shares,
fb_comments,
fb_reactions
)
VALUES(
n_id,
NOW(), #popularity, #tweets, #reach, #favorites, #retweets, n_since_id, n_shares, n_comments, n_reactions) ;
END
I keep getting an error #1416 - Cannot get geometry object from data you send to the GEOMETRY field and the INSERT is never performed. I suppose that the variables assignment is wrong, but cannot understand how to fix it.
As said, I never wrote a stored procedure before, and since that line looks correct to me, I really cannot understand what's wrong. I cannot exclude that I am trying to do something that should not be done with a stored procedure, but the few examples that I found online makes me think this should be correct...
Thank you in advance,
Simone
Edit:
I got rid of that error, but still the INSERT is not performed... here is the updated stored procedure:
BEGIN
SET #tweets := 0, #retweets := 0, #favorites := 0, #reach := 0, #popularity := 0;
UPDATE
articles2
SET
fb_shares = n_shares,
fb_comments = n_comments,
fb_reactions = n_reactions,
tw_tweets = #tweets :=(tw_tweets + n_tweets),
tw_retweets = #retweets :=(tw_retweets + n_retweets),
tw_favorites = #favorites :=(tw_favorites + n_favorites),
tw_reach = #reach :=(tw_reach + n_reach),
tw_since_id = n_since_id,
popularity = #popularity :=(
(n_shares * fb_shares_weight) +(
n_comments * fb_comments_weight
) +(
n_reactions * fb_reactions_weight
) +(#tweets * tw_tweets_weight) +(#retweets * tw_retweets_weight) +(
#favorites * tw_favorites_weight
) +(#reach * tw_reach_weight)
),
popularity_updated =(popularity_updated + 1)
WHERE
id = n_id ;
SELECT #tweets, #retweets, #favorites, #reach, #popularity;
INSERT
INTO
popularity(
article_id,
added,
popularity,
tw_tweets,
tw_reach,
tw_favorites,
tw_retweets,
tw_since_id,
fb_shares,
fb_comments,
fb_reactions
)
VALUES(
n_id,
NOW(), #popularity, #tweets, #reach, #favorites, #retweets, n_since_id, n_shares, n_comments, n_reactions) ;
END
Check the definition of the popularity table for a field defined with data type GEOMETRY and change to appropriate type.

Performance issue with update query after adding index

I have added the index to my update query and by adding the same query start taking several hours to complete the process.While without index its completing in some minutes i have added the index to faster the process but it became very slow exactly opposite to my desire.
Below is sample code snippet of my code.
Cursor c_updt_stg_rsn is
select distinct substr(r.state_claim_id, 1, 12), ROWID
from nemis.stg_state_resp_rsn r
WHERE r.seq_resp_plan_id = v_seq_resp_plan_id
and r.submitted_claim_id is null
and r.filler_2 is null;
BEGIN
OPEN c_updt_stg_rsn;
LOOP
FETCH c_updt_stg_rsn BULK COLLECT
INTO v_state_claim_id, v_rowid LIMIT c_BULK_SIZE;
FORALL i IN 1 .. v_state_claim_id.COUNT()
UPDATE /*+ index(STG_STATE_RESP_RSN,IDX2_STG_STATE_RESP_RSN) */ nemis.stg_state_resp_rsn
SET (submitted_claim_id , filler_2) = (SELECT DISTINCT submitted_claim_id, sl_group_id FROM nemis.state_sub_Resp_dtl D WHERE
(d.state, d.type_of_claim) in (select distinct state, type_of_claim
from nemis.resp_match_state
where seq_resp_match_table_level_id in
(select seq_resp_match_table_level_id
from nemis.resp_match_table_level
where seq_resp_plan_id = v_seq_resp_plan_id))
AND resp_state_claim_id LIKE v_state_claim_id(i)||'%'
)
WHERE ROWID = v_rowid(i);
IF v_state_claim_id.COUNT() != 0 THEN
v_cnt_rsn := v_cnt_rsn + SQL%ROWCOUNT;
END IF;
COMMIT;
EXIT WHEN c_updt_stg_rsn%NOTFOUND;
END LOOP;
CLOSE c_updt_stg_rsn;

Django mysql tuple index out of range

My views.py looks like following:
#api_view(('GET',))
def save_progress_for_particular_user(request, exerciseId, rating):
#view to call stored proc that stores the progress for a particular user
#save_progress(user_id,exercise_id,rating,date)
result=Exercise_progress_model.save_progress_for_particular_user(
request.user.id, exerciseId, rating, datetime.date.today())
serializer = ExerciseProgressSerializer(result, many=True)
return Response(serializer.data)
My models.py looks like following:
class Exercise_progress_model(models.Model):
# Model that will be rendered on the UI
#Field for storing intensity level
INTENSITY_LEVEL_CHOICES = (
(1, 'Really simple'),
(2, 'Rather Simple'),
(3, 'Simple'),
(4, 'Okay'),
(5, 'Difficult'),
(6, 'Rather Difficult'),
(7, 'Really Difficult'),
)
intensity_level = models.IntegerField(choices=INTENSITY_LEVEL_CHOICES)
#Field for storing progress
progress = models.IntegerField(default='0')
#Field for storing exercise type
EXERCISE_TYPE_CHOICES = (
(1, 'Best stretch'),
(2, 'Butterfly reverse'),
(3, 'Squat row'),
(4, 'Plank'),
(5, 'Push up'),
(6, 'Side plank'),
(7, 'Squat'),
)
exercise_id = models.IntegerField(choices=EXERCISE_TYPE_CHOICES)
#Field for storing current date
current_date = models.DateField()
#Field for storing user rating
user_rating = models.IntegerField(default='0')
# static method to save progress for a particular user
#staticmethod
def save_progress_for_particular_user(user_id, exercise_id, rating, date):
# create a cursor
cur = connection.cursor()
# execute the stored procedure passing in
# search_string as a parameter
cur.callproc('save_exercise_state', [user_id, exercise_id, rating, date,])
# grab the results
result = cur.fetchall()
cur.close()
User_progress_list = []
for row in result:
Epm = Exercise_progress_model()
Epm.intensity_level=row[0]
Epm.progress=row[1]
Epm.exercise_id=row[2]
Epm.user_rating=row[3]
Epm.current_date=row[4]
User_progress_list.append(Epm)
return User_progress_list
My serializers.py looks like following:
class ExerciseProgressSerializer(serializers.Serializer):
intensity_level = serializers.IntegerField(required=True)
progress = serializers.IntegerField(required=True)
exercise_type = serializers.IntegerField(required=True)
user_rating = serializers.IntegerField(required=True)
current_date = serializers.DateField()
My stored procedure looks like following:
delimiter //
CREATE PROCEDURE save_exercise_state(
p_user_id int, p_exercise_id int, p_rating int, p_date date
)
BEGIN
DECLARE lv_current_intensity, lv_progress, lv_final_intensity int;
DECLARE lv_date date;
select intensity_level into lv_current_intensity
from demo_exercise_state where
user_id=p_user_id and exercise_id=p_exercise_id;
select progress into lv_progress
from demo_exercise_state where
user_id=p_user_id and exercise_id=p_exercise_id;
select current_date into lv_date
from demo_exercise_state where
user_id=p_user_id and exercise_id=p_exercise_id;
select lv_progress, lv_current_intensity, lv_date;
If lv_date=p_date then
select "OK";
end if;
if p_rating=0 then
set lv_progress=lv_progress;
elseif p_rating=1 then
if lv_progress=3 then
set lv_progress=0;
if lv_current_intensity = 7 then
set lv_final_intensity = 7;
else
set lv_final_intensity = lv_current_intensity + 1;
end if;
else
if lv_current_intensity=7 then
set lv_progress=0;
set lv_final_intensity=lv_current_intensity;
else
set lv_progress=lv_progress+1;
set lv_final_intensity=lv_current_intensity;
end if;
end if;
elseif p_rating =-1 then
if lv_progress=0 then
if lv_current_intensity = 1 then
set lv_final_intensity = 1;
set lv_progress=0;
else
set lv_final_intensity = lv_current_intensity - 1;
set lv_progress=3;
end if;
else
set lv_progress=lv_progress-1;
set lv_final_intensity=lv_current_intensity;
end if;
end if;
Update demo_exercise_state
SET progress = lv_progress,
intensity_level = lv_final_intensity,
`current_date` = p_date,
user_rating = p_rating
WHERE user_id = p_user_id AND exercise_id = p_exercise_id;
SELECT intensity_level, progress, exercise_id, user_rating , current_date
FROM demo_exercise_state
WHERE user_id = p_user_id AND exercise_id = p_exercise_id AND user_rating=p_rating;
END
//
Screenshot of the result of executing this proc for
call save_exercise_state(4,1,1,curdate()); is as following
When I call this view I get tuple index out of range error for the line Epm.user_rating=row[3].
What is the reason for this? Is there any workaround?

error message not displaying on mysql stored procedure

I am trying to validate something but It always returns a success result. I wonder what's wrong with my codes.
INSERT INTO playlist_details (playlist_id,filename,image_id,transition,timeframe,userid,update_date)
VALUES (i_playlistid,i_filename,i_imageid, i_transition, i_time, i_userid,i_date);
SELECT booking_id INTO v_booking_id FROM staging_table WHERE playlist_id = i_playlistid LIMIT 1;
SELECT num_of_spots INTO v_spots FROM booking_sum WHERE booking_id = v_booking_id;
SET v_allowed_time = v_spots * 30;
IF ((SELECT SUM(timeframe) FROM playlist_details WHERE playlist_id = i_playlistid) > v_allowed_time) THEN
SET o_success = FALSE;
SET o_message = 'You exceeded the time allowed with your booking';
SELECT playlist_id,filename,image_id,transition,timeframe,userid FROM playlist_details
WHERE userid = i_userid;
ELSE
SELECT playlist_id,filename,image_id,transition,timeframe,userid FROM playlist_details
WHERE userid = i_userid;
SET o_success = TRUE;
SET o_message = 'Success';
END IF;

MySQL Stored Procedure with IF/THEN/ELSE

I have a MySQL stored procedure and in it, the following WHILE statement.
I have confirmed that #RowCnt is 1, and #MaxRows is 6090, however after further debugging, I realized that the WHILE statement is going through a single iteration and not continuing; so I'm hoping to have some light shed on what could possibly be causing this.
Full disclosure: I ported this from SQL Server to a MySQL stored procedure, something I have never taken on before. (meaning SQL Server, porting OR stored procedures..)
WHILE #RowCnt <= #MaxRows DO
SELECT #currentReadSeq:=ReadSeq, #currentReadStrength:=ReadStrength, #currentReadDateTime:=ReadDateTime, #currentReaderID:=ReaderID FROM tblTempRead WHERE rownum = #RowCnt;
IF ( ((#lastReadSeq + 10) > #currentReadSeq) AND (#lastReaderId = #currentReaderId) ) THEN
SET #lastReadSeq = #currentReadSeq, #lastReadStrength = #currentReadStrength, #lastReadDateTime = #currentReadDateTime, #lastReaderID = #currentReaderID;
ELSE
INSERT INTO tblreaddataresults (SiteID, ReadDateTimeStart, ReadDateTimeEnd, ReadSeqStart, ReadSeqEnd, ReaderID, DirectSeconds) VALUES ('1002', #saveReadDateTime, #lastReadDateTime, #saveReadSeq, #lastReadSeq, #lastReaderID, timestampdiff(SECOND,#saveReadDateTime,#lastReadDateTime));
SET #saveReadSeq = #currentReadSeq, #saveReadStrength = #currentReadStrength, #saveReadDateTime = #currentReadDateTime, #saveReaderID = #currentReaderID;
SET #lastReadSeq = #saveReadSeq, #lastReadStrength = #saveReadStrength, #lastReadDateTime = #saveReadDateTime, #lastReaderID = #saveReaderID;
END IF;
SET #RowCnt = #RowCnt+1;
END WHILE;
Try This Construct
WHILE (#RowCnt <= #MaxRows)
BEGIN
SELECT #currentReadSeq:=ReadSeq, #currentReadStrength:=ReadStrength, #currentReadDateTime:=ReadDateTime, #currentReaderID:=ReaderID FROM tblTempRead WHERE rownum = #RowCnt;
IF (((#lastReadSeq + 10) > #currentReadSeq) AND (#lastReaderId = #currentReaderId))
BEGIN
SET #lastReadSeq = #currentReadSeq, #lastReadStrength = #currentReadStrength, #lastReadDateTime = #currentReadDateTime, #lastReaderID = #currentReaderID;
END
ELSE
BEGIN
INSERT INTO tblreaddataresults (SiteID, ReadDateTimeStart, ReadDateTimeEnd,ReadSeqStart, ReadSeqEnd, ReaderID, DirectSeconds) VALUES ('1002',#saveReadDateTime, #lastReadDateTime, #saveReadSeq, #lastReadSeq, #lastReaderID,timestampdiff(SECOND,#saveReadDateTime,#lastReadDateTime));
SET #saveReadSeq = #currentReadSeq, #saveReadStrength = #currentReadStrength, #saveReadDateTime = #currentReadDateTime, #saveReaderID = #currentReaderID;
SET #lastReadSeq = #saveReadSeq, #lastReadStrength = #saveReadStrength,#lastReadDateTime = #saveReadDateTime, #lastReaderID = #saveReaderID;
END
SET #RowCnt = #RowCnt+1;
END