第九章專案程式碼

大炮的大炮沒有大炮發表於2017-02-24
--修改張三的密碼
uSE bankDB
update cardInfo
set pass='123456'
where cardID='1010 3576 1234 5678' and customerID  in(select customerID from userInfo where customerName='張三')
--修改李四的密碼
USE bankDB
update cardInfo 
set pass='123123'
where cardID='1010 3576 1212 1134'  and customerID  in(select customerID from userInfo where customerName='李四')
--給李四的銀行卡掛失
USE bankDB
update cardInfo 
set IsReportLoss='1'
where cardID='1010 3576 1212 1134'  and customerID  in(select customerID from userInfo where customerName='李四')
--統計銀行資金流通餘額和盈利結算
declare @cun money
select @cun=sum(tradeMoney)  from  tradeInfo
where (tradeType='存入')
declare @qu money
select @qu=sum(tradeMoney)  from  tradeInfo
where (tradeType='支取')
declare @yu money
set @yu=@cun-@qu
print '銀行流通餘額總計為:'+convert(varchar(20),@yu)+'RMB'
DECLARE @yin money
set @yin=@qu*0.008-@cun*0.003
print '盈利結算為:'+convert(varchar(20),@yin)+'RMB'
--查詢本週開戶記錄
select  cardID from cardInfo where DATEDIFF(week,openDate,GETDATE())=0
--查詢本月交易金額最高的卡號
select distinct(cardID) from tradeInfo where tradeMoney=(select max(tradeMoney) from tradeInfo where tradeType='存入')  and DATEDIFF(MONTH,tradeDate,GETDATE())=0
select distinct(cardID) from tradeInfo where tradeMoney=(select max(tradeMoney) from tradeInfo where tradeType='支取')  and DATEDIFF(MONTH,tradeDate,GETDATE())=0
--查詢掛失使用者
select  customerName as 客戶姓名 from userInfo
where customerID in (select customerID from cardInfo where IsReportLoss='1')
--催款提醒業務
select customerName as 客戶姓名,telephone as 聯絡電話,balance as 存款餘額
from userInfo,cardInfo
where userInfo.customerID=cardInfo.customerID  and balance<200
--建立、使用檢視

create view vw_userinfo
as
select * from userinfo
select [customerID] as 客戶編號,[customerName] as 開戶名,[PID] as 身份證號碼,[telephone] as 電話號碼,[address] as 居住地址 from  vw_userinfo

create view vw_cardinfo
as
select [cardID] ,[curID] ,[savingID],[openDate],[openMoney]   ,[balance] ,[pass],[IsReportLoss] ,[customername]  from cardInfo,userinfo where cardInfo.customerID=userinfo.customerID
select [cardID] as 卡號,[curID] as 貨幣種類,[savingID] as 存款型別,[openDate] as 開戶日期,[openMoney] as 開戶金額,[balance] as 餘額,[pass] as 密碼,[IsReportLoss] as 是否掛失,[customerName] as 客戶 from  vw_cardInfo

create view vw_transinfo
as
select *  from tradeInfo
select [tradeDate] as 交易金額 ,[tradeType] as 交易型別,[cardID] as 卡號,[tradeMoney]as 交易金額,[remark] as 備註 from  vw_transinfo
--完成存款或取款業務
create procedure usp_takeMoney 
  @card char(19),
  @m money,
  @type char(4),
  @inputPass char(6)=''
 AS
   print '交易正進行,請稍後......'
   if (@type='支取')
      if ((SELECT pass FROM cardInfo WHERE cardID=@card)<>@inputPass )
         begin
           raiserror ('密碼錯誤!',16,1)
           return -1
         end

    DECLARE @mytradeType char(4),@outMoney MONEY,@myCardID char(19)
    SELECT @mytradeType=tradeType,@outMoney=tradeMoney ,@myCardID=cardID FROM tradeInfo where cardID=@card
    DECLARE @mybalance money
    SELECT @mybalance=balance FROM cardInfo WHERE cardID=@card
    if (@type='支取') 
       if (@mybalance>=@m+1)
           update cardInfo set balance=balance-@m WHERE cardID=@myCardID
       else
          begin
            raiserror ('交易失敗!餘額不足!',16,1)
            
            print '卡號'+@card+'  餘額:'+convert(varchar(20),@mybalance)   
            return -2
          end
    else
         update cardInfo set balance=balance+@m WHERE cardID=@card

    print '交易成功!交易金額:'+convert(varchar(20),@m)
    SELECT @mybalance=balance FROM cardInfo WHERE cardID=@card
    print '卡號'+@card+'  餘額:'+convert(varchar(20),@mybalance) 
	INSERT INTO tradeInfo(tradeType,cardID,tradeMoney) VALUES(@type,@card,@m) 
    RETURN 0
GO

declare @card char(19)
select @card=cardID from cardInfo Inner Join userInfo ON 
   cardInfo.customerID=userInfo.customerID where customerName='張三'
EXEC usp_takeMoney @card,10 ,'支取','123456' 
GO

select * from cardInfo Inner Join userInfo ON 
   cardInfo.customerID=userInfo.customerID where customerName='張三'

declare @card char(19)
select @card=cardID from cardInfo Inner Join userInfo ON 
   cardInfo.customerID=userInfo.customerID where customerName='李四'
EXEC usp_takeMoney @card,500 ,'存入'
select * from vw_cardInfo
select * from vw_tradeInfo
GO


if exists (select * from sysobjects where name = 'usp_randCardID')
	drop proc usp_randCardID
go
create procedure usp_randCardID @randCardID char(19) OUTPUT
  AS
    DECLARE @r numeric(15,8) 
    DECLARE @tempStr  char(10)
    SELECT  @r=RAND((DATEPART(mm, GETDATE()) * 100000 )+ (DATEPART(ss, GETDATE()) * 1000 )
                  + DATEPART(ms, GETDATE()) )
    set @tempStr=convert(char(10),@r) 
    set @randCardID='1010 3576 '+SUBSTRING(@tempStr,3,4)+' '+SUBSTRING(@tempStr,7,4)  --組合為規定格式的卡號
GO
--測試產生隨機卡號
DECLARE @mycardID char(19) 
EXECUTE usp_randCardID @mycardID OUTPUT
print '產生的隨機卡號為:'+@mycardID
GO



if exists (select * from sysobjects where name = 'usp_openAccount')
	drop proc usp_openAccount
GO
create procedure usp_openAccount @customerName char(8),@PID char(18),@telephone char(13)
     ,@openMoney money,@savingName char(8),@address varchar(50)='' 
AS
   DECLARE @mycardID char(19),@cur_customerID int, @savingID int 
   --呼叫產生隨機卡號的儲存過程獲得隨機卡號
   EXECUTE usp_randCardID @mycardID OUTPUT
   while  exists(SELECT * FROM cardInfo WHERE cardID=@mycardID) 
      EXECUTE usp_randCardID @mycardID OUTPUT
   print '尊敬的客戶,開戶成功!系統為您產生的隨機卡號為:'+@mycardID
   print '開戶日期'+convert(char(10),getdate(),111)+'  開戶金額:'+convert(varchar(20),@openMoney)
   IF not exists(select * from userInfo where PID=@PID)
       INSERT INTO userInfo(customerName,PID,telephone,address )
          VALUES(@customerName,@PID,@telephone,@address) 
	SELECT @savingID = savingID FROM deposit WHERE savingName =@savingName

	if @savingID is NULL
	 BEGIN
	  RAISERROR('存款型別不正確,請重新輸入!',16,1)
	  RETURN -1
	 END

    select @cur_customerID=customerID from userInfo where PID=@PID

    INSERT INTO cardInfo(cardID,savingID,openMoney,balance,customerID)
         VALUES(@mycardID,@savingID,@openMoney,@openMoney,@cur_customerID)
GO

--呼叫儲存過程重新開戶
EXEC usp_openAccount '王五','334456889012678','2222-63598978',1000,'活期','河南新鄉' 
EXEC usp_openAccount '趙二','213445678912342222','0760-44446666',1,'定期' 
select * from vw_userInfo
select * from vw_cardInfo
GO



if exists (select * from sysobjects where name = 'usp_CheckSheet')
	drop proc usp_CheckSheet
GO
CREATE PROCEDURE usp_CheckSheet
  @cardID varchar(19),
  @date1 datetime=NULL,
  @date2 datetime=NULL
AS
	DECLARE @custName varchar(20)
	DECLARE @curName varchar(20)
	DECLARE @savingName varchar(20)
	DECLARE @openDate datetime
	SELECT @cardID=c.cardID, @curName=c.curID, @custName=u.customerName,
		   @savingName=d.savingName , @openDate=c.openDate 
	FROM cardInfo c, userInfo u, deposit d
	WHERE c.customerID=u.customerID and c.savingID = d.savingID and cardID = @cardID --and u.customerName = user_name()
	PRINT '卡號:' + @cardID
	PRINT '姓名:' + @custName
	PRINT '貨幣:' + @curName
	PRINT '存款型別:' + @savingName
	PRINT '開戶日期:' + CAST(DATEPART(yyyy,@openDate) AS VARCHAR(4))+'年' + CAST(DATEPART(mm,@openDate) AS VARCHAR(2))+'月' + CAST(DATEPART(dd,@openDate) AS VARCHAR(2))+'日'
	PRINT ' '
	print '--------------------------------------------------------------------'

    IF @date1 IS NULL AND  @date2 IS NULL 
      BEGIN
		SELECT tradeDate 交易日, tradeType 型別, tradeMoney 交易金額, remark 備註
		FROM tradeInfo
		WHERE cardID='1010 3576 1212 1134'--@cardID
		ORDER BY tradeDate
		RETURN
      END
    ELSE IF @date2 IS NULL 
      SET @date2 = getdate()
      
	SELECT tradeDate 交易日, tradeType 型別, tradeMoney 交易金額, remark 備註
	FROM tradeInfo
	WHERE cardID=@cardID AND tradeDate BETWEEN @date1 AND @date2
	ORDER BY tradeDate
GO
--測試列印對帳單
EXEC usp_CheckSheet '1010 3576 1212 1134'

EXEC usp_CheckSheet '1010 3576 1212 1134','2009-11-2','2009-11-30'



if exists (select * from sysobjects where name = 'usp_pagingDisplay')
	DROP PROCEDURE usp_pagingDisplay
GO
CREATE PROCEDURE usp_pagingDisplay
  @records int = 10,
  @page int = 1
AS
  SET NOCOUNT ON

  DECLARE @rec1 int
  SET @rec1 = @records
  DECLARE @rec2 int
  SET @rec2 = (@page - 1) * @records

  DECLARE @statement nvarchar(200)
  SET @statement='SELECT TOP ' + CAST(@rec1 AS varchar(10)) + ' tradeDate 交易日期,tradeType 交易型別,cardID 卡號,trademoney 交易金額 FROM tradeInfo WHERE cardID not in (SELECT TOP '+ CAST(@rec2 AS varchar(10)) + ' cardID FROM tradeInfo)' 

  EXEC SP_EXECUTESQL @statement,N'@rec1 int,@rec2 int',@rec1,@rec2
GO
--
EXEC usp_pagingDisplay 2,2


if exists (select * from sysobjects where name = 'usp_getWithoutTrade')
	drop proc usp_getWithoutTrade
GO
create procedure usp_getWithoutTrade
  @Num int output,
  @Amount decimal(18,2) output,
  @date1 datetime = NULL,
  @date2 datetime = NULL
AS
  IF @date1 IS NULL
  BEGIN
	declare @dateStr varchar(50)
	set @dateStr = convert(varchar(4),DATEPART(YY,GETDATE())) + '-'+convert(varchar(2),DATEPART(mm,GETDATE())) + '-1 00:00:00.000' 
	set @date1 = convert(datetime, @datestr,101)
  END

  IF @date2 IS NULL
	SET @date2 = getdate()

  SELECT distinct u.customerID 客戶號,u.customerName 客戶姓名,u.PID 身份證號,u.telephone 電話,address 地址 
  FROM userInfo u
  JOIN cardInfo c ON u.customerID = c.customerID
  WHERE c.cardID NOT IN (SELECT cardID FROM tradeInfo WHERE tradeDate Between @date1 and @date2)

  SELECT @Num=COUNT(customerID), @Amount=SUM(balance)
  FROM cardInfo
  WHERE cardID NOT IN (SELECT cardID FROM tradeInfo WHERE tradeDate Between @date1 and @date2)
GO

DECLARE @NUM int
DECLARE @Amount decimal(18,2)
DECLARE @date1 datetime
DECLARE @date2 datetime
SET @date1 = '2009-1-1'
SET @date2 = getdate()
EXEC usp_getWithoutTrade @NUM OUTPUT, @Amount OUTPUT--, @date1, @date2
PRINT '統計未發生交易的客戶'
PRINT '---------------------------------------'
PRINT '客戶人數:' + CAST(@NUM AS varchar(10)) + '  客戶總餘額:' + CAST(@Amount AS varchar(20))


if exists (select * from sysobjects where name = 'usp_getTradeInfo')	
	drop proc usp_getTradeInfo
GO
create procedure usp_getTradeInfo
  @Num1 int output,
  @Amount1 decimal(18,2) output,
  @Num2 int output,
  @Amount2 decimal(18,2) output,
  @date1 datetime,
  @date2 datetime = NULL,
  @address varchar(20) = NULL
AS
  -- 初始化變數
  SET @Num1 = 0
  SET @Amount1 = 0

  SET @Num2 = 0
  SET @Amount2 = 0

  IF @date2 IS NULL
	SET @date2 = getdate()

  IF @address IS NULL
   BEGIN
	  SELECT @Num1=COUNT(tradeMoney), @Amount1=SUM(tradeMoney)
	  FROM tradeInfo
	  WHERE tradeDate BETWEEN @date1 AND @date2 AND tradeType='存入'

	  SELECT @Num2=COUNT(tradeMoney), @Amount2=SUM(tradeMoney)
	  FROM tradeInfo
	  WHERE tradeDate BETWEEN @date1 AND @date2 AND tradeType='支取'
    END
  ELSE
   BEGIN
	  SELECT @Num1=COUNT(tradeMoney), @Amount1=SUM(tradeMoney)
	  FROM tradeInfo JOIN cardInfo ON tradeInfo.cardID = cardInfo.cardID
           JOIN userInfo ON cardInfo.customerID = userInfo.customerID
	  WHERE tradeDate BETWEEN @date1 AND @date2 AND tradeType='存入'
        AND address Like '%'+@address+'%'
	  SELECT @Num2=COUNT(tradeMoney), @Amount2=SUM(tradeMoney)
	  FROM tradeInfo JOIN cardInfo ON tradeInfo.cardID = cardInfo.cardID
           JOIN userInfo ON cardInfo.customerID = userInfo.customerID
	  WHERE tradeDate BETWEEN @date1 AND @date2 AND tradeType='支取'
        AND address Like '%'+@address+'%'
    END
GO


DECLARE @CNT1 int
DECLARE @Total1 decimal(18,2)
DECLARE @CNT2 int
DECLARE @Total2 decimal(18,2)
DECLARE @date1 datetime
DECLARE @date2 datetime
SET @date1 = '2009-1-1'
SET @date2 = getdate()
EXEC usp_getTradeInfo @CNT1 OUTPUT, @Total1 OUTPUT, @CNT2 OUTPUT, @Total2 OUTPUT, @date1, @date2--, '北京'
PRINT '統計銀行卡交易量和交易額'
PRINT ''
PRINT '起始日期:' + CONVERT(varchar(10),@date1,102) +  '  截止日期:' + CONVERT(varchar(10),@date2,102)
PRINT '-----------------------------------------------------------'
PRINT '存入筆數:' + CAST(@CNT1 AS varchar(20)) + '  存入金額:' + CAST(@Total1 AS varchar(20))
PRINT '支取筆數:' + CAST(@CNT2 AS varchar(20)) + '  支取金額:' + CAST(@Total2 AS varchar(20))
PRINT '-----------------------------------------------------------'
PRINT '發生筆數:' + CAST(@CNT1+@CNT2 AS varchar(20)) + '  結餘金額:' + CAST(@Total1-@Total2 AS varchar(20))
GO

--轉帳的事務儲存過程
if exists (select * from sysobjects where name = 'usp_tradefer')
	drop proc usp_tradefer
GO
create procedure usp_tradefer 
    @card1 char(19),
    @pwd char(6),
    @card2 char(19),
    @outmoney money
 AS
   DECLARE @date1 datetime
   DECLARE @date2 datetime
   SET @date1 = getdate()

   begin tran
     print '開始轉帳,請稍後......'
     DECLARE @errors int
     set @errors=0
	 DECLARE @result int

     EXEC @result=usp_takeMoney @card1,@outmoney ,'支取',@pwd --'123123'
     set @errors=@errors+@@error

	 if (@errors > 0 or @result <> 0)
			begin
			  print '轉帳失敗!'
			  rollback tran
			  RETURN -1
			end
     EXEC @result=usp_takeMoney @card2,@outmoney ,'存入'
     set @errors=@errors+@@error
     if (@errors > 0 or @result <> 0)
        begin
          print '轉帳失敗!'
          rollback tran
          RETURN -1
        end
     else
        begin
          print '轉帳成功!'
          commit tran

          SET @date2 = getdate()
          print '列印轉出賬戶對賬單'
		  PRINT '-------------------'
		  EXEC usp_CheckSheet @card1,@date1,@date2
          print '列印轉入賬戶對賬單'
		  PRINT '-------------------'
		  EXEC usp_CheckSheet @card2,@date1,@date2

          RETURN 0
        end
GO


declare @card1 char(19),@card2 char(19)
select @card1=cardID from cardInfo Inner Join userInfo ON 
   cardInfo.customerID=userInfo.customerID where customerName='李四'
select @card2=cardID from cardInfo Inner Join userInfo ON 
   cardInfo.customerID=userInfo.customerID where customerName='張三'
--呼叫上述事務過程轉帳
EXEC usp_tradefer @card1,'123123',@card2,2000

select * from vw_userInfo
select * from vw_cardInfo
select * from vw_tradeInfo
GO

--1.新增SQL登入帳號
CREATE LOGIN appAdmin WITH PASSWORD='bank1234'
GO
--2.建立資料庫使用者 
USE bankDB
GO
CREATE USER appAdmin FOR LOGIN appAdmin 
GO
--3. 給資料庫使用者appAdmin授權(增刪改查的許可權)
 GRANT select,insert,update,delete,select  ON vw_userInfo 
                TO appAdmin
 GRANT select,insert,update,delete,select ON vw_tradeInfo 
                TO appAdmin
 GRANT select,insert,update,delete,select ON vw_cardInfo 
                TO appAdmin
GO

相關文章