create proc usp_addnew
@whbn int,
@cbh char(10),
@sl float(8)
as
declare @rc int
select @rc=0
begin
if not exists (select * from yourtable where whbh=@whbn and cbh=@cbh)
insert into youtable values (@whbn,@cbh,@sl)
else
update yourtable set sl=@sl where whbh=@whbn and cbh=@cbh
select @rc=@@error
if @rc<>0
begin
select @rc=-1
return @rc
endend
@whbn int,
@cbh char(10),
@sl float(8)
as
declare @rc int
select @rc=0
begin
if not exists (select * from yourtable where whbh=@whbn and cbh=@cbh)
insert into youtable values (@whbn,@cbh,@sl)
else
update yourtable set sl=@sl where whbh=@whbn and cbh=@cbh
select @rc=@@error
if @rc<>0
begin
select @rc=-1
return @rc
endend
解决方案 »
- 求一老SQL语句~~~~~~~~~~在线等
- 动态查询要拼接SQL语句,最后用EXEC来执行,这个大家都知道,但是
- 请教:如何将oracle数据库里的一张表导到相应的sql server数据库中 (用语句完成)
- excel数据导入到SQL SERVER中的问题?
- 把sql数据库中的aa表里面的数据内容,导入到access数据库中的bb表里面
- 这句SQL能优化吗?
- 紧急请教SQL Server中:iif 的用法,我看了很久但总是不能运行???
- 如何分割列数据
- 我本机的SQL server2000越来越慢
- 不知开发数据库用什么语言最好?
- ★表的更改:“键列信息不足“什么意思?★
- 谢谢大家的帮忙,还有两个问题请教.也是关于存储过程的.
if @rc<>0
begin
select @rc=-1
return @rc
end这是什么意思。是不是表示没有错误。
create proc usp_addnew
@whbn int,
@cbh char(10),
@sl float(8)
as
declare @rc int
select @rc=0if not exists (select * from yourtable where whbh=@whbn and cbh=@cbh)
insert into youtable values (@whbn,@cbh,@sl)
else
update yourtable
set sl=sl+@sl
where whbh=@whbn and cbh=@cbhselect @rc=@@error
if @rc<>0
select @rc=-1
return @rcgo呵呵,版主也要上班,不一定在线。而且tj_dns(愉快的登山者) 给你解释了!
create unique index i_u_ytfwh on ytfwh(whbh,cbh)
---------------------------------------------------------------
create proc MyProc --这是你要的存储过程
@whbh int,
@cbh char(10),
@sl float(10)
as
update ytfwh
set sl = sl + @sl
where whbh = @whbh and cbh = @cbhif @@rowcount=0 --如果修改的记录数为零,则插入
insert into ytfwh(whbh,cbh,sl)
values(@whbh,@cbh,@sl)
-----------------------------------------------------------------
go--以下是执行结果:
exec myproc 1,'a',3.25
exec myproc 1,'b',5
exec myproc 1,'a',6select * from ytfwh--1 a 9.25
--1 b 5.0--建议,如果每次只插入一条记录,更好的办法是将这个
--控制建在ytfwh表的触发器里。
----------------------------------------------------------------
create trigger mytrig on ytfwh instead of insert as
update ytfwh
set sl = ytfwh.sl + b.sl
from inserted b
where ytfwh.whbh = b.whbh and ytfwh.cbh = b.cbhif @@rowcount=0 --如果修改的记录数为零,则插入
insert into ytfwh(whbh,cbh,sl)
select whbh,cbh,sl
from inserted
-----------------------------------------------------------------
--然后直接往该表插入数据就可以了。
用触发器不用参数呀,你直接在前端工具写insert语句也可以。如果不行就用存储过程吧。