I am getting error when I execute script mention below
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 '$$
DROP PROCEDURE IF EXISTS `GetTopParentGivenDepth`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `GetTopParentGivenDepth`(GivenDepth INT)
BEGIN
DECLARE X,Y,x1,x2 INT;
SET X = 0;
SET Y = 1;
SET #SQ = 'SELECT DISTINCT A0.id FROM categories A0';
WHILE Y < GivenDepth DO
SET #SQ = CONCAT(#SQ,' INNER JOIN categories A',Y,' ON A',X,'.id = A',Y,'.parent_id');
SET X = Y;
SET Y = X + 1;
END WHILE;
SET #SQ = CONCAT(#SQ,' WHERE A0.parent_id = 0');
SELECT #SQ;
PREPARE stmt FROM #SQ;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END$$
missing DELIMITER before creating procedure
DELIMITER $$
DROP PROCEDURE IF EXISTS `GetTopParentGivenDepth`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `GetTopParentGivenDepth`(GivenDepth INT)
BEGIN
DECLARE X,Y,x1,x2 INT;
SET X = 0;
SET Y = 1;
SET #SQ = 'SELECT DISTINCT A0.id FROM categories A0';
WHILE Y < GivenDepth DO
SET #SQ = CONCAT(#SQ,' INNER JOIN categories A',Y,' ON A',X,'.id = A',Y,'.parent_id');
SET X = Y;
SET Y = X + 1;
END WHILE;
SET #SQ = CONCAT(#SQ,' WHERE A0.parent_id = 0');
SELECT #SQ;
PREPARE stmt FROM #SQ;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END$$
DELIMITER ;
Related
Is there a way to check if a table exists on database MySQL using Stored Procedure?
This is the SP, I expected when the table exists the variable value titem_id return 1 and when table not exists the variable value titem_id return 0.
Instead in all conditions (the table exists or not) the value is always zero...
Help me to do it.
I don't need number of records but check if exist table in database.
If table exists the return of value titem_id it's 1 else it's 0.
CREATE DEFINER=`root`#`%` PROCEDURE `SP`(tmonth int(2), tddlarea CHAR(100), OUT titem_id INT(11))
BEGIN
DECLARE 2tmonth int(2);
DECLARE 2tddlarea char(100);
DECLARE 2tyear int(4);
DECLARE 2titem_id int(11);
SET 2tmonth = tmonth;
SET 2tddlarea = tddlarea;
SET 2tyear = YEAR(CURDATE());
SET 2titem_id = 0;
SET #t = CONCAT('SELECT EXISTS(SELECT * FROM INFORMATION_SCHEMA.tables AS titem_id
WHERE table_schema = ''db''
AND table_name = ''t_contents_', 2tddlarea, '_', 2tmonth, '_', 2tyear, ''');');
PREPARE stmt FROM #t;
EXECUTE stmt;
DEALLOCATE PREPARE `stmt`;
SELECT #t;
IF #t = 1 THEN
SET titem_id := 1;
SET #s = -- EXECUTE SQL QUERY
PREPARE stmt FROM #s;
EXECUTE stmt;
DEALLOCATE PREPARE `stmt`;
ELSE
SET titem_id := 0;
END IF;
END
UPDATE
CALL sys.table_exists('db', CONCAT('t_contents_', 2tddlarea, '_', 2tmonth, '_', 2tyear, ''), #exists);
SELECT #exists;
IF #exists > '' THEN
SET titem_id = 1;
SET #s = -- -- EXECUTE SQL QUERY
PREPARE stmt FROM #s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
ELSE
SET titem_id = 0;
END IF;
--- I have written this below procedure but getting syntax error in
prepare statement ----
DELIMITER $$
drop procedure if exists test;
CREATE PROCEDURE test()
BEGIN
DECLARE min_counter INT ;
DECLARE max_counter INT ;
DECLARE usr varchar(20);
DECLARE pwd varchar(20);
DECLARE var1 varchar(100);
select id into min_counter from ftp order by id limit 0,1;
select id into max_counter from ftp order by id desc limit 0,1;
WHILE min_counter <= max_counter do
select usr1 into usr from ftp where id = min_counter;
select new_passwd into pwd from ftp where id = min_counter;
set #stm = "\! echo "$usr":$pwd" | chpasswd";
prepare stmt from #stm;
execute stmt;
deallocate prepare stmt;
SET min_counter = min_counter + 1 ;
END WHILE;
END$$
DELIMITER ;
I wanted to build around 900 views(1 view for 1 table and based on 2 conditions) and i have all the table names in a table in mysql environment. I have created a stored proc . For test purpose i used limit 1.
I get below error when i call the sp.
Actually I re-wrote a mssql sp, did i miss anything here. please help
CALL ca_uim.sp_viewcreation_ontime() Error Code: 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 'NULL' at line 1 0.000 sec
USE `ca_uim`;
DROP procedure IF EXISTS `sp_viewcreation_ontime`;
DELIMITER $$
USE `ca_uim`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `sp_viewcreation_ontime`()
BEGIN
DECLARE qos varchar(255);
DECLARE pos int;
DECLARE r_table varchar(255);
DECLARE view varchar(255);
DECLARE cview varchar(2048);
DECLARE done int default 0;
DECLARE qos_cursor CURSOR FOR SELECT DISTINCT qos,r_table FROM S_QOS_DATA ORDER BY 2 limit 1;
DECLARE continue handler for not found set done = 1;
OPEN qos_cursor;
-- Perform the first fetch.
FETCH qos_cursor INTO qos, r_table;
-- Check ##FETCH_STATUS to see if there are any more rows to fetch.
WHILE not done
DO
-- Check QOS name for '-' character & replace with '_' if exist
SET pos = LOCATE('-',qos, 1);
IF pos != 0
THEN
SET qos = INSERT(qos, pos, 1, '_');
END IF;
-- Check QOS name for '/' character & replace with '_' if exist
SET pos = LOCATE('/',qos, 1);
IF pos != 0
THEN
SET qos = INSERT(qos, pos, 1, '_');
END IF;
-- Check QOS name for '(' character & replace with '_' if exist
SET pos = LOCATE('(',qos, 1);
IF pos != 0
THEN
SET qos = INSERT(qos, pos, 1, '_');
END IF;
-- Check QOS name for ')' character & replace with '_' if exist
SET pos = LOCATE(')',qos, 1);
IF pos != 0
THEN
SET qos = INSERT(qos, pos, 1, '_');
END IF;
-- Create view
SET view = CONCAT('V_',qos);
SET cview = CONCAT('CREATE VIEW ',view,' AS ',
'SELECT Q.source,Q.target,Q.origin,Q.robot,Q.probe,D.sampletime,D.samplevalue,D.samplestdev,D.samplerate,D.tz_offset ',
'FROM S_QOS_DATA Q JOIN ',r_table,' D ON Q.table_id=D.table_id');
BEGIN
-- Suppress Error message for Views that don't exist
DECLARE CONTINUE HANDLER FOR 1051
set #stmt_str = CONCAT('DROP VIEW ',view);
prepare stmt from #stmt_str;
execute stmt;
deallocate prepare stmt;
END;
BEGIN
-- Create the View, Catch tables that don't have samplestdev & samplerate fields
DECLARE CONTINUE HANDLER FOR 1054
set #stmt_str = cview;
prepare stmt from #stmt_str;
execute stmt;
deallocate prepare stmt;
/* PRINT CONCAT('Created View: ' , view) */
END;
BEGIN
DECLARE CONTINUE HANDLER FOR 1054
SET cview = CONCAT('CREATE VIEW ',view,' AS ',
'SELECT Q.source,Q.target,Q.origin,Q.robot,Q.probe,D.sampletime,D.samplevalue,D.tz_offset ',
'FROM S_QOS_DATA Q JOIN ',r_table,' D ON Q.table_id=D.table_id');
set #stmt_str = cview;
prepare stmt from #stmt_str;
execute stmt;
deallocate prepare stmt;
/* PRINT CONCAT('Created View: ' , view) */
END;
-- PRINT 'qos: ' + #qos + ' ' + #r_table+' '+#view
-- PRINT #cview
-- This is executed as long as the previous fetch succeeds.
FETCH qos_cursor INTO qos, r_table;
END WHILE;
CLOSE qos_cursor;
END$$
DELIMITER ;
Thanks
When I try to run the mysql stored procedure code on phpmyadmin then error is generating .
The ERROR is : Syntax error near concat(strWhere, ' AND (qxt.topic_id IN (',
Thanks!
DELIMITER $$
DROP PROCEDURE IF EXISTS PercentageCalc$$
CREATE PROCEDURE PercentageCalc(topicArray VARCHAR(255),topicIndId int(11),userId VARCHAR(255),userArraySize int(11))
BEGIN
DECLARE x INT;
SET x = 0;
SELECT topicArray;
SELECT topicIndId;
SET #sql = CONCAT('SELECT COUNT(*) AS total FROM answer as ans
INNER JOIN question AS qst ON ans.question_id = qst.question_id
INNER JOIN question_topic AS qxt ON qxt.question_id = qst.question_id
WHERE (ans.answer_correct_flag = \'yes\')
AND (qxt.topic_id IN (\',topicArray,\'))',strwhere);
IF (topicIndId NOT NULL) THEN
SET strWhere = concat(strWhere, ' AND (qxt.topic_id IN (',topicIndId,'))');
END IF;
IF (userId NOT NULL) THEN
REPEAT
SET strWhere = concat(strWhere, ' AND (ans.user_id IN (',userId[X],'))');
SET x = x + 1;
UNTIL x > userArraySize
END REPEAT
END IF;
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END$$
DELIMITER ;
CALL PercentageCalc('1,2,3','1,2','1','1');
When I call my procedure, it return an 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 'NULL' at line 1
How to fix it, do I have an error in my procedure code?
Here is my stored procedure :
DELIMITER $$
DROP PROCEDURE IF EXISTS `gamedb`.`ALIAS#SEARCH`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `ALIAS#SEARCH`(
in section_id varchar(255),
in category_id varchar(255),
in content_id varchar(255)
)
BEGIN
declare q varchar(4000);
set #q = 'SELECT * FROM tbl_alias WHERE ALIAS_ACTIVE_STATUS=1';
IF section_id IS NOT NULL THEN
set #q = concat(q,' AND ALIAS_SECTION_ID = ',section_id);
END IF;
IF category_id IS NOT NULL THEN
set #q = concat(q,' AND ALIAS_CATEGORY_ID = ',category_id);
END IF;
IF content_id IS NOT NULL THEN
set #q = concat(q,' AND ALIAS_CONTENT_ID = ',content_id);
END IF;
set #q= concat(q,' ORDER BY ALIAS_ID DESC');
prepare stmt from #q;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END$$
DELIMITER ;
There several problems
Since you can PREPARE only from user variable, you don't need a local variable q
You're missing # for #q variable when using CONCAT()
set #q = concat(q,' AND ALIAS_SECTION_ID = ',section_id);
^
Since your IN parameters defined as VARCHAR you better quote their values when you build the query
That being said your SP might look like this
DELIMITER $$
CREATE PROCEDURE `ALIAS#SEARCH`(
in section_id varchar(255),
in category_id varchar(255),
in content_id varchar(255)
)
BEGIN
SET #q = 'SELECT * FROM tbl_alias WHERE ALIAS_ACTIVE_STATUS = 1';
IF section_id IS NOT NULL THEN
SET #q = CONCAT(#q, ' AND ALIAS_SECTION_ID = ''', section_id, '''');
END IF;
IF category_id IS NOT NULL THEN
SET #q = CONCAT(#q, ' AND ALIAS_CATEGORY_ID = ''', category_id, '''');
END IF;
IF content_id IS NOT NULL THEN
SET #q = CONCAT(#q, ' AND ALIAS_CONTENT_ID = ''', content_id, '''');
END IF;
SET #q = CONCAT(#q, ' ORDER BY ALIAS_ID DESC');
PREPARE stmt FROM #q;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END$$
DELIMITER ;