Creating a stored procedure - MySQL - mysql

I'm trying to create a stored procedure using Mysql workbench what i want is to know how many rows affected without display all the rows when i remove my selection no rows affected. can anyone help me please?
This my procedure :
DROP PROCEDURE IF EXISTS GetAllProducts;
SELECT * FROM csii;
DELIMITER |
CREATE PROCEDURE GetAllProducts ()
BEGIN
DECLARE csii_id INT;
DECLARE csii_fk_cc INT;
DECLARE csii_s VARCHAR(255);
DECLARE csi_p DECIMAL;
DECLARE csi_c_a DATE;
DECLARE csi_o_p DECIMAL;
DECLARE csi_shi_c DECIMAL;
DECLARE csi_xc_s VARCHAR(20);
DECLARE csi_wei VARCHAR(20);
DECLARE csi_fk_c_at INT;
DECLARE csi_sta enum('a','i','d');
SELECT
id, fk_cc, s, p, c_a, o_p, shi_c, xc_s, wei, fk_c_at, sta
INTO
csii_id, csii_fk_cc, csii_s, csi_p, csi_c_a, csi_o_p, csi_shi_c,
csi_xc_s, csi_wei, csi_fk_c_at, csi_sta
FROM
csii
WHERE
csi_sta = sta AND csi_sta = 'a';
END|
DELIMITER ;
Many thanks for any help.

You should just be able to add SELECT ROW_COUNT(); to the end of your query.

Related

I am getting synxtax error for creating stored procedure [duplicate]

I need call sp before declaring a cursor. The sp fills a table and then the cursor loop in this tables rows. But workbench does not allow call sp or anything else before declare statements.
create definer=`root`#`%` procedure `usp_tesst`()
begin
declare ordid int;
declare packid int;
declare cstmid int;
declare deltimespanid int;
declare dstrcid int;
declare pcstatus tinyint(1);
declare deliverytime datetime;
declare maxdate datetime;
declare temppackcount int default(0);
call usp_createtemppackages;
-- it says there is a syntax error in here
declare crs cursor for
select * from temppackages....
How should I call this sp ?
DECLARE statements must be first in Stored Procedures.
Consider either - 1) calling usp_createtemppackages before calling usp_tesst 2) integrating usp_createtemppackages functionality into usp_tesst 3) Removing the cursor and iterating the table some other way (which may give a performance increase too as cursors are a bit slow).
You can accomplish what you need as follows:
...
declare maxdate datetime;
declare temppackcount int default(0);
/*call usp_createtemppackages;*/
-- it says there is a syntax error in here
declare crs cursor for
select * from temppackages....
call usp_createtemppackages;
...
SQL Fiddle demo

How to convert Oracle stored procedure to MySQL

I did these SQL statements in oracle
create or replace procedure teacherlist(lists out SYS_REFCURSOR )
is
begin
open lists for
select teacherid,teachername,status,email,contact,address
from teacher;
end;
but now I have to do the same stored procedure in MySQL for PHPMyAdmin, which I did it like this:
DELIMITER $$
CREATE PROCEDURE `teacherlist`(OUT `v_stuid` VARCHAR(10), OUT `v_teachername` VARCHAR(100), OUT `v_status` VARCHAR(20), OUT `v_email` VARCHAR(40), OUT `v_contact` VARCHAR(20), OUT `v_address` VARCHAR(100))
begin
DECLARE v_teacherid varchar(10);
DECLARE v_teachername varchar(100);
DECLARE v_status varchar(20);
DECLARE v_email varchar(40);
DECLARE v_contact varchar(20);
DECLARE v_address varchar(100);
DECLARE My_Cursor CURSOR FOR
select teacherid,teachername,status,email,contact,address
from teacher;
OPEN My_Cursor;
FETCH My_Cursor INTO v_teacherid,v_teachername,v_status,v_email,v_contact,v_address;
CLOSE My_Cursor;
END$$
DELIMITER ;
which I'm not sure right or wrong because when I execute the procedure it did not give me any output.
There is no direct equivalent of Oracle Database REF CURSOR in MySQL.
The easiest solution is probably something like:
DELIMITER $$
CREATE PROCEDURE teacherlist()
BEGIN
select teacherid,teachername,status,email,contact,address
from teacher;
END$$
DELIMITER ;
And using call statement to use the result set.

My stored procedure not returning all rows of my table in MYSQL

I am trying to create a script to update my database.
My problem is that my stored procedure of the first table does not return all rows just 1000 and in my database there's 848301.
This my stored procedure :
DROP PROCEDURE IF EXISTS GetAllProducts;
SELECT * FROM csii;
DELIMITER |
CREATE PROCEDURE GetAllProducts ()
BEGIN
DECLARE csii_id INT;
DECLARE csii_fk_cc INT;
DECLARE csii_s VARCHAR(255);
DECLARE csi_p DECIMAL;
DECLARE csi_c_a DATE;
DECLARE csi_o_p DECIMAL;
DECLARE csi_shi_c DECIMAL;
DECLARE csi_xc_s VARCHAR(20);
DECLARE csi_wei VARCHAR(20);
DECLARE csi_fk_c_at INT;
DECLARE csi_sta enum('a','i','d');
SELECT
id, fk_cc, s, p, c_a, o_p, shi_c, xc_s, wei, fk_c_at, sta
INTO
csii_id, csii_fk_cc, csii_s, csi_p, csi_c_a, csi_o_p, csi_shi_c,
csi_xc_s, csi_wei, csi_fk_c_at, csi_sta
FROM
csii
WHERE
csi_sta = sta AND csi_sta = 'a';
SELECT
csii_id, csii_fk_cc, csii_s, csi_p, csi_c_a, csi_o_p, csi_shi_c,
csi_xc_s, csi_wei, csi_fk_c_at, csi_sta;
END|
DELIMITER ;
Thanks for any help.

mysql out parameter for procedure not showing

I've created a procedure where I have 2 in parameters and 1 out parameter. However, I don't know why my output doesn't want to come out. I don't want to use a select statement. I would like to use the out parameter. Thanks.
create procedure quiz_totals(in q1 double unsigned, in q2 double unsigned, out p_total int)
begin
declare v_ceil_q1 int;
declare v_ceil_q2 int;
declare v_max int;
declare v_min int;
set v_ceil_q1 := ceiling(q1);
set v_ceil_q2 := ceiling(q2);
create table temp_tbl(t_scores int);
insert into temp_tbl(t_scores) values(v_ceil_q1), (v_ceil_q2));
select max(t_scores) into v_max from temp_tbl;
select min(t_scores) into v_min from temp_tbl;
set p_total := (v_ceil_q1) + (v_ceil_q2) + v_max - 2*v_min;
drop table temp_tbl;
end;
#
delimiter ;
call quiz_totals(23, 32.4, #total);
This is my output:
Query OK, 0 rows affected (0.02 sec)
No p_total ! Why?
You need a select, even if you don't want to...
SELECT #total;
If you wanna see what's inside !

MySQL Procedure with Cursor Parameter

I have the following code that is supposed to return a list of customers that are younger than the provided age parameter and a total count of found records.
My table consists of: columns ID, FIRST_FIRSTNAME, LASTNAME, DATE_OF_BIRTH
I could have easily made a few mistakes in the syntax, logic and formatting of this procedure but forgive me, I am new to this! The following is my sql procedure:
DELIMITER $$
DROP PROCEDURE IF EXISTS sample_procedure$$
CREATE PROCEDURE sample_procedure(IN actualinput INT)
BEGIN
DECLARE no_more_customers int(4);
DECLARE l_customer_count int(4);
DECLARE l_id varchar(1);
DECLARE l_first_name varchar(10);
DECLARE l_last_name varchar(10);
DECLARE l_date_of_birth varchar(20);
DECLARE customer_list varchar(250);
DECLARE dateinput DATE;
SET dateinput=DATE_SUB(now(), interval actualinput year);
DECLARE cid CURSOR FOR
SELECT ID, FIRST_FIRSTNAME, LAST_NAME, DATE_OF_BIRTH
FROM customers WHERE DATE_OF_BIRTH >= dateinput;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET no_more_customers=1;
SET no_more_customers=0;
SET l_customer_count=0;
OPEN cid;
cid_cursor: REPEAT
FETCH cid INTO l_id, l_first_name, l_last_name, l_date_of_birth; IF no_more_customers THEN
LEAVE cid_cursor;
END IF;
SET customer_list=concat(customer_list, l_id,',',l_first_name,',',l_last_name,',',l_date_of_birth);
SET l_customer_count=1+l_customer_count;
UNTIL no_more_customers
END REPEAT cid_cursor;
CLOSE cid;
SET no_more_customers=0;
SELECT customer_list AS Customers;
SELECT concat(l_customer_count);
END;
$$
I appear to have an error in the method I used to calculate the age of customers, possibly an issue with the method I used to call the cursor and my final list of customers is only returning null.
Thanks in advance...
I can suppose that variable 'customer_list' should be initialized, e.g. -
...
DECLARE customer_list VARCHAR(250) DEFAULT '';
...
because 'SET customer_list = CONCAT(customer_list, ...)' when customer_list is NULL, will return NULL too.
Take advantage of MySQL Stored Procedure Debugger. Hope this feature will help you.