【问题标题】:How to get total count of two tables using stored procedure?如何使用存储过程获取两个表的总数?
【发布时间】:2015-06-26 07:23:09
【问题描述】:
  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() 缺少括号。

标签: mysql stored-procedures


【解决方案1】:

你必须把 AS 放在 create 语句之后。

    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 ;

【讨论】:

    【解决方案2】:

    你可以试试这个,伙计:

    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 原子性事务

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-31
      • 2016-08-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多