首页 新闻 会员 周边

SQL;水电气收费!存储过程写语句:请问哪位大侠怎么在得到用户余额!随时关注

0
悬赏园豆:50 [待解决问题]

任务 题目!
任务描述:
假设实际应用:某物管公司每月抄录每个住户的水、电、气读数,并根据该值与上月的差值计算本月应交纳的费用。住户不是逐月来交纳费用,可能不定期来交纳。水、电、气的单价可能每月不同。设计数据库结构。
实现如下的功能:
录入读数存储过程:传入用户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

 是计算余额的    但是在查表时候只有 存入了多少, 没有显示余额啊

急急急!!!

sj3136456的主页 sj3136456 | 初学一级 | 园豆:35
提问于:2010-09-28 23:36
< >
分享
所有回答(2)
0

写反了吧

if exists (select * from users  where  usersname=@usersname )

@GB | 园豆:190 (初学一级) | 2010-09-29 08:49
谢谢你的对问题的关注!!
支持(0) 反对(0) sj3136456 | 园豆:35 (初学一级) | 2010-09-29 10:42
0

--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去掉就可以了

天神一 | 园豆:845 (小虾三级) | 2010-09-29 13:03
你好! 谢谢你的提示,我已经改改了! 请持续关注; 最后才能给分哈!
支持(0) 反对(0) sj3136456 | 园豆:35 (初学一级) | 2010-09-29 13:39
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册