首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

一个存储过程的有关问题(部分语句不执行),解决马上给分

2012-01-06 
一个存储过程的问题(部分语句不执行),解决马上给分usezgwpjhgoifexists(selecttop1namefromsysobjectswher

一个存储过程的问题(部分语句不执行),解决马上给分
use   zgwpjh
go
if   exists(select   top   1   name   from   sysobjects   where   name= 'Edit_product '   and   type= 'p ')
drop   proc   Edit_product
go
Create   Procedure   [dbo].[Edit_product]
(
@pid   int,
@pic   varchar(150),
@wpmc   varchar(50),
@xjcd   int,
@wpsl   int,
@catecode   varchar(250),
@wpms   text,
@isshow   int,
@jhqy   varchar(150),
@jhtj   varchar(4000),
@wpproperty   varchar(250),
@wpzr   varchar(50),
@mycategory   varchar(50)
)
as
declare   @n   int
declare   @i   int
declare   @k   int
declare   @Lstr1   int
declare   @Lstr2   int
declare   @mycate   varchar(200)
declare   @property   varchar(250)
set   xact_abort   on
begin   tran
if   @catecode <> ' '
--更新物品类别总量
select   @Lstr1=len(物品类别)/4   from   products   where   id=@pid       --取得老类别
Set   @i=0
WHILE   @i   <   @Lstr1  
BEGIN  
Set   @i   =@i   +1  
Update   Category   set   pcount=pcount-1   Where   CateCode=Substring(@CateCode,1,@i*4)   --老类别上减一
END
Set   @Lstr2   =   Len(@catecode)/4  
Set   @k=0
WHILE   @k   <   @Lstr2  
BEGIN  
Set   @k   =@k   +1  
Update   Category   set   pcount=pcount+1   Where   CateCode=Substring(@CateCode,1,@k*4)   --新类别上增一
END
--更新物品类别总量结束
--产品信息入库
if   @pic= ' '
if   @catecode= ' '
begin
update   products   set   物品名称=@wpmc,新旧程度=@xjcd,物品数量=@wpsl,物品描述=@wpms,前台可见=@isshow,期望交换区域=@jhqy,交换条件=@jhtj,属性=@wpproperty,我的类别=@mycategory   where   id=@pid
end  
else
begin
update   products   set   物品名称=@wpmc,新旧程度=@xjcd,物品数量=@wpsl,物品描述=@wpms,前台可见=@isshow,期望交换区域=@jhqy,物品类别=@catecode,交换条件=@jhtj,属性=@wpproperty,我的类别=@mycategory   where   id=@pid
end
else
if   @catecode= ' '
begin
update   products   set   物品图片=@pic,物品名称=@wpmc,新旧程度=@xjcd,物品数量=@wpsl,物品描述=@wpms,前台可见=@isshow,期望交换区域=@jhqy,交换条件=@jhtj,属性=@wpproperty,我的类别=@mycategory   where   id=@pid
end  
else
begin
update   products   set   物品图片=@pic,物品名称=@wpmc,新旧程度=@xjcd,物品数量=@wpsl,物品描述=@wpms,前台可见=@isshow,期望交换区域=@jhqy,物品类别=@catecode,交换条件=@jhtj,属性=@wpproperty,我的类别=@mycategory   where   id=@pid
end
--产品入库结束
--更新当前物品的属性
select   @property=属性   from   products   where   id=@pid
if   @property <> ' '
UPdate   Cate_Property_value   set   pcount=pcount-1   where   valueid   in(@property)
if   @wpproperty <> ' '
UPdate   Cate_Property_value   set   pcount=pcount+1   where   valueid   in(@wpproperty)
--更新物品属性总量结束
select   @mycate=我的类别   from   products   where   id=@pid
update   mycategory   set   pcount=pcount-1   where   username=@wpzr   and   catename=@mycate
if   @mycategory <> ' '
update   mycategory   set   pcount=pcount+1   where   username=@wpzr   and   catename=@mycategory
commit   tran


现在的问题是
涉及到运算的代码都不执行



if   @catecode <> ' '
--更新物品类别总量
select   @Lstr1=len(物品类别)/4   from   products   where   id=@pid                   --取得老类别
Set   @i=0
WHILE   @i   <   @Lstr1  
BEGIN  
Set   @i   =@i   +1  
Update   Category   set   pcount=pcount-1   Where   CateCode=Substring(@CateCode,1,@i*4)   --老类别上减一
END
Set   @Lstr2   =   Len(@catecode)/4  
Set   @k=0
WHILE   @k   <   @Lstr2  
BEGIN  
Set   @k   =@k   +1  
Update   Category   set   pcount=pcount+1   Where   CateCode=Substring(@CateCode,1,@k*4)   --新类别上增一
END
--更新物品类别总量结束
--更新当前物品的属性
select   @property=属性   from   products   where   id=@pid
if   @property <> ' '
UPdate   Cate_Property_value   set   pcount=pcount-1   where   valueid   in(@property)
if   @wpproperty <> ' '
UPdate   Cate_Property_value   set   pcount=pcount+1   where   valueid   in(@wpproperty)
--更新物品属性总量结束
select   @mycate=我的类别   from   products   where   id=@pid
update   mycategory   set   pcount=pcount-1   where   username=@wpzr   and   catename=@mycate
if   @mycategory <> ' '
update   mycategory   set   pcount=pcount+1   where   username=@wpzr   and   catename=@mycategory


[解决办法]
楼主在if判断后的语句块中忘了用begin...end括起来.这样试试:
...
set xact_abort on
begin tran
if @catecode <> ' '
begin /*补上*/
--更新物品类别总量
...
end /*补上*/
--更新物品类别总量结束
--产品信息入库
if @pic= ' '
begin /*补上*/
if @catecode= ' '
...
else
...
end /*补上*/
else
begin /*补上*/
if @catecode= ' '
...
else
...
end /*补上*/
--产品入库结束
...

[解决办法]
--更新物品类别总量结束
--产品信息入库
if @pic= ' ' 需要 begin...end

=@mycategory where id=@pid
end
else 需要 begin...end

[解决办法]
如果不加begin end,则系统自动把if后的一句SQL作为需要根据if条件控制是否执行的SQL,而后续SQL无论if条件是真是假,都会执行,所以,不论if后是否真的有一条还是多条,增加begin end是一个好的习惯。


if 条件
begin
..
end
else
begin
...
end

[解决办法]
begin end
一定要加的呀.

热点排行