日期:2014-05-19  浏览次数:21007 次

(牛人在不在?)触发器问题
《tbVIP》
VIPID varchar(20) Not   Null VIP卡号明码(主键)
VIPWord varchar(20) Not   Null VIP卡号暗码(主键)
CurMoney money Not   Null vip卡内当前余额(默认为0)
JoinTime datetime Not   Null vip注册日期
Score(默认为0) int Not   Null 积分(默认为0分)
CardType varchar(10) Not   Null 卡类型(金卡、银卡、普通卡)(外键)
Phone varchar(20) NUll VIP联系电话
Email varchar(20) NULL VIP联系Email
Note varchar(50) null VIP备注说明
<tbVIPRule>
CardType varchar(10) Not   Null VIP卡类型(主键)
Charge float Not   Null VIP打折率(0——1)
MinScore int Not   Null 积分下限
MaxScore int Not   Null 积分上限
//------------------------------------
如何写一个触发器,当修改tbVIPRule中积分上下限时自动更改表tbVIP中的CardType字段?
      万分火急!请大家帮忙解决

------解决方案--------------------
CREATE TRIGGER [TRIGGER_tbVIPRule] ON [dbo].[tbVIPRule]
FOR UPDATE
AS
update A
set CardType = B.CardType
from tbVIP A inner join inserted B on A.Score > = B.MinScore and A.Score < B.MaxScore
where A.CardType <> B.CardType
if ( @@error= 'N ')
begin
rollback
RaisError( '提示错误信息! ',16,3)
return
end

------解决方案--------------------

ALTER TRIGGER [tr_update_cpzd] ON dbo.cpzd
FOR UPDATE
AS
if update(cpbh)
return
if not (update(cartonqty) or update(GrossWeight) or update(dz) or update(cpfl) or update(bjyl_xc) or update(bjyl_dz) or update(bjyl_qt) or update(bjyl_sk))
return
declare @cpbh varchar(20),@dz numeric(18,4),@dz_new numeric(18,4),@工号 char(6),@title varchar(250),@content varchar(1000)
declare @gw_new numeric(18,4),@gw numeric(18,4),@gw_revno int,@bjylsum numeric(18,4)
declare @cpfl varchar(20),@cpfl_new varchar(20)
declare @dz_revno int ,@dzbl numeric(18,4)
declare @bjylsum_new numeric(18,4),@bomdz numeric(18,4),@dzcb numeric(18,4)

set @工号=dbo.查询工号(system_user)
declare cursor_cpzd cursor for
select cpbh,dz,dz_revno,cpfl,grossweight,gw_revno ,bjyl_sum from deleted
open cursor_cpzd
fetch next from cursor_cpzd into @cpbh,@dz,@dz_revno,@cpfl,@gw,@gw_revno,@bjylsum
while @@fetch_status=0
begin
if update(DZ)
begin
update cpzd set dz_dlr=system_user,dz_dlsj=getdate(),dz_revno=dz_revno+1 where cpbh=@cpbh
set @dz_new=(select isnull(dz,0) from inserted where cpbh=@cpbh)
update cpzd set netweight=isnull(@dz_new,0)*isnull(cartonqty,0)/1000 where cpbh=@cpbh
insert into cpdz_update(cpbh,dz,dz_new,update_user,update_date) values(@cpbh,isnull(@dz,0),isnull(@dz_new,0),system_user,getdate())
----如果 (新单重-旧单重)/旧单重 else (旧单重-新单重)/旧单重 //Kyle modifyDate 20070705 10:50
if isnull(@dz,0)=0
begin
return
set @dzbl=1
end
else
begin
if @dz_new> =@dz
begin
set @dzbl=(isnull(@dz_new,0)-isnull(@dz,0))/@dz
end
else
begin
set @dzbl=(isnull(@dz,0)-isnull(@dz_new,0))/@dz
end
end
if @dzbl> 0.01 --单重更改范围大于0.01,发BBS
begin
set @title= '产品单重变更: '+@cpbh
set @content= '产品编号: '+@cpbh+char(10)+
'更改前单重: '+str(isnull(@dz,0),18,4)+char(10)+
'更改后单重: '+str(isnull(@dz_new,0),18,4)+char(10)+
'更改比重: '+str(@dzbl*100)+ '% '+char(10)+
'更改人: '+system_user+char(10)+
'更改时间: '+convert(varchar,getdate(),120)+char(10)+
'更改次数: '+str(isnull(@dz_revno,0)+1)
exec insert_article 116,@工号,@title,@content
end
end

----