基於銀行數據庫 存儲過程、函數、觸發器、遊標練習

--存儲過程 函數 觸發器 複習

--1.輸入任意支行,統計其總客戶(存款客戶和貸款客戶)的個數,這裏要單獨寫
--create function customer_count4(@branch_name nvarchar(30))
--returns integer
-- begin
--    declare @a int
--    declare @b innt
--    declare @c int
--    set @a=(select count(*) from account join depositor on(account.account_number=depositor.account_number) where branch_name=@branch_name)
--    set @b=(select count(*) from loan where branch_name=@branch_name)
--    set @c=@a+@b
--    return @c
--end

--select dbo.customer_count4('ABC') as customer_count

--drop function customer_count1
--drop procedure cun_loan_count

--法二 存儲過程
--create procedure customer_count2(@branch_name nvarchar(30),@customer_count int output)
--as begin
--    declare @a int
--    declare @b int
    
--    set @a=(select count(*) from account join depositor on(account.account_number=depositor.account_number) where branch_name=@branch_name)
--    set @b=(select count(*) from loan where branch_name=@branch_name)
--    set @customer_count=@a+@b
--    end
--    declare @c_count int
--    execute customer_count2 @branch_name='abc',@customer_count=@c_count output
--    print @c_count


--2、輸入任意支行,返回一個zhi行的貸款總額,存款總額,總資產
--create procedure money_count(@branch_name nvarchar(30),@amount_count int output,@balance_count int output,@assets_count int output)
--as begin
--    set @amount_count=(select sum(amount) from loan where branch_name=@branch_name)
--    set @balance_count=(select sum(balance) from account where branch_name=@branch_name)
--    set @assets_count=(select assets from branch where branch_name=@branch_name)
--end

--declare @am int
--declare @ba int
--declare @as int
--execute money_count @branch_name='abc',@amount_count=@am output,@balance_count=@ba output,@assets_count=@as output
--print @am
--print @ba
--print @as

--3.輸入任意支行,返回存款客戶,客戶名稱及居住地點
--drop function customer_information

--create function customer_information(@branch_name nvarchar(30))
--returns table
--as return(
--    select c.customer_name,c.customer_street,c.customer_city
--    from account as a,depositor as d,customer as c
--    where branch_name=@branch_name
--)
--select distinct * from customer_information('abc')

--觸發器複習
--1.一個用於跟蹤哪個用戶、何時間更新了Account表的balance,
--drop table table2
--create table table10(
--    account_number nvarchar(30),
--    time datetime,
--    username nvarchar(30),
--    o_balance float,
--    i_balance float
--)
create trigger trigger0 on account after update
as
begin
    if(UPDATE(balance))
        begin
            declare @account_number nvarchar(30)
            declare @o_balance float
            declare @i_balance float
            declare aaa cursor for select i.account_number, i.balance,d.balance from inserted as i,deleted as d
                    open aaa
                    fetch next from aaa into @account_number,@o_balance,@i_balance
                    while @@FETCH_STATUS=0
                    begin
                        insert into table10 values(@account_number,GETDATE(),USER_NAME(),@o_balance,@i_balance)
                        fetch next from aaa into @account_number,@o_balance,@i_balance
                    end
                    close aaa
                    deallocate aaa
        end
    end
update account set balance=1000 where account_number='1'
select * from table10

--2.另一個用於跟蹤哪個用戶、何時間更新了loan表的amount,


--3.當Branch表的city爲NULL時,一律寫成“presently uncertain”
--drop trigger trigger1
--create trigger trigger2 on branch after update
--as
--begin
--    if(update(branch_city))
--    begin
--        declare @branch_name nvarchar(30)
--        declare @city nvarchar(30)
--        declare aaa cursor for select branch_city,branch_name from inserted
--        open aaa
--        fetch  next from aaa into @city,@branch_name
--        while @@fetch_status=0
--        begin
--            if(@city is null)
--                begin
--                update branch set branch_city='presently uncertain' where branch_name=@branch_name
--                fetch  next from aaa into @city,@branch_name
--                end
--            else
--                begin
--                update branch set branch_city=@city where branch_name=@branch_name
--                fetch  next from aaa into @city,@branch_name
--                end
            
--        end
--        close aaa
--        deallocate aaa
--    end
--end

--insert into branch values('chen' ,'yantai',1000)
--update branch set branch_city=null where branch_name='abc'
--select * from branch

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章