MySql Stored procedure shows as error in syntax - mysql

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

Related

How can I get sql to return #cardType variable data into my table

Directions
Write a proceedure called GetAllCustomerLevelsthat returns the customerNumber, customerName, and cardtype for all customers.
Hint: Use CASE in your select statement. See: Case in Select Statement at stackoverflow or MySQL CASE operator from w3resource. This could also be done using a nested IF. See: Inline IF and CASE statements in MySQL from timmurphy.
Below is an example proceedure that returns the customerNumber and customerName for all customers:
DROP PROCEDURE IF EXISTS GetAllCustomerLevels;
DELIMITER $$
CREATE PROCEDURE GetAllCustomerLevels()
BEGIN
select customerNumber, customerName
from customers;
END$$
DELIMITER ;
Save the sql that created the GetAllCustomerLevels procedure as GetAllCustomerLevels.sql
One of my Attempts
DROP PROCEDURE IF EXISTS GetAllCustomerLevels2;
DELIMITER $$
CREATE PROCEDURE GetAllCustomerLevels2()
BEGIN
DECLARE creditlimit double;
call GetCustomerLevel(#customerNumber,#cardType);
select customerNumber, customerName, #cardType
from customers
end;
SET #cardType =
(CASE
WHEN (creditlimit > 50000) THEN 'PLATINUM'
WHEN (creditlimit <= 50000 AND creditlimit >= 10000) THEN 'GOLD'
WHEN (creditlimit < 10000) THEN 'SILVER'
ELSE 'No card'
END
);
END$$
DELIMITER ;
My result returns customerName and customerNumber but #cardType stays empty.

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

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

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.

Insert,upadate in single Procedure in mysql

im creating stored procedure in my sql but getting error,error shows as below,help me Error Descrption:You hve an error in your sql syntax: ckeck the manual that corresponds to your MYSQL
server version fro the right syntax to use near 'END' at line 13
DELIMITER $$
DROP PROCEDURE IF EXISTS myhealthcamp.area $$
CREATE PROCEDURE myhealthcamp.area
(
IN id INT,
IN ar VARCHAR(45)
)
BEGIN
if exists (select area_id from area where area_id = id)
Then
update area set areaname = ar where area_id=id;
else
insert into area(area_id, areaname) values(id,ar);
END IF
END $$
DELIMITER ;
You're missing a ; in this line:
END IF
It should be:
END IF;
IF is a statement and all statements must end in semi-colons. This one's no different. See the MySQL documentation for IF.
DROP PROCEDURE IF EXISTS myhealthcamp.area $$
CREATE PROCEDURE myhealthcamp.area
(
IN id INT,
IN ar VARCHAR(45)
)
BEGIN
if exists (select area_id from area where area_id = id)
Then
update area set areaname = ar where area_id=id;
else
insert into area(area_id, areaname) values(id,ar);
END IF;
semicolon missing
Thanks
Sanil