2 u014420185 u014420185 于 2015.06.20 10:39 提问

SQL SEVER触发器,在条件判断中,怎么实现更新一行,而不是一列

USE [Ticket]
GO

create Trigger [dbo].[Price]
on [dbo].[OrderList]
after insert
as
begin
declare @FlightId char(3)
declare @TicketType char(1)
select @TicketType = TicketType from inserted
select @FlightId = FlightId from inserted

declare @Flight_Id char(3)
declare @FPrice float
declare @BPrice float
declare @EPrice float
declare @Rebate float
declare @bool real
select @Flight_Id=FlightId
from Ticket
where FlightId=@FlightId
select @FPrice=FirstClassPrice
from Ticket
where FlightId=@FlightId
select @BPrice=BusinessClassPrice
from Ticket
where FlightId=@FlightId
select @EPrice=EconomyClassPrice
from Ticket
where FlightId=@FlightId
select @Rebate=Rebate
from Ticket
where FlightId=@FlightId
update OrderList
set TicketPrice = @FPrice * @Rebate
where (@Flight_Id = @FlightId and @TicketType like '1')
update OrderList
set TicketPrice = @BPrice * @Rebate
where (@Flight_Id = @FlightId and @TicketType like '2')
update OrderList
set TicketPrice = @EPrice * @Rebate
where (@Flight_Id = @FlightId and @TicketType like '3')

end

为什么一列都会更新?

2个回答

danielinbiti
danielinbiti   Ds   Rxr 2015.06.20 12:32
 update OrderList 
set TicketPrice = @BPrice * @Rebate
where (@Flight_Id = @FlightId and @TicketType like '2')

随便抽一句SQL,看看where条件,和当前表的字段没有任何联系,只要这几个变量符合条件,所有记录都更新。
看看这几个变量和OrderList表的字段有没有关联吧,不然肯定全表更新
frank_20080215
frank_20080215   2015.06.21 11:35

update语句写入所有字段的新值

Csdn user default icon
上传中...
上传图片
插入图片