Prepare statement with chpasswd - mysql

--- 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 ;

Related

pass parameter is field after where mysql , procedure

I do my filter with column of table pass through parameter(field after where in line 7) procedure but pass parameter column of table ,mysql doesn't know
use testphantrang;
DELIMITER $$
CREATE PROCEDURE `filter`(in field varchar(40), in giatri varchar(40),in index_of_page int)
begin
declare xx int ;
set xx = index_of_page*15;
SELECT * from test where field like CONCAT('%', giatri,'%') limit xx,15;
END; $$
DELIMITER ;
call `filter`('email','b',0);
Below is my table:
DELIMITER $$
CREATE PROCEDURE `filter`(in field varchar(40), in giatri varchar(40),in index_of_page int)
begin
declare xx int ;
set xx = index_of_page*15;
set #query = CONCAT('SELECT * from test where field rlike ''', giatri,''' limit ',xx,',15;');
PREPARE dynamic_statement FROM #query;
EXECUTE dynamic_statement;
DEALLOCATE PREPARE dynamic_statement;
END; $$
DELIMITER ;
call `filter`('email','b',0);

Stored procedure to loop through a list of tables deleting data in batches of 100000 in MySQL

I have 2 schemas with exactly the same tables schema_oltp and schema_olap. I want to delete from selected tables in schema_oltp only the data which already exists in schema_olap. The tables I want to delete from are listed in a table called archived_to_delete. For each of the tables listed in archived_to_delete, I need to delete the data in batches of 100 000. This is what I have so far. It only deletes 100 000 in each table and stops. I want it to continue until there is no data in schema_oltp which also exists in schema_olap:
DELIMITER $$
CREATE DEFINER=`root`#`%` PROCEDURE `schema_oltp`.`schemaDeletes`(IN from_schema VARCHAR(100), IN to_schema VARCHAR(100))
BEGIN
DECLARE v_tname varchar(100);
DECLARE v_pkey varchar(455);
DECLARE v_upfield varchar(455);
DECLARE done INT DEFAULT 0;
DECLARE v_limit INT DEFAULT 100000;
DECLARE v_deleted INT;
DECLARE table_cursor CURSOR FOR SELECT a.table_name, a.column_name
FROM INFORMATION_SCHEMA.COLUMNS a
INNER JOIN archived_to_delete b
ON a.table_name = b.table_name
WHERE TABLE_SCHEMA = 'schema_oltp'
AND COLUMN_KEY='PRI'
ORDER BY b.archived_to_delete_id;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN table_cursor;
REPEAT
FETCH table_cursor INTO v_tname, v_pkey;
IF NOT done THEN
SET v_deleted=0;
SET #sql = CONCAT('DELETE FROM ',from_schema,'.', v_tname, ' WHERE ',v_pkey,' IN (SELECT ', v_pkey, ' FROM ',to_schema,'.', v_tname, ') limit ',v_limit,';');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET v_deleted=v_deleted+1;
IF v_deleted = v_limit THEN
commit;
SET v_deleted=0;
END IF;
SELECT CONCAT('Deleting...',v_tname);
END IF;
UNTIL done END REPEAT;
CLOSE table_cursor;
END$$
DELIMITER ;

Stored procedure doesn't create a new table

The procedure is suppose to take a existing table (oldtable) and create a new table (new_table_n). It's running, but not creating a new table. Any help will be appreciated.
DELIMITER $$
CREATE PROCEDURE P_DYN_TABLE(IN OLDTABLE VARCHAR(30), IN NEW_TABLE_N VARCHAR(30))
LANGUAGE SQL
BEGIN
DECLARE STATEMENT_1 VARCHAR(1000);
DECLARE CONTINUE HANDLER FOR SQLSTATE '42704'
SET STATEMENT_1 = '';
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
SET STATEMENT_1 = CONCAT('CREATE TABLE ',NEW_TABLE_N,' AS SELECT * FROM ',OLDTABLE,' WHERE 1=2');
PREPARE S1 FROM #STATEMENT_1;
EXECUTE S1;
SET STATEMENT_1 = CONCAT('INSERT INTO ',NEW_TABLE_N,' SELECT * FROM ',OLDTABLE);
PREPARE S2 FROM #STATEMENT_1;
EXECUTE S2;
END; $$
DELIMITER $$
CREATE PROCEDURE P_DYN_TABLE(IN OLDTABLE VARCHAR(30), IN NEW_TABLE_N VARCHAR(30))
BEGIN
DECLARE STATMENT VARCHAR(1000);
SET #STATMENT="";
SET #STATMENT = CONCAT('CREATE TABLE ',NEW_TABLE_N,' AS SELECT * FROM ',OLDTABLE,' WHERE 1=2');
PREPARE S1 FROM #STATMENT;
EXECUTE S1;
DEALLOCATE PREPARE S1;
SET #STATMENT = CONCAT('INSERT INTO ',NEW_TABLE_N,' SELECT * FROM ',OLDTABLE);
PREPARE S1 FROM #STATMENT;
EXECUTE S1;
DEALLOCATE PREPARE S1;
END; $$

MYSql Stored procedure Debug Query

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');

Stored procedure error when being called

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 ;