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
Related
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 ;
I have the following stored procedure . I'm trying to insert the users from the table usuaris, whose admin variable is equal to 1, into the table that the stored procedure creates with the name( nombre varchar(50)) that is passed as a parameter.
When the procedure is called, it duplicates the user 'mary' with id 4. I've tried a couple of ways to implement the logic condition in order to avoid the duplication, but still, I'm missing something and I can't get the desired result. In the code below, the logic condition before the insertion is the last thing I've tried. Any ideas?
Thanks.
CREATE DEFINER=`root`#`localhost` PROCEDURE `createNewtable`(nombre varchar(50))
BEGIN
/*variable declaration*/
declare centinela int ;
declare id1 int ;
declare nom1 varchar(50);
declare admin1 enum('0','1') ;
declare cadena varchar(100); /*string to concatenate table creation and insertion*/
/*cursor declaration*/
declare cursor1 cursor for select * from users.usuaris where admin = '1' ;
declare continue handler for not found set #centinela = 1 ;
/*create the table with the name that's passed as parameter*/
set #cadena=concat("create table ",nombre,
"(
id2 int not null primary key,
nom2 varchar(50),
admin2 enum ('0','1')
)" );
prepare stmt from #cadena ;
execute stmt ;
deallocate prepare stmt;
/* loop that fetches the data from the table usuaris and
inserts them into the newly created table. */
set #centinela = 0 ;
open cursor1 ;
bucle: loop
fetch cursor1 into id1,nom1,admin1 ;
if ( centinela = 1 ) then
leave bucle ;
end if ;
/*logic condition to avoid entry duplication */
if not exists (select * from users.usuaris where admin='1' and id=#id1) then
set #cadena=concat("insert into ",nombre," values( ",id1,",'",nom1,"','",admin1,"')");
end if;
select #cadena;
prepare stmt from #cadena;
execute stmt ;
deallocate prepare stmt;
end loop bucle;
close cursor1;
END
Here is the single-table database of users :
create database if not exists `users` ;
use `users` ;
create table usuaris(
id int not null auto_increment primary key ,
nom varchar(50),
admin enum ('0','1')
);
insert into usuaris(id,nom,admin)
values
(1,'jose','1'),
(2,'maria','0'),
(3,'frank','1'),
(4,'mary','1'),
(5,'godfrey','0') ;
Also it has to duplicate jose. The reason of duplication - if the IF statement isn't TRUE then you don't set the new #cadena variable BUT anyway execute PREVIOUS #cadena statement. You should move execution into the IF statement also:
if not exists (select * from users.usuaris where admin='1' and id=#id1) then
set #cadena=concat("insert into ",nombre," values( ",id1,",'",nom1,"','",admin1,"')");
select #cadena;
prepare stmt from #cadena;
execute stmt ;
deallocate prepare stmt;
end if;
Also in SQL you should always try to avoid loops if it possible and use SQL statements instead.
You can replace your loop with one SQL statement:
INSERET INTO NEW_TABLE_NAME_HERE
SELECT id1,nom1,admin1
FROM users.usuaris where admin<>'1'
Further more you can use SELECT INTO statement syntax to automatically create new table without CREATE TABLE statement:
SELECT id1 as id2,
nom1 as nom2,
admin1 as admin2
INTO NEW_TABLE_NAME_HERE
FROM users.usuaris where admin<>'1'
Change ur below code to my new code and try-
Existing Code
if not exists (select * from users.usuaris where admin='1' and id=#id1) then
set #cadena=concat("insert into ",nombre," values( ",id1,",'",nom1,"','",admin1,"')");
end if;
select #cadena;
prepare stmt from #cadena;
execute stmt ;
deallocate prepare stmt;
New Code-
SET #cnt=SELECT count(*) FROM users.usuaris WHERE admin='1' AND id=#id1
IF #cnt>0 THEN
SET #cadena=CONCAT("insert into ",nombre," values( ",id1,",'",nom1,"','",admin1,"')");
prepare stmt from #cadena;
execute stmt ;
deallocate prepare stmt;
end if;
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
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);
I generate a dynamic query in My sql Stored procedure. I wanna get the result of this query into a out parameter. How to do this ?
CREATE PROCEDURE 'searchInvoice'
(
OUT numOfRecords INT
)
BEGIN
DECLARE query1 TEXT;
DECLARE query2 TEXT;
SET query1 = 'SELECT COUNT(*) bla bla bla.....';
// Query1 to select the count of matching tuples..
SET query2 = 'SELECT * from bla bla bla....';
// Query2 to select original records...
// later part of this both queries generate dynamically according to some IN parameters..
// now I wanna assign the output of the query1 into numOfRecords
// and I wanna execute the query2 as well.. like this
SET #Sql = query2;
PREPARE STMT FROM #Sql;
EXECUTE STMT;
DEALLOCATE PREPARE STMT;
// output of the query2 can be read in PHP
END
How to get the output of the query1 into OUT parameter(numOfRecords ) ??
Have a look at this example -
CREATE TABLE table1(
column1 VARCHAR(255) DEFAULT NULL,
column2 VARCHAR(255) DEFAULT NULL,
column3 VARCHAR(255) DEFAULT NULL
);
INSERT INTO table1 VALUES
('1', 'value1', 'value2'),
('2', 'value3', 'value4');
DELIMITER $$
CREATE PROCEDURE procedure1(IN Param1 VARCHAR(255), OUT Param2 VARCHAR(255), OUT Param3 VARCHAR(255))
BEGIN
SET #c2 = '';
SET #c3 = '';
SET #query = 'SELECT column2, column3 INTO #c2, #c3 FROM table1 WHERE column1 = ?';
PREPARE stmt FROM #query;
SET #c1 = Param1;
EXECUTE stmt USING #c1;
DEALLOCATE PREPARE stmt;
SET Param2 = #c2;
SET Param3 = #c3;
END$$
DELIMITER ;
-- Call procedure and use variables
SET #Param1 = 2;
SET #Param2 = '';
SET #Param3 = '';
CALL procedure1(#Param1, #Param2, #Param3);
SELECT #Param2, #Param3;
+---------+---------+
| #Param2 | #Param3 |
+---------+---------+
| value3 | value4 |
+---------+---------+
select count(*) into #numOfRecords from ....
You have do declare the variable within stored procedure
I hope I've understood your question.