简体   繁体   English

mysql如何将空参数传递给存储过程并在sql语句中对其进行测试

[英]mysql how to pass null parameter into stored procedure and test for it in sql statement

NOTE: Solution for this issue has been attached at the bottom.注意:此问题的解决方案已附在底部。 :) :)

5.6.17 - MySQL Community Server (GPL) Using MySQL Console 5.6.17 - MySQL 社区服务器 (GPL) 使用 MySQL 控制台

Trying to Test this procedure out in mysql console.尝试在 mysql 控制台中测试此过程。 It actually involves numerous fields which maybe searched against.它实际上涉及许多可能被搜索的领域。 Some values maybe defined as NULL.某些值可能定义为 NULL。

I was having troubles with the query with an ERROR #1064 which involved a NULL value at line 1.我在使用 ERROR #1064 的查询时遇到问题,该查询涉及第 1 行的 NULL 值。

Here is the query and it breaks when I added the @P1 IS NULL test.这是查询,当我添加@P1 IS NULL测试时它会中断。 I saw this somewhere but cannot for the life of my find it again...我在某个地方看到了这个,但我终生无法再次找到它......

SET @p0='46,51,52,1317,1318,1319,1320,1322,1323'; 
SET @p1='500-000'; 
CALL `searchCount2`(@p0, @p1);

DROP PROCEDURE IF EXISTS `searchCount2`//

CREATE PROCEDURE `searchCount2`(
IN _dealerIds varchar(100),
IN _dealerPhoneNumber varchar(10)
)
BEGIN
        SET @query = CONCAT('SELECT count(cID) 
        FROM tblclassifieds c
        JOIN tblphotos p 
        ON c.cmd5val=p.cClassCode 
        WHERE p.cpMain=1 
        AND c.cMarkedInappropriate=0 
        AND c.cBlacklisted=0 
        AND c.cEndDate>NOW() 
        AND (cType=29) OR (c.cType=27 OR c.cType=28) 
        AND c.cCompanyId IN (',_dealerIds,')
        AND (("',_dealerPhoneNumber,'" is null) or (c.cPhoneNum="',_dealerPhoneNumber,'"));');
    --  SELECT @query;
     PREPARE stmt FROM @query;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
END //

Retested the Above using quotes.使用引号重新测试了上述内容。

Here is the example I have which works before I added the @P1 IS NULL , but as I mentioned this query is far from complete.这是我添加@P1 IS NULL之前的示例,但正如我提到的,这个查询还远未完成。 There are numerous parameters to search against.有许多参数可供搜索。

DROP PROCEDURE IF EXISTS `searchCount3`//

CREATE PROCEDURE `searchCount3`(
IN _dealerIds varchar(100),
IN _dealerPhoneNumber varchar(10)
)
BEGIN
        SET @query = CONCAT('SELECT count(cID) 
        FROM tblclassifieds c
        JOIN tblphotos p 
        ON c.cmd5val=p.cClassCode 
        WHERE p.cpMain=1 
        AND c.cMarkedInappropriate=0 
        AND c.cBlacklisted=0 
        AND c.cEndDate>NOW() 
        AND ((cType=29) OR (cType=27 OR cType=28))
        AND c.cCompanyId IN (',_dealerIds,')
        OR c.cPhoneNum=',_dealerPhoneNumber,';');
     -- SELECT @query;
    PREPARE stmt FROM @query;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
END //

SO what is my error with this NULL error?那么我对这个 NULL 错误的错误是什么? Is there another way I should implement this?我还有其他方法可以实现吗? How can I test it in MySQL?如何在 MySQL 中测试它?

Is Set @p1=NULL;是否设置@p1=NULL; valid?有效的?

Please disregard the horrible naming convention.请忽略可怕的命名约定。

Thanks for any help.谢谢你的帮助。 I have been struggling with this for too long.我已经为此苦苦挣扎太久了。

Here is a print off of the query before it executes, SELECT @query :这是执行前查询的打印结果, SELECT @query

SELECT count(cID)
              FROM tblclassifieds c
              JOIN tblphotos p
              ON c.cmd5val=p.cClassCode
              WHERE p.cpMain=1
              AND c.cMarkedInappropriate=0
              AND c.cBlacklisted=0
              AND c.cEndDate>NOW()
              AND (cType=29)
              AND c.cCompanyId IN (46,51,52,1317,1318,1319,1320,1322,1323)
              OR (cType=27 OR cType=28)
              AND cCompanyId IN (46,51,52,1317,1318,1319,1320,1322,1323)
              AND ((579-7775 is null) or (c.cPhoneNum=579-7775)); 

I copy and paste this query into sql console and I get results.我将此查询复制并粘贴到 sql 控制台中,然后得到结果。 But Execute fails!但执行失败! Why is this so?为什么会这样? Error #1064.错误 #1064。

SOLUTION:解决方案:

I removed the parameter test for _dealerPhoneNumber IS NULL and replaced it with _dealerPhoneNumber = "".我删除了 _dealerPhoneNumber IS NULL 的参数测试,并将其替换为 _dealerPhoneNumber = ""。 This has fixed the issue.这已经解决了这个问题。

DROP PROCEDURE IF EXISTS `searchCount2`//

CREATE PROCEDURE `searchCount2`(
IN _dealerIds varchar(100),
IN _dealerPhoneNumber varchar(10)
)
BEGIN
        SET @query = CONCAT('SELECT count(cID) 
        FROM tblclassifieds c
        JOIN tblphotos p 
        ON c.cmd5val=p.cClassCode 
        WHERE p.cpMain=1 
        AND c.cMarkedInappropriate=0 
        AND c.cBlacklisted=0 
        AND c.cEndDate>NOW() 
        AND ((cType=29) AND cCompanyId IN (',_dealerIds,')) 
        OR ((cType=27 OR cType=28) AND cCompanyId IN (',_dealerIds,')) 
        AND (("',_dealerPhoneNumber,'" = "") or (c.cPhoneNum="',_dealerPhoneNumber,'"));');
    --  SELECT @query;
     PREPARE stmt FROM @query;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
END //

try to define the parameters as null on the creation.尝试在创建时将参数定义为 null。

CREATE PROCEDURE `searchCount3`(
IN _dealerIds varchar(100) = NULL,
IN _dealerPhoneNumber varchar(10) = NULL
)

I had a similar issue.我有一个类似的问题。 I was passing NULL to a MySQL stored procedure and getting this error message that I thought was obscure:我将 NULL 传递给 MySQL 存储过程,并收到了我认为晦涩难懂的错误消息:

ERROR #1064 which involved a 'NULL' value at line 1.

I thought maybe passing NULL to stored procedures was not allowed, but it is allowed.我想也许不允许将 NULL 传递给存储过程,但它是允许的。

My issue was in the stored procedure, specifically:我的问题出在存储过程中,特别是:

  • I was dynamically generating an insert statement via a call to CONCAT, AND我通过调用 CONCAT 动态生成插入语句,并且
  • i was not guarding against the presence of NULL, so i ended up with a quoted 'NULL' in my insert statement我没有防范 NULL 的存在,所以我在插入语句中得到了一个带引号的 'NULL'

By adding a condition in my string concatenation to account for NULL i was able to fix my issue通过在我的字符串连接中添加一个条件来解释 NULL 我能够解决我的问题

BEFORE

...
"more_info_link = '", more_info_link, "', ",
...

AFTER

IF(more_info_link IS NULL,
  "more_info_link = NULL, ",
  CONCAT("more_info_link = '", more_info_link, "', ")
),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用mysql将参数传递给存储过程 - how to pass parameter to stored procedure using mysql 如何在MySQL中将数组作为参数传递给存储过程? - How to pass array as parameter to stored procedure in mysql? 如何将IN参数传递给mysql存储过程 - How to pass IN parameter to a mysql stored procedure 如何在SQL中将空值传递给存储过程 - How to Pass null value to stored procedure in sql mysql:在存储过程中将sql语句作为参数传递以执行它 - mysql : Passing sql statement as parameter in stored procedure to execute it 如何在存储过程中将变量传递给具有语句-MYSQL? - How to pass the variable to having statement in stored procedure - MYSQL? 在MySQL中,如何将整数列表传递给存储过程中的准备好的语句? - In MySQL how to pass a list of integers to a prepared statement in a stored procedure? 如何将变量值传递给mysql中全文语句的存储过程? - How to pass variable value to stored procedure for fulltext statement in mysql? 如何将日期作为参数传递给PHP CodeIgniter中的MySQL存储过程? - How to pass date as a parameter to MySQL Stored Procedure in PHP CodeIgniter? 如何将C#数据表传递给mysql的存储过程参数 - How to pass C# datatable to mysql's stored procedure parameter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM