How to use select statement in procedure in SQL - mysql

Why would my SQL throw an error like LEAVE with no matching label: tableList
DELIMITER $$
DROP PROCEDURE IF EXISTS CountSignatures$$
CREATE PROCEDURE CountSignatures()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE signatureCount INT;
DECLARE tableName CHAR(100);
DECLARE tableList CURSOR FOR Select table_name from information_schema.tables where table_schema="LogData" and table_name like "%FAULT_20150320%";
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN tableList;
tableListLoop: LOOP
SET done = FALSE ;
FETCH tableList INTO tableName;
IF done THEN
LEAVE tableListLoop;
END IF;
***select signatureCount := signatureCount + count(distinct signature) from tableList;*** Line giving syntax error
END LOOP;
CLOSE tableList;
END$$
DELIMITER;

leave lable:
This statement is used to exit the flow control construct that has the
given label. If the label is for the outermost stored program block,
LEAVE exits the program.
In your code tableList is a cursor, you should leaved the tableListLoop not tableList, so try:
LEAVE tableListLoop;

I'm not sure why it would generate that particular error, but this line is not correct:
signatureCount = signatureCount + Select count (distinct signature) from tableList;
If you want to update the variable, try:
set signatureCount = (signatureCount +
(select count(distinct signature)
from tableList));
However, you don't initialize the variable, so this will just produce NULL anyway.

Related

what is wrong in this mysql cursor, and how to correct it?

CREATE PROCEDURE curLike()
BEGIN
DECLARE likeRec likecounttable;
DECLARE c_likeCount CURSOR FOR SELECT l.likeCount, l.qId FROM likecounttable l;
OPEN c_likeCount;
start_loop:LOOP
FETCH c_likeCount IN likeRec
UPDATE qentry SET qentry.likeCount = likeRec.likeCount WHERE qentry.qId=likeRec.qId;
END LOOP;
CLOSE c_likeCount;
END;
I am trying to use a cursor here which fetches records from likecounttable, I saw this type of syntax in few sites so I used it but it is not working
You are missing a semi-colon after your first declaration, furthermore, likecounttable is a table, not a data type.
Since you're trying to store two column values into your declared variables, your first line should look more like this
DECLARE likeRec_Count, likeRec_qId INT;
After reading your code, if you aren't adding to your cursor, you can simplify by using the following sql instead, which does the same thing as your cursor.
UPDATE qentry JOIN likecounttable l ON l.qId=qentry.qId
SET qentry.likeCount = l.likeCount
;
EDIT: If you wanted a complete update to your cursor, the following should do the same thing.
DELIMITER $$
CREATE PROCEDURE curLike()
BEGIN
DECLARE c_likeRec_isdone BOOLEAN DEFAULT FALSE;
DECLARE likeRec_Count, likeRec_qId INT;
DECLARE c_likeCount CURSOR FOR SELECT l.likeCount, l.qId FROM likecounttable l;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET c_likeRec_isdone = TRUE;
OPEN c_likeCount;
loop_likeRecList: LOOP
FETCH c_likeCount INTO likeRec_Count, likeRec_qId;
IF c_likeRec_isdone THEN
SET c_likeRec_isdone = FALSE;
LEAVE loop_likeRecList;
END IF;
UPDATE qentry SET qentry.likeCount = likeRec_Count WHERE qentry.qId=likeRec_qId;
END LOOP loop_likeRecList;
CLOSE c_likeCount;
END;
$$
CREATE PROCEDURE curLike()
BEGIN
DECLARE likeRec_Count, likeRec_qId INT;
DECLARE c_likeCount CURSOR FOR SELECT l.likeCount, l.qId FROM likecounttable l;
OPEN c_likeCount;
start_loop:LOOP
FETCH c_likeCount INTO likeRec_Count,likeRec_qId
UPDATE qentry SET qentry.likeCount = likeRec_Count WHERE qentry.qId=likeRec_qId ;
END LOOP;
CLOSE c_likeCount;
END;

Trying to add an additional value to an existing value in a column for all the rows that are retrieved using a stored procedure for

I have spent quite a long time to figure out.. so my update statement is affecting 0 rows although I know for a fact that it should affect at least affect more than a few rows as I have tried as a standalone. In place of update statement I tried select statement and it is working so does that mean that update statement is not supposed to work in stored procedure.. I kinda doubt it.. so I would like to get a second opinion.
my stored procedure code here:
DELIMITER $$
CREATE PROCEDURE updateKeywordsInRIConsole(in retailerId int )
BEGIN
declare key_words varchar(200) default null;
declare grpid bigint(20);
declare finished bool default false;
declare cur1 cursor for
select Keywords, GRPID
from RIConsole
where RetailerID = retailerId
and DateCreated > date(now()) - interval 1 year
and INSTR(Keywords, "offer_page") = false;
declare continue handler for not found set finished = 1;
declare exit handler for sqlexception
begin
show errors;
end;
declare exit handler for sqlwarning
begin
show warnings;
end;
open cur1;
start_loop: loop
fetch cur1 into key_words, grpid;
if finished = 1 then
leave start_loop;
end if;
update RIConsole set Keywords = concat(key_words, " ",
"offer_page") where GRPID = cast(grpid as signed); <-- this code not working...I called it with cast function to make sure.. and i also tried without it.
end loop start_loop;
close cur1;
END $$
DELIMITER ;
DROP PROCEDURE updateKeywordsInRIConsole;
Yes, you can do an UPDATE in a stored procedure.
If you are happy with your SELECT, you could do the while thing in a single statement. e.g.
CREATE PROCEDURE updateKeywordsInRIConsole(IN retailerId INT)
BEGIN
UPDATE RIConsole
SET Keywords = CONCAT(Keywords, " ", "offer_page")
WHERE where RetailerID = retailerId
AND DateCreated > DATE(NOW()) - INTERVAL 1 YEAR
AND INSTR(Keywords, "offer_page") = false;
END
;

Mysql syntax error on stored procedure

I run this code as a sql script from command line, and I get "you have an error in your SQL syntax" almost in all lines! Any ideas what is wrong here?
CREATE PROCEDURE updatemandate()
BEGIN
DECLARE _mandate_id BIGINT(20);
DECLARE _has_succesful_payment tinyint(1);
DECLARE done INT DEFAULT 0;
DECLARE cnt INT;
DECLARE mandateCursor CURSOR FOR Select mandate_id, has_succesful_payment From mandates;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN mandateCursor;
allmandates: LOOP
Fetch mandateCursor INTO _mandate_id, _has_succesful_payment;
IF done THEN LEAVE allmandates;
END IF;
Select COUNT(*) FROM payments WHERE mandate_id=_mandate_id AND status='OK' into cnt;
IF cnt>0 THEN
SET _has_succesful_payment=1;
END IF;
END LOOP allmandates;
CLOSE mandateCursor;
END
The ; character is the default delimiter, so when MySQL sees the first ; it thinks you are done. When you create a sproc, you need to declare a different delimiter character, like so:
DELIMITER $$
CREATE PROCEDURE updatemandate()
READS SQL DATA
BEGIN
DECLARE _mandate_id BIGINT(20);
DECLARE _has_succesful_payment tinyint(1);
DECLARE done INT DEFAULT 0;
DECLARE cnt INT;
DECLARE mandateCursor CURSOR FOR Select mandate_id, has_succesful_payment From mandates;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN mandateCursor;
allmandates: LOOP
Fetch mandateCursor INTO _mandate_id, _has_succesful_payment;
IF done THEN LEAVE allmandates;
END IF;
Select COUNT(*) FROM payment WHERE mandate_id=_mandate_id AND status='OK' into cnt;
IF cnt>0 THEN
SET _has_succesful_payment=1;
END IF;
END LOOP allmandates;
CLOSE mandateCursor;
END$$
DELIMITER ;
Also a good idea to add the READS SQL DATA to the sproc definition in case you need to support binary logging.
Have you tried putting delimiter // before the CREATE statement and change the last line with END //?

Find a column in one of many tables given only one value?

I have this:
SELECT TABLE_NAME, COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE column_name = 'whatever'
but what I need is something like this:
SELECT TABLE_NAME, COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE column_data = 'whatever'
So, in words, I have a value and I have no idea where it's stored. Is there a way to literally check the entire database and return the table, column?
aaaand yes, I know, db admins wouldn't be happy!
This might get you going in the right direction.
1. Create find_column stored procedure
DROP PROCEDURE IF EXISTS `find_column`;
DELIMITER $$
CREATE PROCEDURE `find_column`(IN i_value varchar(200),
OUT o_columns varchar(2000),
OUT o_message varchar(500))
MAIN_BLOCK : BEGIN
DECLARE is_numeric boolean;
CHECK_NUMERIC : BEGIN
set is_numeric = i_value REGEXP '^(-|\\+){0,1}([0-9]+\\.[0-9]*|[0-9]*\\.[0-9]+|[0-9]+)$';
END CHECK_NUMERIC;
FIND_IT : BEGIN
DECLARE bNoMoreRows BOOLEAN DEFAULT FALSE;
DECLARE v_schema varchar(64);
DECLARE v_table varchar(64);
DECLARE v_column varchar(64);
DECLARE v_data_type varchar(64);
DECLARE v_count int;
-- all schemas, tables and columns in DB
DECLARE columns CURSOR FOR
select table_schema,table_name,column_name,data_type from information_schema.columns;
DECLARE EXIT HANDLER for SQLEXCEPTION set o_message := concat('Unexpected error while trying to find schema, table and column for value : ',i_value);
declare continue handler for not found set bNoMoreRows := true;
open columns;
set o_columns = "";
COLUMN_LOOP: loop
fetch columns
into v_schema,v_table,v_column,v_data_type;
if (
(v_data_type in ('int','bigint','tinyint','decimal','smallint','mediumint') and is_numeric=1)
or (v_data_type not in ('int','bigint','tinyint','decimal','smallint','mediumint') and is_numeric=0)
)
then
SET #dyn_sql=CONCAT('select count(*) into #c from `',v_schema,'`.`',v_table,'` where `',v_column,'`=?');
SET #c = 0;
SET #v_value = i_value;
PREPARE stmt FROM #dyn_sql;
EXECUTE stmt using #v_value;
DEALLOCATE PREPARE stmt;
SET v_count = #c;
if v_count > 0 then
if length(o_columns <= 1800) then
set o_columns = concat(o_columns,",",v_schema,".",v_table,".",v_column);
end if;
end if;
end if;
if bNoMoreRows then
set o_columns = substring(o_columns,2);
close columns;
leave COLUMN_LOOP;
end if;
END loop COLUMN_LOOP;
END FIND_IT;
END MAIN_BLOCK$$
DELIMITER ;
2. Call find_column stored procedure with your value
call `find_column`('whatever',#columns,#message);
3. Check out the results
select #columns;
The is_numeric bit is lovingly ripped-off JBB's answer from this post.
It ain't perfect (what happens if the number of columns that your value exists exceeds 10 or so? If that is the case then this will only return the first 10 or so columns (depends on how long the schema.table.column name string is).
Hopefully it'll get you going in the correct direction.
An you're right. You're DB admins will be unhappy with you. But if you don't annoy them once in a while then you're not trying hard enough IMHO ;-)
Good luck.

How to replace a string with another in whole tables in a mysql database

I want to replace a string with another in full database. Assume database name is "A" and it contains 101 tables. I want to change "subhojit" to "jeet" in all table's column if it contains "subhojit".
Is it possible in MySql?
Need to write procedure?
Please reply.
update thetable set thecol = replace(thecol, 'subhojit', 'jeet')
where thecol like '%subhojit%';
From here.
This is a bit rough and ready (exception handling for example). But hopefully you can tidy it up for your own purposes:
DELIMITER $$
CREATE PROCEDURE `replace_value_in_all_cols`(IN i_schema varchar(250),IN i_fromVal varchar(250),IN i_toVal varchar(250), OUT o_errMessage varchar(250))
BEGIN
ALL_TEXT_COLUMNS : BEGIN
DECLARE noMoreRows boolean;
DECLARE db varchar(250);
DECLARE tbl varchar(250);
DECLARE col varchar(250);
DECLARE allTextCols CURSOR FOR
select c.table_schema,c.table_name,c.column_name
from information_schema.columns c
where c.table_schema = i_schema
and lower(data_type) in ('char','text','varchar');
DECLARE EXIT HANDLER for SQLEXCEPTION set o_errMessage := "Some error message";
declare continue handler for not found set noMoreRows := true;
open allTextCols;
UPDATE_LOOP : loop
fetch allTextCols
into db,tbl,col;
if noMoreRows then
close allTextCols;
leave UPDATE_LOOP;
end if;
SET #update_stmt:=CONCAT("UPDATE ",db,".",tbl," SET ",col," = replace(",col,",'",i_fromVal,"','",i_toVal,"');");
PREPARE update_stmt FROM #update_stmt;
EXECUTE update_stmt;
DEALLOCATE PREPARE update_stmt;
end loop UPDATE_LOOP;
END ALL_TEXT_COLUMNS;
END$$
DELIMITER ;
Then you can do something like:
call `replace_value_in_all_cols`("A",'subhojit','jeet', #err);