2

我正在使用 mysql,我有一个存储过程,它有一个输入变量。

我想在 select 语句中使用这个变量(带有 like 子句)。

例如:

DELIMITER $$

DROP PROCEDURE IF EXISTS `DeleteDataByTransactionID` $$
CREATE DEFINER=`root`@`%` PROCEDURE `DeleteDataByTransactionID`(in **$TransactionID** varchar(50))

BEGIN

delete from sqlstatements where tempsql like '%'+ **$TransactionID** + '%';

END $$


DELIMITER ;

谢谢

4

2 回答 2

8
DELIMITER $$
DROP PROCEDURE IF EXISTS `DeleteDataByTransactionID`
$$
CREATE DEFINER=`root`@`%` PROCEDURE `DeleteDataByTransactionID`(TransactionID VARCHAR(50))
BEGIN
        DELETE
        FROM    sqlstatements
        WHERE   tempsql LIKE CONCAT('%', TransactionID, '%');
END
$$
DELIMITER ;
于 2009-05-20T14:48:35.110 回答
2

实际上,如果调用者没有正确参数化对存储过程的调用,则接受的答案版本对 SQL 注入开放。我建议在存储过程中使用准备好的语句,如下所示是安全的,而不是依赖调用者:

DELIMITER $$
CREATE PROCEDURE `DeleteDataByTransactionID`
(
    TransactionID VARCHAR(50)
)
BEGIN

SET @sql = 
 "DELETE 
  FROM sqlstatements
  WHERE   
     tempsql LIKE CONCAT('%', ?, '%')";

SET @transid  = TransactionID;

PREPARE stmt FROM @sql;
EXECUTE stmt USING @transid;
DEALLOCATE PREPARE stmt;

END
$$
于 2009-08-18T06:54:55.197 回答