我的查询执行并返回一个值,我需要在同一个过程中使用返回值,如何使用它?
我的代码是
set @table_name_count = CONCAT('SELECT COUNT(*) into @xcount ', CONCAT( ' FROM ' ) , concat( table_name),concat( ' where issueNumber=',concat('30030'),concat( ' and ISSNcode='),CONCAT('112640402')));
PREPARE statement from @table_name_count;
EXECUTE statement;
DEALLOCATE PREPARE statement;
select 'hello4' from dual;
IF xcount > 0 and xcount<>NULL THEN
DELETE from table_name where issueNumber='30030' and ISSNcode='112640402' ;
END IF ;
这里我需要在条件中使用@xcount
IF xcount > 0 and xcount<>NULL THEN
DELETE from table_name where issueNumber='30030' and ISSNcode='112640402' ;
END IF ;
最佳答案
因为您正在处理用户( session )变量,所以您只是忘记了变量名称前的 @
。你的 IF
语句应该是这样的
IF @xcount > 0 THEN
^
您不需要检查 NULL
,因为 COUNT()
总是返回一个值。
这是 SQLFiddle 演示
现在没有意义在发出DELETE
之前检查记录是否存在。
如果 DELETE
语句的 WHERE
子句中没有匹配条件的行,它不会执行任何操作,也不会产生任何错误。
因此您可以放弃除DELETE
语句之外的所有代码。
关于mysql在同一过程中使用返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21037982/