Dynamic query in procedure, how do I get OUT value? - mysql

I want to call a dynamic procedure with a particular time_stamp and pro_id.
In the first step I want to find out if that particular pro_id exists in the table. Is there anything wrong in the Concat statement? I do not get the desired OUT value
DELIMITER ;;
CREATE PROCEDURE 'ADDCONSENSUS'(IN time_stamp int(10), IN pro_id INT(10), OUT cnt INT(11))
BEGIN
SET #sql1 = CONCAT('SELECT COUNT(pid) INTO #cnt FROM ',time_stamp,' WHERE pid = ,pro_id);
PREPARE stmt from #sql1;
EXECUTE stmt;
END

You can try the following:
DELIMITER $$
DROP PROCEDURE IF EXISTS `ADDCONSENSUS`$$
CREATE PROCEDURE `ADDCONSENSUS`(
IN `time_stamp` INT,
IN `pro_id` INT,
OUT `cnt` INT)
BEGIN
SET #sql1 := CONCAT('
SELECT COUNT(`pid`) INTO #`cnt`
FROM `', CAST(`time_stamp` AS CHAR), '`
WHERE `pid` = ', CAST(`pro_id` AS CHAR));
PREPARE stmt FROM #sql1;
EXECUTE stmt;
SET `cnt` := #`cnt`;
DEALLOCATE PREPARE stmt;
END$$
DELIMITER ;
CALL `ADDCONSENSUS`(1395395302, 3, #`_count`);
SQL Fiddle demo

Related

stored procedure stops working after first update

I have a task related to MYSQL where I am required to gather a particular set of values from a horizontal table and insert it into another table. The values in the table are like Index_0, AngleValue_0, Index_1, AngleValue_1,.....Index_11, AngleValue_11. I have to identify a set by the Index value, grab all the values of that set (for example if the Index is identified as Index_0, then grab Index_0 and AngleValue_0), and insert it into another table.
I have created a stored procedure for this, but it works for the first time and then stops updating.
Here is how my stored procedure looks.
DELIMITER //
drop procedure if exists updaterearseattorquereapir//
create procedure updaterearseattorquereapir(in ind int, in kanbn varchar(50), in rp_bdg int, in tl_bdg int, in qc_bdg int)
BEGIN
set #cnt = 0;
WHILE #cnt <= 11 DO
set #i = 0;
DROP TEMPORARY TABLE IF EXISTS indexcheck_Temp;
set #sql_check = CONCAT('CREATE TEMPORARY TABLE indexcheck_Temp
Select ',concat('Index_',#cnt),' as a From rearseattorque where ',concat('Index_',#cnt),'=', ind,' and Kanban = ',"#kanbn");
prepare stmt from #sql_check;
Execute stmt;
deallocate prepare stmt;
select #i := a from indexcheck_Temp;
if ind = #i
then
DROP TEMPORARY TABLE IF EXISTS rearseattorquerepair_Temp;
set #sql1 = CONCAT('
CREATE TEMPORARY TABLE rearseattorquerepair_Temp
SELECT Kanban, ',rp_bdg,', ',tl_bdg,', ',qc_bdg,', DTRowUpdate, ',concat('AngleValue_',#cnt),', ',concat('AngleStatus_',#cnt),', ',
concat('Index_',#cnt),', ',concat('Name_', #cnt),', ',concat('Operator_',#cnt),', ',concat('PSETNumber_',#cnt),', ',
concat('TighteningID_',#cnt),', ',concat('TighteningStatus_',#cnt),', ',concat('TimeStamp_',#cnt),', ',
concat('TorqueValue_',#cnt),', ',concat('TorqueStatus_',#cnt),
' FROM rearseattorque where ',concat('Index_',#cnt),'=', ind,' and Kanban = ',"#kanbn");
prepare stmt from #sql1;
EXECUTE stmt;
deallocate prepare stmt;
Insert into rearseattorquerepair(Kanban
,RepairOperatorBadge
, TeamLeaderBadge
, QCBadge
, DTRowUpdate
, Bolt_AngleValue
, Bolt_AngleStatus
, Bolt_Index
, Bolt_Name
, Bolt_Operator
, Bolt_PSETNumber
, Bolt_TighteningID
, Bolt_TighteningStatus
, Bolt_TimeStamp
, Bolt_TorqueValue
, Bolt_TorqueStatus
)
Select * from rearseattorquerepair_Temp;
else
select 'data not found';
END if;
SET #cnt = #cnt + 1;
END WHILE;
END//
DELIMITER ;

How to write a procedure to create table by inputting a variable date?

Goal
I want to create table as follow, the format of table name is tbl+_+date , but I want to write a procedure to create when I input a given date.
drop table if exists tbl_20200802 ;
create table tbl_20200802 (index (USERID) )
select * from a;
Try
DELIMITER //
DROP PROCEDURE IF EXISTS pro_ljj_push_pre_id;
CREATE PROCEDURE pro_ljj_push_pre_id(
IN test_date date
)
BEGIN
set #table_name=CONCAT('tbl_', test_date);
SET #t1 =CONCAT(
'drop table if exists '
,#table_name
,';'
,'\n'
,'create table ', tab_name
, ' (index (USERID))'
,'\n'
,'SELECT * FROM a');
PREPARE stmt3 FROM #t1;
EXECUTE stmt3;
DEALLOCATE PREPARE stmt3;
--
END //
DELIMITER ;
CALL pro_ljj_push_pre_id('20200802');
1054 - Unknown column 'tab_name' in 'field list'
You have some syntax error in your code,
However, the following is your desired. Change the structure of table as you want.
DELIMITER $$
DROP PROCEDURE IF EXISTS pro_ljj_push_pre_id$$
CREATE PROCEDURE pro_ljj_push_pre_id(IN test_date VARCHAR(50))
BEGIN
set #table_name=CONCAT('tbl_', test_date);
SET #t1 =CONCAT('drop table if exists ',#table_name);
SELECT #t1;
PREPARE stmt3 FROM #t1;
EXECUTE stmt3;
DEALLOCATE PREPARE stmt3;
SET #t2 =CONCAT('create table ', #table_name, ' (id INT PRIMARY KEY, name VARCHAR(30));');
SELECT #t2;
PREPARE stmt4 FROM #t2;
EXECUTE stmt4;
DEALLOCATE PREPARE stmt4;
--
END $$
DELIMITER ;
CALL pro_ljj_push_pre_id('20200802');

Why is my procedure producing null result?

I have simply created procedure which take 2 inputs and give 2 outputs. When I run query separately it gives me proper output, But If I will try to call procedure It produce me null result
while calling Query:
SELECT field1, field2
INTO #var1, #var2
FROM MyTable ID=? and Name=?
and then :
select #var1, #var2;
But If I combine the same code in procedure it will give null for both fields.
Procedure:
DELIMITER $$
CREATE DEFINER=`user`#`localhost` PROCEDURE `My_Proc`(
IN `ID` INT,
IN `Name` VARCHAR(20)
-- OUT `result` INT,
-- OUT `result1` INT
)
BEGIN
set #var1 = 0;
set #var2 = 0;
set #query := CONCAT("SELECT
field1, field2
INTO #var1, #var2
FROM MyTable ID=? and Name=?");
PREPARE stmt FROM #query;
Execute stmt USING #Id,#Name;
DEALLOCATE PREPARE `stmt`;
select #var1,#var2;
END
I tried to call select #var1,#var2; outside procedure but it also give null fields.
I am working in Mysql Workbench 6.0
you have some mistakes in your procedure thats have been removed below..........have a look
DELIMITER $$
CREATE DEFINER=`user`#`localhost` PROCEDURE `My_Proc`(
IN `ID` INT,
IN `Name` VARCHAR(20)
-- OUT `result` INT,
-- OUT `result1` INT
)
BEGIN
set #var1 = 0;
set #var2 = 0;
set #query := CONCAT("SELECT
field1,field2
INTO #var1, #var2
FROM MyTable where ID=? and Name=?");
PREPARE stmt FROM #query;
Execute stmt USING #Id,#Name;
DEALLOCATE PREPARE stmt ;
select #var1,#var2;
END

How can i create a procedure for select statement in 'IN Clause' for phpmyadmin

I want to create a procedure for a select query in which the where clause will have a IN Clause. I had created one procedure like below butu its not working-
CREATE DEFINER = `root`#`localhost` PROCEDURE `agentin` ( IN `code` VARCHAR( 100 ) ) NOT DETERMINISTIC NO SQL SQL SECURITY DEFINER
BEGIN
SELECT * FROM AgentInformation WHERE AgentCode IN (code);
END
After putting the In clause values like --> IN ('CG001','CG002')
I am getting null values and the query made by phpmyadmin was
SET #p0 = '''CG001'',''CG002''';
CALL agentin (
#p0
);
Please help regarding it , Thanks
Here it is an example -
DELIMITER $$
CREATE PROCEDURE procedure1(IN id_param VARCHAR(255))
BEGIN
SET #sql = CONCAT('SELECT * FROM table WHERE id IN (', id_param, ')');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END$$
DELIMITER ;
SET #id = '1,2,3,4,5';
CALL procedure1(#id);

MySQL stored procedure not working on Mac OS X

I have the following stored procedure (Taken from a comment on http://dev.mysql.com/doc/refman/5.0/en/create-index.html, used to check if an index exists on a table):
DELIMITER $$
DROP PROCEDURE IF EXISTS `create_index_if_not_exists`$$
CREATE DEFINER=`user`#`%` PROCEDURE `create_index_if_not_exists`(table_name_vc varchar(50), index_name_vc varchar(50), field_list_vc varchar(200))
SQL SECURITY INVOKER
BEGIN
set #Index_cnt = (
select count(1) cnt
FROM INFORMATION_SCHEMA.STATISTICS
WHERE table_name = table_name_vc
and index_name = index_name_vc
);
IF ifnull(#Index_cnt,0) = 0 THEN set #index_sql = concat('Alter table ',table_name_vc,' ADD INDEX ',index_name_vc,'(',field_list_vc,');');
PREPARE stmt FROM #index_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END IF;
END$$
DELIMITER ;
The strange thing is that it works perfectly on CentOS 5.4 with MySQL 5.5.25, but does not work on Mac OS X 10.8.1 with MySQL 5.5.24. On Mac, the #Index_cnt is always 0 if I add
select #Index_cnt
in the stored procedure. If I do the SELECT COUNT(1) cnt ... statement on it's own, then 1 or 0 is returned as it should be.
Any ideas?
I have re-written the stored procedure and this now works on all platforms:
DELIMITER $$
DROP PROCEDURE IF EXISTS myschema.create_index_if_not_exists $$
CREATE PROCEDURE myschema.create_index_if_not_exists(in p_tableName VARCHAR(128), in p_indexName VARCHAR(128), in p_columnName VARCHAR(128) )
BEGIN
PREPARE stmt FROM 'SELECT #indexCount := COUNT(1) from information_schema.statistics WHERE `table_name` = ? AND `index_name` = ?';
SET #table_name = p_tableName;
SET #index_name = p_indexName;
EXECUTE stmt USING #table_name, #index_name;
DEALLOCATE PREPARE stmt;
-- select #indexCount;
IF( #indexCount = 0 ) THEN
SELECT 'Creating index';
SET #createIndexStmt = CONCAT('CREATE INDEX ', p_indexName, ' ON ', p_tableName, ' ( ', p_columnName ,')');
PREPARE stmt FROM #createIndexStmt;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END IF;
END $$
DELIMITER ;
Use it as follows:
call myschema.create_index_if_not_exists('MyTable','end_time_index','end_time');
This was tested on MAC OS X 10.8.2 with MySQL 5.5.24 and on Windows 7 with MySQL 5.5.21