• 数据库--事务:级联删除(学生教师信息表)为例


    --事物:保障整个流程的完整执行,全部没有问题统一提交,一旦有问题,回到原点
    --存储过程或者触发器里面用
    
    begin tran--事务的开始
    --开始写流程语句
    --语句写完之后
    if @@ERROR>0
    rollback--回滚事务
    else
    commit tran--提交事务
    
    
    
    ---------------------------------级联删除为例
    
    drop table Scorebak
    drop trigger Score_Delete
    --删除前先备份数据到备份表之后再删除
    create table Scorebak
    (
        ids int identity(1,1) primary key,
        sno varchar(50),
        cno varchar(50),
        degree decimal(4,1),
        dayetime datetime
    )
    create trigger Score_Delete --备份删除--学名:级联删除
    on Score
    instead of delete
    as
        declare @count int
        select @count = count(*) from deleted
        
        declare @i int
        set @i = 0
        while @i<@count 
        begin
            declare @sno varchar(20)
            declare @cno varchar(20)
            declare @degree decimal(4,1)
            
            select top 1 @sno=sno,@cno=cno,@degree=degree from deleted 
            where sno not in (select top (@i) sno from deleted) or cno not in(select top (@i) cno from deleted) 
            
            begin tran---------------------------------------------------
            
            insert into Scorebak values(@sno,@cno,@degree,getdate())
            
            delete from score where sno=@sno and cno=@cno
            if @@ERROR>0-------------------------------------------------
            begin
                rollback tran--------------------------------------------
            end
            else---------------------------------------------------------
            begin
                commit tran----------------------------------------------
            end        
            
            set @i=@i+1        
        end
    go
    select *from Scorebak
    select *from Score
    delete from Score where Cno='3-105'
    --事物:保障整个流程的完整执行,全部没有问题统一提交,一旦有问题,回到原点
    --存储过程或者触发器里面用
    
    begin tran--事务的开始
    --开始写流程语句
    --语句写完之后
    if @@ERROR>0
    rollback--回滚事务
    else
    commit tran--提交事务
    
    
    
    ---------------------------------级联删除为例
    
    drop table Scorebak
    drop trigger Score_Delete
    --删除前先备份数据到备份表之后再删除
    create table Scorebak
    (
        ids int identity(1,1) primary key,
        sno varchar(50),
        cno varchar(50),
        degree decimal(4,1),
        dayetime datetime
    )
    create trigger Score_Delete --备份删除--学名:级联删除
    on Score
    instead of delete
    as
        declare @count int
        select @count = count(*) from deleted
        
        declare @i int
        set @i = 0
        while @i<@count 
        begin
            declare @sno varchar(20)
            declare @cno varchar(20)
            declare @degree decimal(4,1)
            
            select top 1 @sno=sno,@cno=cno,@degree=degree from deleted 
            where sno not in (select top (@i) sno from deleted) or cno not in(select top (@i) cno from deleted) 
            
            begin tran---------------------------------------------------
            
            insert into Scorebak values(@sno,@cno,@degree,getdate())
            
            delete from score where sno=@sno and cno=@cno
            if @@ERROR>0-------------------------------------------------
            begin
                rollback tran
            end
            else---------------------------------------------------------
            begin
                commit tran----------------------------------------------
            end        
            
            set @i=@i+1        
        end
    go
    select *from Scorebak
    select *from Score
    delete from Score where Cno='3-245'
  • 相关阅读:
    锐浪报表应用系列二
    论产品和项目
    我的处女作
    今天晚上吃什么?
    今日晚餐
    PYTHON+数据库
    周末看到小区有个阿姨溜羊驼
    AD 10使用技巧---新学习
    使用.NET进行高效率互联网敏捷开发的思考和探索【一、概述】
    【开发随感】【一】【开发基础的基础】
  • 原文地址:https://www.cnblogs.com/dlexia/p/4466245.html
Copyright © 2020-2023  润新知