任务 题目!
任务描述:
假设实际应用:某物管公司每月抄录每个住户的水、电、气读数,并根据该值与上月的差值计算本月应交纳的费用。住户不是逐月来交纳费用,可能不定期来交纳。水、电、气的单价可能每月不同。设计数据库结构。
实现如下的功能:
录入读数存储过程:传入用户id、年份、月份、水读数、电读数、气读数,将这些值存储到数据库中,并计算该用户本月应交纳的费用。
缴费存储过程:传入用户id、缴费金额,将这些值和缴费时间存储到数据库中,计算该用户缴费后结余款。
欠费告警视图:列出欠款超过1000元以上的住户。
___附加 表
create table [user]
(
userid int IDENTITY(1,1) not null,
[money] int not null,
constraint PK_user primary key nonclustered (userid)
)
create table [save]
(
saveid int identity(1,1) not null,
userid int not null,
[year] int not null,
[month] int not null,
[money] int not null,
constraint PK_save primary key nonclustered (saveid)
)
create table [cb]
(
cbid int identity(1,1) not null,
userid int not null,
[year] int not null,
[month] int not null,
enumber int not null,
wnumber int not null,
anumber int not null,
[pay] int not null,
constraint PK_cb primary key nonclustered (cbid)
)
create table [price]
(
priceid int identity(1,1) not null,
[year] int not null,
[month] int not null,
[eprice] int not null,
[wprice] int not null,
[aprice] int not null,
constraint PK_price primary key nonclustered (priceid)
)
————附加 表 字段描述
user表是用户账户,主码是userid,money是账户里的钱
save表是存钱信息,主码是saveid,userid是用户,year是年份,month是月,money是钱
cb是查表数据,enumber是电表,wnumber是水表,anumber是气表,pay是当月应该支付的钱
price是价格表,eprice是电费,wprice是水费,aprice是气费
create procedure [dbo].[userspay]
@userid int,
@year int,
@month int,
@enumber int,
@wnumber int,
@anumber int,
@money int
as
declare @eprice int,@wprice int,@aprice int,@pay int,@eold int,@wold int,@aold int,@yold int,@mold int
select @eprice=eprice,@wprice=wprice,@aprice=aprice from price where [year]=@year and [month]=@month
if @eprice is null
print '当月价格没设置'
else
if exists(select * from [users] where userid=@userid)
begin
if @month=1
begin
Set @mold=12
Set @yold=@year-1
end
else
begin
Set @mold=@month-1
set @yold=@year
end
select @eold=enumber,@wold=wnumber,@aold=@anumber from cb where [year]=@yold and [month]=@mold
set @pay=@eprice*(@enumber-@eold)+@wprice*(@wnumber-@wold)+@aprice*(@anumber-@aold)
insert into cb
(
userid,
[year],
[month],
enumber,
wnumber,
anumber,
pay
)
values
(
@userid,
@year,
@month,
@enumber,
@wnumber,
@anumber,
@pay
)
update [users] set [money]=[money]-@pay where userid=@userid
print '扣费结束'
end
else
print '用户号错误'
if exists(select * from [users] where userid=@userid)
begin
insert into [ssave]
(
userid,
[year],
[month],
[money]
)
values
(
@userid,
@year,
@month,
@money
)
update [users] set [money]=[money]+@money where userid=@userid
print '存钱成功'
end
else
print '住户号有误'
—————— update [users] set [money]=[money]-@pay where userid=@userid
是计算余额的 但是在查表时候只有 存入了多少, 没有显示余额啊
急急急!!!
写反了吧
if exists (select * from users where usersname=@usersname )
--create table [user]
--(
-- userid int IDENTITY(1,1) not null,
-- [money] int not null,
-- constraint PK_user primary key nonclustered (userid)
--)
--
--create table [save]
--(
--saveid int identity(1,1) not null,
--userid int not null,
--[year] int not null,
--[month] int not null,
--[money] int not null,
--constraint PK_save primary key nonclustered (saveid)
--)
--create table [cb]
--(
--cbid int identity(1,1) not null,
--userid int not null,
--[year] int not null,
--[month] int not null,
--enumber int not null,
--wnumber int not null,
--anumber int not null,
--[pay] int not null,
--constraint PK_cb primary key nonclustered (cbid)
--)
--create table [price]
--(
--priceid int identity(1,1) not null,
--[year] int not null,
--[month] int not null,
--[eprice] int not null,
--[wprice] int not null,
--[aprice] int not null,
--constraint PK_price primary key nonclustered (priceid)
--)
--go
--附加 表 字段描述
--user表是用户账户,主码是userid,money是账户里的钱
--save表是存钱信息,主码是saveid,userid是用户,year是年份,month是月,money是钱
--cb是查表数据,enumber是电表,wnumber是水表,anumber是气表,pay是当月应该支付的钱
--price是价格表,eprice是电费,wprice是水费,aprice是气费
--下面是存储语句
create procedure [dbo].[userspay]
@userid int,
@year int,
@month int,
@enumber int,
@wnumber int,
@anumber int,
@money int
as
declare @eprice int,@wprice int,@aprice int,@pay int,@eold int,@wold int,@aold int,@yold int,@mold int
select @eprice=eprice,@wprice=wprice,@aprice=aprice from price where [year]=@year and [month]=@month
if @eprice is null
print '当月价格没设置'
else
if exists(select * from [user] where userid=@userid)
begin
if @month=1
begin
Set @mold=12
Set @yold=@year-1
end
else
begin
Set @mold=@month-1
set @yold=@year
end
select @eold=enumber,@wold=wnumber,@aold=@anumber from cb where [year]=@yold and [month]=@mold
set @pay=@eprice*(@enumber-@eold)+@wprice*(@wnumber-@wold)+@aprice*(@anumber-@aold)
insert into cb
(
userid,
[year],
[month],
enumber,
wnumber,
anumber,
pay
)
values
(
@userid,
@year,
@month,
@enumber,
@wnumber,
@anumber,
@pay
)
update [user] set [money]=[money]-@pay where userid=@userid
print '扣费结束'
end
else
print '用户号错误'
if exists(select * from [user] where userid=@userid)
begin
insert into [save]
(
userid,
[year],
[month],
[money]
)
values
(
@userid,
@year,
@month,
@money
)
update [user] set [money]=[money]+@money where userid=@userid
print '存钱成功'
end
else
print '住户号有误'
兄弟,上面写的是正确的,把if上面的那个end去掉就可以了