mysql 存储过程 通过传入ids[]数组参数, 实现批量删除数据!


求大神解答。 存储过程,接收的参数为,保存多个id的字符串数组 ,以实现批量删除的 ,这样的存储过程怎么写?

mysqli 存储过程

纯粹精神恋爱 9 years, 7 months ago

为什么一定要使用存储过程呢??用循环执行Sql 不行吗???

loli控. answered 9 years, 7 months ago

create procedure sp_split_string
(
@string NVARCHAR(4000)
)
AS
BEGIN
DECLARE @object_id nvarchar(500)
DECLARE @i INT
DECLARE @len INT
print @string
IF ( @string IS NULL) OR (LTRIM(@string) = '')
RETURN


 WHILE CHARINDEX(',',@string) > 0
    BEGIN
        SET @len = LEN(@string)
        SET @i = CHARINDEX(',', @string)
        SET @object_id = LEFT(@string, @i-1)

        INSERT INTO a (id) VALUES (@object_id)--少做修改,改成需要的sql语句即可
        SET @string = RIGHT(@string, @len - @i)
    END
SET @object_id = @string
INSERT INTO a (id) VALUES (@object_id)--少做修改,改成需要的sql语句即可

END
go

--测试
-- select * from a
-- exec sp_split_string '102,103,105,106,107,108,200,500,306,408'
-- select * from a

s3mytao answered 9 years, 7 months ago

php的思路可以是这样的:


 $id = [1,2,3,5,6];//传入的id数组

$delete_in = implode(",", $id);// 1,2,3,5,6

$query = ... delete where id in ( $delete_id ); //大概是这样的

kairyu answered 9 years, 7 months ago

Your Answer