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;

posted on 2017-05-31 11:36  嘿呀土豆  阅读(1394)  评论(0编辑  收藏  举报

导航