sql 创建多表删除的存储过程
Posted 小、野
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql 创建多表删除的存储过程相关的知识,希望对你有一定的参考价值。
CREATE PROCEDURE [dbo].[存储哦过程的名称]
@userID varchar(50) --声明变量
AS
BEGIN
IF(@userID=‘‘ OR @userID IS NULL) --判断是否输入
BEGIN
SELECT ‘0‘ as ‘message‘,‘请输入要删除的用户ID!‘ AS ‘ErrorInfo‘ --提示信息
RETURN
END
BEGIN TRANSACTION --开启事务
DELETE FROM 表1 WHERE ID = @userID; --删除表中数据
IF(@@ERROR<>0)
BEGIN
ROLLBACK TRANSACTION;
SELECT ‘0‘ as ‘message‘,‘删除失败!‘ AS ‘ErrorInfo‘
RETURN
END
DELETE FROM 表2 WHERE ID = @userID; --删除表中数据
IF(@@ERROR<>0)
BEGIN
ROLLBACK TRANSACTION;
SELECT ‘0‘ as ‘message‘,‘删除失败!‘ AS ‘ErrorInfo‘
RETURN
END
DELETE FROM 表3 WHERE ID = @userID; --删除表中信息
IF(@@ERROR<>0)
BEGIN
ROLLBACK TRANSACTION;
SELECT ‘0‘ as ‘message‘,‘删除失败!‘ AS ‘ErrorInfo‘
RETURN
END COMMIT TRANSACTION;
SELECT ‘0‘ as ‘message‘,‘删除成功!‘ AS ‘ErrorInfo‘
END;
以上是关于sql 创建多表删除的存储过程的主要内容,如果未能解决你的问题,请参考以下文章