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$$
Related
I have example code query store procedure in sql server from internet like this
CREATE PROCEDURE `usp_UpdateStudent` (#tblStudent XML)
BEGIN
SET NOCOUNT ON;
INSERT INTO Student(StudentId,StudentName)
SELECT Student.value('(StudentId)[1]', 'nvarchar(100)') as StudentId,
Student.value('(StudentName)[1]', 'nvarchar(100)') as StudentName
FROM
#tblStudent.nodes('/ArrayOfStudent/Student')AS TEMPTABLE(Student)
END
i am just wondering if is there any way to create this sp in mysql, because when i search it is dominant with sql server. i try to create it in mysql but got error in paramater XML. Here is my store procedure in mysql with param XML
DELIMITER $$
CREATE PROCEDURE get_test(IN param XML)
BEGIN
SELECT *
FROM master_course
END$$
DELIMITER ;
I have the following table created in mysql database.
create table stud_info(Student_ID int,Name varchar(10),Class varchar(10),Marks int)
I have also created a stored procedure to retrieve the name given the id like below:
DELIMITER //
create procedure selectEmp2(IN num1 INT,OUT name varchar(20))
BEGIN
select Name INTO name from myDB.stud_info where Student_ID=num1;
END//
When I am calling the stored procedure , I am getting null value. Please let me know where I am going wrong.
I think your stored procedure should work, but I would advise giving names to parameters that are likely to be unique. I also prefer explicit variable assignment, because select into can mean different things. Does this work?
DELIMITER //
create procedure selectEmp2(IN in_num1 INT, OUT out_name varchar(20))
BEGIN
select si.Name into out_name
from myDB.stud_info si
where si.Student_ID = in_num1;
END;//
Try this:
DELIMITER //
create procedure selectEmp2(IN _num1 INT,OUT _name varchar(20))
BEGIN
select Name INTO _name
from myDB.stud_info
where Student_ID=_num1;
END//
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
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
I've written a stored procedure. It's working fine except taking the table name as input parameter.
Let see my proc in MySQL:
DELIMITER $$
USE `db_test`$$
DROP PROCEDURE IF EXISTS test_proc$$
CREATE DEFINER=`root`#`localhost`
PROCEDURE `test_proc`(IN serviceName VARCHAR(10),IN newsInfoTable VARCHAR(100))
BEGIN
SELECT COUNT(*) FROM newsInfoTable WHERE newsServiceName=serviceName;
END$$
DELIMITER ;
Stored procedure calling parameters:
USE db_test;
CALL test_proc('abc','tbl_test_news');
Here the service name parameter is working fine. But if I include the newsInfoTable variable as table input parameter then a error shows.
Table 'db_test.newsinfotable' doesn't exist
Why does this happen only for table parameter? How can I retrieve from this error or
How I pass a table name into a stored procedure as a parameter?
An SP cannot be optimized with a dynamic table name, so many DBs, MySQL included, don't allow table names to be specified dynamically.
One way around this is to use Dynamic SQL.
CREATE DEFINER=`root`#`localhost` PROCEDURE `test_proc`(IN serviceName VARCHAR(10),IN newsInfoTable VARCHAR(100))
BEGIN
SET #sql = CONCAT('SELECT COUNT(*) FROM ',newsInfoTable,' WHERE newsServiceName=?;');
PREPARE s1 from #sql;
SET #paramA = serviceName;
EXECUTE s1 USING #paramA;
END$$
You can use EXECUTE IMMEDIATE for a "less is more" solution (for me, less code = good)
CREATE PROCEDURE test_proc(IN serviceName VARCHAR(10), IN newsInfoTable VARCHAR(100))
BEGIN
EXECUTE IMMEDIATE CONCAT('SELECT COUNT(*) FROM ',newsInfoTable,' WHERE newsServiceName=''', serviceName, '''');
END
that part of a query cannot be dynamic.
you may consider implementing as a string that is executed dynamically at runtime
Although may not be what you want, alternatively, can consider to use conditionally if and prepare the statement.
DELIMITER $$
CREATE PROCEDURE select_count(IN table_name VARCHAR(20))
BEGIN
IF table_name = 'xxx' THEN
SELECT * FROM xxx;
ELSEIF table_name = 'yyy' THEN
...
ENDIF
END$$