Procedure and multiple queries in MySql version 5.1.47 - mysql

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.

Related

mysql syntax error on creating stored procedure

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.

IN clause not working in WHERE condition in MYSQL Trigger

I am creating a trigger that have a MYSQL query having IN clause look like below.
drop trigger onPublish;
delimiter $$
create trigger onPublish AFTER insert on publish_drawing_details
for each row
begin
Declare userIds VARCHAR(255);
set userIds = (select GROUP_CONCAT(refUser) from manpower where refProject=new.refProjectId and refRoleType in (select role from hr_user));
insert into notification_container(loginUserId,mailSubject,projectId,userIds,attachmentIds,ccMails,mailText,metaNotificationEventId)
values (new.submittedBy,new.mailSubject,new.refProjectId,userIds,new.attachmentIds,new.ccEmails,new.mailText,1);
end $$
It works fine if I write this like set userIds = (select GROUP_CONCAT(refUser) from manpower where refProject=new.refProjectId and refRoleType in (1,2,3,4,5));
I try it like set userIds = (select GROUP_CONCAT(refUser) from manpower where refProject=new.refProjectId and refRoleType in (select GROUP_CONCAT(role) from hr_user));
but don't work at all.
You don't need GROUP_CONCAT in inner query, try the following:
SET userIds = (
SELECT GROUP_CONCAT(refUser)
FROM manpower
WHERE refProject=new.refProjectId
AND refRoleType IN (
SELECT role
FROM hr_user
)
);

CALL for a procedure keeps loading, takes high CPU, and fails

I am trying to create a procedure that will take a view table , composed from thousands of records, and will filter it by removing several records according to various conditions.
When I try to use the CALL to run it, it keeps on loading, and according to WHM Panel, the process (phpMyAdmin/import.php) takes about 90% of CPU power.
Eventually it fails perhaps after 10 min due to a timeout:
Static analysis:
1 errors were found during analysis.
Missing expression. (near "ON" at position 25) SQL query: Edit Edit
SET FOREIGN_KEY_CHECKS = ON;
MySQL said: Documentation
2006 - MySQL server has gone away
I'm using the following code, and the new view table is using 3 DB tables:
vw_r2_items_tr, is using a tr DB core table, and a vw_items - a view table which uses items and visits core tables
DELIMITER $$
CREATE VIEW `vw_r2_items_tr` AS
SELECT
IF((`i`.`id` = `t`.`item_ord`),1,0) AS `has_tr`,
`i`.`calculated_score` AS `calculated_score`,
`i`.`visited_at` AS `visited_at`,
`i`.`ip_address` AS `ip_address`,
`i`.`user_id` AS `user_id`,
`i`.`visit_id` AS `visit_id`,
`i`.`product_id` AS `product_id`,
`i`.`name` AS `name`,
`i`.`id` AS `id`,
`i`.`item_no` AS `item_no`,
FROM (`vw_items` `i`
LEFT JOIN `trs` `t`
ON (((`t`.`visit_id` = `i`.`visit_id`)
AND (`t`.`item_ord` = `i`.`id`))))$$
DELIMITER ;
DELIMITER $$
CREATE PROCEDURE `vw_unique_trs2`()
BEGIN
SET #vat=0;
SET #ht=0;
SET #tdiff=0;
-- EXPLAIN
SELECT
COUNT(*) AS ct,
tb1.* FROM (SELECT it.*, #tdiff:=UNIX_TIMESTAMP(CONVERT_TZ(it.visited_at,'+00:00',##global.time_zone)) AS tdiffs,
IF(#vat > 0 AND ((#vat-#tdiff) < 28800 AND (#vat-#tdiff) > -28800),#vat DIV 28800, #tdiff DIV 28800) AS diff,
#ht:=(IF(#vat > 0 AND ((#vat-#tdiff) < 28800 AND (#vat-#tdiff) > -28800),#ht+has_tr,has_tr)) hts,
#vat:=(IF(#vat > 0 AND ((#vat-#tdiff) < 28800 AND (#vat-#tdiff) > -28800),#vat,#tdiff)) curr_vat
FROM (SELECT * FROM (SELECT * FROM `vw_r2_items_tr` ORDER BY has_tr DESC, item_no, visited_at) AS tb
GROUP BY `tb`.`visit_id`,`tb`.`product_id`) AS it
ORDER BY has_tr DESC, it.visited_at DESC
) AS tb1
GROUP BY user_id, calculated_score, diff, hts;
END$$
DELIMITER ;
i think you cant use only one "ON" with two join, try to do 2 join query like that
`SELECT *
FROM Table1
LEFT JOIN Table2 ON Table1.key = Table2.key` and
LEFT JOIN Table3 ON Table1.key = Table2.key`

Syntax error when creating procedure in mysql

I have this query.
SELECT distinct c.reportingDate, c.clientId
FROM clients AS c
WHERE NOT EXISTS
( SELECT 1
FROM accounts_europe AS e
WHERE e.reportingDate = c.reportingDate
AND e.clientId = c.clientId
union
SELECT 1 -- line A. unexpected select error
FROM accounts_usa AS u
WHERE u.reportingDate = c.reportingDate
AND u.clientId = c.clientId
) ;
when executing it at mysql workbench it displays a syntax error which says "unexpected select" at line A.
However, since the query is right, when I execute it, it indeed executes and returns the correct results.
When I try to create a procedure that contains the exact same thing, it cannot be compiled. The source code is this:
CREATE PROCEDURE `proc_sample`()
BEGIN
SELECT distinct c.reportingDate, c.clientId
FROM clients AS c
WHERE NOT EXISTS
( SELECT 1
FROM accounts_europe AS e
WHERE e.reportingDate = c.reportingDate
AND e.clientId = c.clientId
union
SELECT 1 -- line A. unexpected select error
FROM accounts_usa AS u
WHERE u.reportingDate = c.reportingDate
AND u.clientId = c.clientId
) ;
END
The message displayed is "The object's DDL statement contains syntax errors."
Is it something I am doing wrong about this or is it a bug of the mysql workbench?
This error comes up because of a MySQL grammar bug* that has been fixed meanwhile. Try the latest Workbench version instead (currently WB 8.0.11 RC, see "Development Releases" on the download page). That should work for you.
(*) MySQL Workbench uses ANTLR4 for parsing SQL code. That requires a MySQL grammar to generate the parser.
Your query is correct syntactically. It's maybe a workbench's bug. Coming to Stored Procedure, you should be able to create it with that query. Try creating SP using below query. In my case, I am able to create SP using this. Copy this query open a new query editor in the workbench and paste it there, select all and press Ctrl + Shift + Enter
USE `YOUR_DB`;
DROP procedure IF EXISTS `proc_sample`;
DELIMITER $$
USE `YOUR_DB`$$
CREATE DEFINER=`admin`#`%` PROCEDURE `proc_sample`()
BEGIN
SELECT distinct c.reportingDate, c.clientId
FROM clients AS c
WHERE NOT EXISTS
( SELECT 1
FROM accounts_europe AS e
WHERE e.reportingDate = c.reportingDate
AND e.clientId = c.clientId
union
SELECT 1
FROM accounts_usa AS u
WHERE u.reportingDate = c.reportingDate
AND u.clientId = c.clientId
) ;
END$$
DELIMITER ;

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