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
这是存储过程我试图创建如何使用存储过程获得两个表的总数?
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 ;
为了更好的理解我试图用简单的SQL查询这样
SELECT (SELECT COUNT(*) FROM candidates WHERE active=1)+
(SELECT COUNT(*) FROM voters_enrollment WHERE active=1) AS Total
我得到的结果作为
Total
10
像这样我需要创建过程并调用它来获得相同的结果通过使用简单的sql查询。任何人都可以帮助我解决这个问题。
'CREATE PROCEDURE bcp.getTotalCount()'缺少括号。 –