mysql syntax error on creating stored procedure - mysql

There are tons of similar posts, but still couldn't solve the error.
Here's my stored procedure query:
DROP PROCEDURE IF EXISTS spa_gui_get_next_task;
DELIMITER //
CREATE PROCEDURE `spa_gui_get_next_task`(IN var_user_id INT(11), IN var_task_id INT(11))
BEGIN
-- create temp table
CREATE TEMPORARY TABLE temp_table_user_tasks engine=memory
SELECT
tsk.id,
tsk.request_id,
tsk.sys_index,
tsk.category_group,
tsk.category,
tsk.is_assigned,
tsk.hash_id
FROM
user_tasks as usr
inner join
unassigned_tasks as tsk
on usr.task_id = tsk.id
WHERE
usr.assigned_to = var_user_id
AND
tsk.product_id NOT IN ( SELECT product_id FROM product_progresses WHERE request_id = tsk.request_id )
AND
BINARY hash_id NOT IN ( SELECT hash_id FROM product_match_unmatches WHERE request_id = tsk.request_id AND (is_matched=1 OR auto_unmatched_by IS NOT NULL) )
ORDER BY tsk.id;
-- fetch next row
SELECT qs.*
FROM temp_table_user_tasks as qs
WHERE qs.id = ( SELECT min(qs.id) FROM temp_table_user_tasks as qt WHERE qt.id > var_task_id);
-- drop temp table
DROP TEMPORARY TABLE IF EXISTS temp_table_user_tasks;
END //
DELIMITER ;
I am getting this error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DELIMITER //
CREATE PROCEDURE `spa_gui_get_next_task`(IN var_user_id INT(11), ' at line 1
Also the same query is working on phpmyadmin 10.1.26-MariaDB, and even tried exporting the same stored procedure from phpmyadmin to another mysql v5.7.12.

Related

Mysql procedure insert into temporary table

I have written a mysql procedure. I want into a temporary table using different select statement. While creating procedure its showing syntax error near at '
SELECT
value as last
FROM
wp_13_rg_lead
LEFT JOIN wp_13_rg_' at line 18
Below is my procedure
DELIMITER $$
CREATE PROCEDURE TEMP_JOIN_WORK ()
BEGIN
start transaction;
CREATE TEMPORARY TABLE IF NOT EXISTS TEMP_JOIN(firstname varchar(500) not null,lastname varchar(500) not null) ;
insert into TEMP_JOIN(SELECT
value
FROM
wp_13_rg_lead
LEFT JOIN wp_13_rg_lead_detail on wp_13_rg_lead.id = wp_13_rg_lead_detail.lead_id
WHERE
wp_13_rg_lead.form_id = 9
AND CAST(wp_13_rg_lead_detail.field_number AS DECIMAL) = CAST(1.3 AS DECIMAL) ,
SELECT
value as lastname
FROM
wp_13_rg_lead
LEFT JOIN wp_13_rg_lead_detail on wp_13_rg_lead.id = wp_13_rg_lead_detail.lead_id
WHERE
wp_13_rg_lead.form_id = 9
AND CAST(wp_13_rg_lead_detail.field_number AS DECIMAL) = CAST(1.6 AS DECIMAL)
);
SELECT * FROM TEMP_JOIN;
commit;
END
$$
DELIMITER;
This should not give syntax error. In workbench, click on create stored procedure and copy the following between BEGIN END. Logic will work or not, that is a separate issue.
CREATE TEMPORARY TABLE IF NOT EXISTS TEMP_JOIN(firstname VARCHAR(500) NOT NULL,lastname VARCHAR(500) NOT NULL) ;
INSERT INTO TEMP_JOIN VALUES((SELECT
`value`
FROM
wp_13_rg_lead
LEFT JOIN wp_13_rg_lead_detail ON wp_13_rg_lead.id = wp_13_rg_lead_detail.lead_id
WHERE
wp_13_rg_lead.form_id = 9
AND CAST(wp_13_rg_lead_detail.field_number AS DECIMAL) = CAST(1.3 AS DECIMAL)) , (
SELECT
`value` AS lastname
FROM
wp_13_rg_lead
LEFT JOIN wp_13_rg_lead_detail ON wp_13_rg_lead.id = wp_13_rg_lead_detail.lead_id
WHERE
wp_13_rg_lead.form_id = 9
AND CAST(wp_13_rg_lead_detail.field_number AS DECIMAL) = CAST(1.6 AS DECIMAL))
);
SELECT * FROM TEMP_JOIN;

Procedure and multiple queries in MySql version 5.1.47

i have a stored procedure that delete a rows from 3 tables.
DROP procedure IF EXISTS `Epurer_Documents`;
DELIMITER $$
CREATE DEFINER=`dba_account`#`localhost` PROCEDURE `Epurer_Documents`(IN start_E INTEGER,IN pas_E INTEGER )
BEGIN
DELETE gdan from ged_document_annotation_individual as gdan
INNER JOIN ged_document_annotation as gda on gdan.id_ged_document_annotation=gda.id_ged_document_annotation
INNER JOIN ged_document as gd on gd.id_ged_document=gda.id_ged_document
INNER JOIN EpureridsgeFdolder as igf on gd.id_ged_folder = igf.id_ged_folderToEpurer
WHERE igf.id_ged_folderToEpurer in
(select id_ged_folderToEpurer from
( select id_ged_folderToEpurer from EpureridsgeFdolder limit start_E , pas_E) as x);
DELETE gdrs FROM ged_document_revision_seal as gdrs
INNER JOIN ged_document_revision as gdr on (gdrs.id_ged_document_revision_child=gdr.id_ged_document_revision
INNER JOIN EpureridsgeFdolder as igf on gd.id_ged_folder = igf.id_ged_folderToEpurer
WHERE igf.id_ged_folderToEpurer in
(select id_ged_folderToEpurer from
( select id_ged_folderToEpurer from EpureridsgeFdolder limit start_E , pas_E) as x);
DELETE FROM ged_folder WHERE id_ged_folder in ( select id_ged_folderToEpurer from ( select id_ged_folderToEpurer from EpureridsgeFdolder limit start_E , pas_E) as x);
END$$
DELIMITER ;
When i run it it show me a error :"" near , pas_E) as x); Delete gdrs From "",
but when i run it in a outher version of MySql it work juste fine.
so i wonder if the oldes version of mySQL doesn't support multiple queries in a stored procedure.
Thansk
Older MySQL version (<5.5.6) do not allow use of variables in LIMIT. Multiple queries in a stored procedure work just fine.

Create Stored Procedure in MySQL

i have the following syntax for creating a stored procedure in MySQL 5.0.10
delimiter //
CREATE PROCEDURE getTweets (var1 varchar(100))
LANGUAGE SQL
SQL SECURITY DEFINER
COMMENT 'A procedure to return 20 least scored tweets based on the temp sessionid of the user'
BEGIN
SELECT count(ts.tweetid) as "count",t.id as "tweetid", t.tweettext as "tweet"
FROM tweets t
LEFT JOIN tweetscores ts ON t.id = ts.tweetid
where t.id not in (select distinct(tweetid) from tweetscores where temp_sessionid=var1)
group by t.id, t.tweettext order by count
LIMIT 10;
END//
i keep on getting the error
#1064 - You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the right syntax to use near '//' at line 12
can anybody spot the error..
Your query works when executing it as a script on DB level.
But using PhpMyAdmin you need remove the delimiter statement.
CREATE PROCEDURE getTweets (var1 varchar(100))
LANGUAGE SQL
SQL SECURITY DEFINER
COMMENT 'A procedure to return 20 least scored tweets based on the temp sessionid of the user'
BEGIN
SELECT count(ts.tweetid) as "count",t.id as "tweetid", t.tweettext as "tweet"
FROM tweets t
LEFT JOIN tweetscores ts ON t.id = ts.tweetid
where t.id not in (select distinct(tweetid) from tweetscores where temp_sessionid=var1)
group by t.id, t.tweettext order by count
LIMIT 10;
end
PhpMyAdmin will do the rest for you.
CREATE PROCEDURE getTweets (var1 varchar(100))
LANGUAGE SQL
SQL SECURITY DEFINER
COMMENT 'A procedure to return 20 least scored tweets based on the temp sessionid of the user'
BEGIN
SELECT count(ts.tweetid) as "count",t.id as "tweetid", t.tweettext as "tweet"
FROM tweets t
LEFT JOIN tweetscores ts ON t.id = ts.tweetid
where t.id not in (select distinct(tweetid) from tweetscores where temp_sessionid=var1)
group by t.id, t.tweettext order by count
LIMIT 10;
end

issue with create table inside if/else block

I'm trying to create a table in a MySQL stored procedure depending upon the values of some of the passed parameters. My code is as follows:
DELIMITER //
CREATE PROCEDURE testifelse(IN CurrentGPA varchar(40), IN CurrentGPAValue varchar(40))
BEGIN
IF CurrentGPAValue IS NULL THEN CREATE TABLE tableCurrentGPA(SELECT DISTINCT u.user_id AS uid FROM users u);
ELSE CREATE TABLE tableCurrentGPA(SELECT DISTINCT udata.user_id AS uid FROM userdata udata WHERE udata.userDataTypeName = CurrentGPA AND udata.userDataText = CurrentGPAValue);
END
I'm getting an error stating: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 5.
I'm not sure what I've done wrong here. The syntax looks fine to me and the queries seem well-formulated.
You're missing the END IF statement.
CREATE PROCEDURE testifelse(IN CurrentGPA varchar(40), IN CurrentGPAValue varchar(40))
BEGIN
IF CurrentGPAValue IS NULL THEN CREATE TABLE tableCurrentGPA(SELECT DISTINCT u.user_id AS uid FROM users u);
ELSE CREATE TABLE tableCurrentGPA(SELECT DISTINCT udata.user_id AS uid FROM userdata udata WHERE udata.userDataTypeName = CurrentGPA AND udata.userDataText = CurrentGPAValue);
END IF;
END

stored procedure in mysql in ubuntu

I have created stored procedure in MySQL in Window-7. it run successfully on windows. But when I switch to Ubuntu it gives error in the stored procedure. On windows I am using SQLyog for creating stored procedure. On Ubuntu, I run the SQL script and call that stored procedure but it gives error. Below is my stored procedure.
DELIMITER $$
USE `adserver`$$
DROP PROCEDURE IF EXISTS `getDaypartTimeDetail`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `getDaypartTimeDetail`
(currentDate DATE,noOfdays INT,cityId BIGINT)
BEGIN
DECLARE i INT;
DECLARE dateCnt INT;
SET dateCnt = 0;
DROP TEMPORARY TABLE IF EXISTS OnlyDate;
DROP TEMPORARY TABLE IF EXISTS AdvScheduleData;
CREATE TEMPORARY TABLE OnlyDate(dday DATE);
CREATE TEMPORARY TABLE AdvScheduleData(dday BIGINT,daypartId INT,totalFile BIGINT,totalDur BIGINT);
/* Generate Dates */
WHILE(dateCnt < noOfdays) DO
SET i = 1;
INSERT INTO OnlyDate(dday) VALUES (DATE_ADD(currentDate, INTERVAL dateCnt DAY));
SET dateCnt = dateCnt + 1;
END WHILE;
/* Insert all dayparts for all dates */
INSERT INTO AdvScheduleData (dday, daypartID) SELECT (UNIX_TIMESTAMP(dday)*1000), id FROM OnlyDate, daypart;
/* Update total files and duration */
UPDATE AdvScheduleData SET
TotalFile = (SELECT COUNT(advt_id)
FROM adv_schedule AdvSch
INNER JOIN advertisement Adv ON Adv.id = AdvSch.advt_id
WHERE AdvScheduleData.dday BETWEEN AdvSch.start_date AND AdvSch.end_date
AND AdvSch.status = 2
AND AdvSch.active = 1
AND AdvSch.id IN (SELECT schedule_id FROM schedule_daypart
WHERE daypart_id = AdvScheduleData.daypartId )
AND AdvSch.id IN (SELECT schedule_id FROM schedule_cities WHERE city_id = cityId)
AND Adv.is_active = 1
AND Adv.is_deleted = 0
AND Adv.status = 2
AND Adv.expiry_date >= AdvScheduleData.dday),
totalDur = (SELECT SUM(Adv.duration)
FROM adv_schedule AdvSch
INNER JOIN advertisement Adv ON Adv.id = ADVSCH.advt_id
WHERE AdvScheduleData.dday BETWEEN AdvSch.start_date AND AdvSch.end_date
AND AdvSch.status = 2
AND AdvSch.active = 1
AND AdvSch.id IN (SELECT schedule_id FROM schedule_daypart
WHERE daypart_id = AdvScheduleData.daypartId )
AND AdvSch.id IN (SELECT schedule_id FROM schedule_cities WHERE city_id = cityId)
AND Adv.is_active = 1
AND Adv.is_deleted = 0
AND Adv.status = 2
AND Adv.expiry_date >= AdvScheduleData.dday);
SELECT * FROM AdvScheduleData;
END$$
DELIMITER ;
The output I get in Ubuntu is
mysql> call getDaypartTimeDetail('2012-08-13',5,30534); ERROR 1054
(42S22): Unknown column 'ADVSCH.advt_id' in 'on clause'
Table name and aliases are case sensitive in Ubuntu.
Hence, this reference is giving an error:
totalDur = (SELECT SUM(Adv.duration)
FROM adv_schedule AdvSch
INNER JOIN advertisement Adv ON Adv.id = ADVSCH.advt_id
Change it to AdvSch.advt_id