查询结果为空,输出变量正确

时间:2018-03-12 00:55:52

标签: mysql multiple-results

我正在创建一个存储过程(例程),它从表中选择多个字段,然后设置一个检查/输出变量。

根据我的数据库中的数据,检查/输出变量适当地设置为要求的数据,但我的查询不返回任何实际数据:

CREATE DEFINER=`odcorg_darrmik`@`%` PROCEDURE `sp_Get_Specific_Load`(
IN LOAD_ID INT,
OUT SUCCESS BIT)
BEGIN
set SUCCESS = 0;
SELECT 
       LOAD_ID,
       DRIVER_ID,
       BOL_NUMBER,
       MILES,
       LOAD_PAY,
       SURCHARGE,
       TARP_FEE,
       DESCRIPTION,
       WEIGHT, 
       TRUCK_NUMBER,
       TRAILER_NUMBER, 
       HAZARDOUS, 
       DATE_RECEIVED, 
       DATE_DISPATCHED,
       DATE_DUE, 
       DATE_DELIVERED,
       BROKER_COMPANY,
       BROKER_NAME,
       TIME_APPOINTMENT,
       TIME_ARRIVED,
       TIME_STARTED,
       TIME_FINISHED,
       FCFS,
       COMPLETE,
       PAID_COMPANY,
       PAID_DRIVER,
       (SELECT NAME FROM customers inner join loads_info on loads_info.LOAD_ID = @LOAD_ID and customers.CUSTOMER_ID = loads_info.SHIPPER_ID) AS SHIPPER_NAME,
       SHIPPER_ID,
       (SELECT NAME FROM customers INNER JOIN loads_info ON loads_info.LOAD_ID = @LOAD_ID AND customers.CUSTOMER_ID = loads_info.CONSIGNEE_ID) AS CONSIGNEE_NAME,
       CONSIGNEE_ID,
       (SELECT FIRST_NAME + ' ' + LAST_NAME FROM employee inner join loads_info on loads_info.LOAD_ID = @LOAD_ID and EMPLOYEE_ID = DRIVER_ID) AS DRIVER_NAME, 
       (SELECT SIGNED_BOL FROM loads_documents INNER JOIN loads_info ON loads_info.LOAD_ID = @LOAD_ID and loads_documents.LOAD_ID = @LOAD_ID) AS SIGNED_BOL
FROM loads_info WHERE LOAD_ID = @LOAD_ID;
set SUCCESS = 1;
END

我甚至从查询中删除了大部分内容:

CREATE PROCEDURE sp_Get_Specific_Load(
IN LOAD_ID INT,
OUT SUCCESS BIT)
BEGIN
set SUCCESS = 0;
SELECT * FROM loads_info;
set SUCCESS = 1;
END

上述过程将再次返回SUCCESS输出,但不会从

返回任何内容
SELECT * FROM loads_info;

如果我删除所有内容只留下

SELECT * FROM loads_info;

该过程将返回数据...如果我添加一个WHERE子句(对于上面非常简单的查询)我再次得不到数据......所有字段标题但没有数据。

我是MySQL的新手...我将这个程序转换为我在MSSQL中编写的一个按预期运行的程序,返回请求的数据和检查/输出变量。

我做错了什么?

1 个答案:

答案 0 :(得分:0)

您需要查看变量的使用。首先要注意的是WHERE子句中的@LOAD_ID不是您调用过程的LOAD_ID,如下例所示。 @LOAD_ID是一个完全独立的变量,因为它永远不会设置为任何变量,所以它的值为NULL。因此,WHERE子句正在测试LOAD_ID = NULL,它永远不会成立,因此不会返回任何行。

mysql> CREATE PROCEDURE `sp_Get_Specific_Load`(IN LOAD_ID INT, OUT SUCCESS BIT)
    -> BEGIN
    -> set SUCCESS = 0;
    -> SELECT LOAD_ID, @LOAD_ID;
    -> set SUCCESS = 1;
    -> END //

mysql> SET @SUCCESS = 0;
mysql> CALL `sp_Get_Specific_Load` (999, @SUCCESS);
+---------+----------+
| LOAD_ID | @LOAD_ID |
+---------+----------+
|     999 | NULL     |
+---------+----------+
1 row in set (0.01 sec)

mysql> SELECT @SUCCESS;
+----------+
| @SUCCESS |
+----------+
|        1 |
+----------+
1 row in set (0.00 sec)

要避免的第二件事是使用与列名匹配的参数名称。如果使用相同的名称,MySQL将在列名称上选择输入参数。如下一个示例所示,它将选择参数,有效地针对自身测试参数值(对于每一行都为true)。因此,您需要重命名输入参数以区分这两者。

mysql> CREATE TABLE LOADS_INFO (
    ->   LOAD_ID INT
    -> );  
mysql> INSERT INTO LOADS_INFO (LOAD_ID) VALUES (1),(2),(3),(4),(5),(999);

mysql> CREATE PROCEDURE `sp_Get_Specific_Load`(IN LOAD_ID INT, OUT SUCCESS BIT)
    -> BEGIN
    -> set SUCCESS = 0;
    -> SELECT * FROM LOADS_INFO WHERE LOAD_ID = LOAD_ID;
    -> set SUCCESS = 1;
    -> END //

-- Matches ALL records with input of 999
mysql> CALL `sp_Get_Specific_Load` (999, @SUCCESS);
+---------+
| LOAD_ID |
+---------+
|       1 |
|       2 |
|       3 |
|       4 |
|       5 |
|     999 |
+---------+
6 rows in set (0.00 sec)