How to get total count of two tables using stored procedure? - mysql

I need to create a stored procedure for getting the count of two table by using where clause condition, but when i try to create procedure it shows error
Query : CREATE PROCEDURE bcp.getTotalCount BEGIN -- Create two integer values DECLARE #tableOneCount int, #tableTwoCount int -- Get ...
Error Code : 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 'BEGIN
-- Create two integer values
DECLARE #tableOneCount int, #tableTwoCount' at line 2
This is the stored procedure i tried to create
DELIMITER $$
DROP PROCEDURE IF EXISTS bcp.getTotalCount $$
CREATE PROCEDURE bcp.getTotalCount
BEGIN
-- Create two integer values
DECLARE #tableOneCount INT, #tableTwoCount INT
-- Get the number of rows from the first table
SELECT #tableOneCount = (SELECT COUNT(*) FROM candidates WHERE active=1)
SELECT #tableTwoCount = (SELECT COUNT(*) FROM voters_enrollment WHERE active=1)
-- Return the sum of the two table sizes
SELECT TotalCount = #tableOneCount + #tableTwoCount
END $$
DELIMITER ;
For better understanding i tried with simple sql query like this
SELECT (SELECT COUNT(*) FROM candidates WHERE active=1)+
(SELECT COUNT(*) FROM voters_enrollment WHERE active=1) AS Total
I get the result as
Total
10
Like this way i need to create the procedure and call it to get the same result by using simple sql query. can anyone help me to solve this please.

You have to put AS after the create statement.
DELIMITER $$
DROP PROCEDURE IF EXISTS bcp.getTotalCount $$
CREATE PROCEDURE bcp.getTotalCount
AS
BEGIN
-- Create two integer values
DECLARE #tableOneCount INT, #tableTwoCount INT
-- Get the number of rows from the first table
SELECT #tableOneCount = (SELECT COUNT(*) FROM candidates WHERE active=1)
SELECT #tableTwoCount = (SELECT COUNT(*) FROM voters_enrollment WHERE active=1)
-- Return the sum of the two table sizes
SELECT TotalCount = #tableOneCount + #tableTwoCount
END $$
DELIMITER ;

You can try this, mate:
DELIMITER $$
DROP PROCEDURE IF EXISTS bcp_getTotalCount $$
CREATE PROCEDURE bcp_getTotalCount()
BEGIN
-- clear/initialize session variables
SET
#tableOneCount = 0,
#tableTwoCount = 0;
START TRANSACTION;
-- get record count from the source tables
SELECT COUNT(*) INTO #tableOneCount FROM candidates WHERE active = 1;
SELECT COUNT(*) INTO #tableOneCount FROM voters_enrollment WHERE active = 1;
-- return the sum of the two table sizes
SELECT TotalCount = #tableOneCount + #tableTwoCount;
COMMIT;
END $$
DELIMITER ;
MySQL Transaction for Atomicity

Related

How use while loop in procedure for delete a few rows . SQL (Mysql)

I want to delete a quantity of rows in procedure .
I tried do it like this ,but I did something wrong.
BEGIN
DECLARE counter INT DEFAULT 0;
SELECT COUNT(*) INTO quantity FROM (SELECT A.idImages FROM images A WHERE A.Posts_idposts = id ) a LIMIT 1;
START TRANSACTION;
WHILE counter< quantity DO
SELECT A.idImages INTO imagesid FROM imagesA WHERE A.Posts_idposts = id LIMIT counter,1;
DELETE FROM images WHERE IdImages = imagesid;
END WHILE;
//other single deletes
COMMIT;
END
the procedure gets the "id" from the attribute
CREATE DEFINER=`root`#`localhost` PROCEDURE `usun_zgloszenie`(id INT)
the procedure lasts indefinitely

phpMyAdmin - SELECT statement's result in stored procedure not showing

I Have created a simple procedure:
CREATE PROCEDURE `simpleProcedure` ( IN `parameter` INT) NOT DETERMINISTIC READS SQL DATA SQL SECURITY DEFINER
BEGIN
SELECT *
FROM table1;
END
Running this procedure using CALL simpleProcedure(1) in phpMyAdmin does not show the result of the query... How do I make phpMyAdmin show the result of the query inside the procedure?
You need an OUT parameter for it to return results, see http://dev.mysql.com/doc/refman/5.0/en/create-procedure.html.
Example from the sakila database:
DELIMITER $$
CREATE DEFINER=`root`#`127.0.0.1` PROCEDURE `film_in_stock`(IN p_film_id INT, IN p_store_id INT, OUT p_film_count INT)
READS SQL DATA
BEGIN
SELECT inventory_id
FROM inventory
WHERE film_id = p_film_id
AND store_id = p_store_id
AND inventory_in_stock(inventory_id);
SELECT FOUND_ROWS() INTO p_film_count;
END$$

Mysql procedure sum

I cannot remember how to do a basic SUM in a SQL proceedure, can anybody remind me quickly please?
drop procedure if exists pCaCalculateCcyFluc;
delimiter //
create procedure pCaCalculateCcyFluc(
IN pPrice DECIMAL(10,6),
IN pPricePrev DECIMAL(10,6),
OUT pCcyFluc DECIMAL(10,6)
)
MODIFIES SQL DATA
COMMENT 'calculate fluctuation'
begin
pCcyFluc = (pPricePrev - pPrice)/pPrice;
--
end;
//
delimiter ;
To use the SUM Function:
SELECT SUM(TableName.ColumnName)
FROM TableName
JOIN TableName ON TableName.ColumnName = TableName.ColumnName
GROUP BY TableName.ColumnName;
Check out these examples.

MySql Stored procedure shows as error in syntax

I am new to creating procedures in mysql, i know how to create them in MSSQL, but i am not sure what is wrong with this, it says Syntax Error Near END
CREATE PROCEDURE GetNameByID(IN CustID INT)
BEGIN
SELECT * FROM Customers WHERE CustomerID = CustID
END
The query in your procedure needs a semi colon after it:
CREATE PROCEDURE GetNameByID(IN CustID INT)
BEGIN
SELECT * FROM Customers WHERE CustomerID = CustID;
END
You may also need to set the delimiter to something. The MySQL documentation does this:
DELIMITER //
CREATE PROCEDURE GetNameByID(IN CustID INT)
BEGIN
SELECT * FROM Customers WHERE CustomerID = CustID;
END//
(but obviously not with your query)
You are missing the ; at the end of select statement

Mysql stored procedure multiple selects

I am running a stored procedure. The issue seems to be that it will go into the if statement. Also for some reason or another regardless of how many selects I use it will only return the first. I've copied this from another stored procedure that works like a charm, but this one just won't go. Any ideas?
DROP PROCEDURE IF EXISTS genSelPriceTier;
DELIMITER $$
CREATE PROCEDURE genSelPriceTier(tier_id INT, default_id INT)
BEGIN
DECLARE rowCount INT DEFAULT 0;
SELECT * FROM price_tier WHERE price_tier_id = tier_id;
SET rowCount = FOUND_ROWS();
IF rowCount < 1 THEN
SELECT * FROM price_tier WHERE price_tier_id = default_id;
END IF;
END$$
DELIMITER ;
There is a bug reported related to the usage of FOUND_ROWS(). So, I recommend using Count(*) for the number of rows returned. Something like the following should work.
DROP PROCEDURE IF EXISTS genSelPriceTier;
DELIMITER $$
CREATE PROCEDURE genSelPriceTier(tier_id INT, default_id INT)
BEGIN
DECLARE rowCount INT DEFAULT 0;
SELECT COUNT(*) INTO rowCount FROM price_tier WHERE price_tier_id = tier_id
IF rowCount < 1 THEN
SELECT * FROM price_tier WHERE price_tier_id = default_id;
END IF;
END$$
DELIMITER ;