/*--1.取钱或存钱的存储过程*/
if exists (select * from sysobjects where name = 'usp_takeMoney')
drop proc usp_takeMoney
go
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
--调用存储过程取钱或存钱 张三取300,李四存500
--现实中的取款机依靠读卡器读出张三的卡号,这里根据张三的名字查出考号来模拟
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
/*--2.产生随机卡号的存储过程(一般用当前月份数\当前秒数\当前毫秒数乘以一定的系数作为随机种子) --*/
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) --产生0.xxxxxxxx的数字,我们需要小数点后的八位数字
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
/*--3.开户的存储过程--*/
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
-- SELECT savingID FROM deposit WHERE savingName ='活期'
if @savingID is NULL
BEGIN
RAISERROR('存款类型不正确,请重新输入!',16,1)
RETURN -1
END
--PRINT CAST(@savingID AS varchar(10))
select @cur_customerID=customerID from userInfo where PID=@PID
--PRINT CAST(@cur_customerID AS varchar(10))
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
/*---- 4.打印对账单 ----*/
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'
/*--5.输入页数和每页显示的记录数,实现分页显示*/
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 --@page * @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)'
--print @statement
EXEC SP_EXECUTESQL @statement,N'@rec1 int,@rec2 int',@rec1,@rec2
GO
--
EXEC usp_pagingDisplay 2,2
/*--6.查询、统计在指定时间段内没有发生交易的账户信息*/
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))
/*--7.统计银行卡交易量和交易额*/
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_takeMoney')
drop proc usp_takeMoney
go
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
--调用存储过程取钱或存钱 张三取300,李四存500
--现实中的取款机依靠读卡器读出张三的卡号,这里根据张三的名字查出考号来模拟
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
/*--2.产生随机卡号的存储过程(一般用当前月份数\当前秒数\当前毫秒数乘以一定的系数作为随机种子) --*/
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) --产生0.xxxxxxxx的数字,我们需要小数点后的八位数字
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
/*--3.开户的存储过程--*/
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
-- SELECT savingID FROM deposit WHERE savingName ='活期'
if @savingID is NULL
BEGIN
RAISERROR('存款类型不正确,请重新输入!',16,1)
RETURN -1
END
--PRINT CAST(@savingID AS varchar(10))
select @cur_customerID=customerID from userInfo where PID=@PID
--PRINT CAST(@cur_customerID AS varchar(10))
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
/*---- 4.打印对账单 ----*/
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'
/*--5.输入页数和每页显示的记录数,实现分页显示*/
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 --@page * @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)'
--print @statement
EXEC SP_EXECUTESQL @statement,N'@rec1 int,@rec2 int',@rec1,@rec2
GO
--
EXEC usp_pagingDisplay 2,2
/*--6.查询、统计在指定时间段内没有发生交易的账户信息*/
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))
/*--7.统计银行卡交易量和交易额*/
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