我目前有一个存储过程,如果文件存在则更新文件,如果不存在则插入。下面的查询的问题是它似乎无论如何插入所有记录。
我似乎无法发现问题所在?
使用SQL Server 2012
ALTER PROC [dbo].[usp_UpdateInsertOptimise_new]
@Resid int = null,
@PostCode varchar(50),
@DriverID int,
@droporder int,
@X int,
@Y int,
@Calculate bit,
@uidref bigint,
@Description1 varchar(500),
@Description2 varchar(500),
@Description3 varchar(500),
@Uiddriver int,
@reservationsid int,
@Time varchar(50),
@TimeInPoint int,
@UniqueString varchar(50),
@DistanceBetweenTwoPoints float,
@Weight int,
@TimeBetweenPoints int,
@TimeFrom datetime,
@TimeTo datetime,
@TotalDistance float,
@Display varchar(150),
@Volume float,
@VehicleID int,
@Pallets int,
@AddressId int,
@CD nvarchar(10),
@ForkLiftRequired bit,
@HazardousRequired bit,
@FridgeRequired bit,
@TailLiftRequired bit,
@CurtinsideRequired bit,
@CDID varchar(50),
@CDOrder int,
@OriginalDate datetime,
@ArivalTime datetime,
@TimeForStage int,
@Optimise bit,
@ForVehicleGroupID int,
@DepotID int
AS
if EXISTS (SELECT * FROM optimise WITH (READUNCOMMITTED)
Where ReservationsID = @ResID)
BEGIN
UPDATE dbo.Optimise SET
PostCode = @PostCode,
[DriverID] = @DriverID,
[droporder] = @DropOrder,
[X] = @X,
[Y] = @Y,
[Calculate] = @Calculate,
[uidref] = @UIDRef,
[Description1] = @Description1,
[Description2] = @Description2,
[Description3] = @Description3,
[Uiddriver] = @UIDDriver,
[reservationsid] = @ReservationsID,
[Time] = @Time,
[TimeInPoint] = @TimeInPoint,
[UniqueString] = @UniqueString,
[DistanceBetweenTwoPoints] = @DistanceBetweenTwoPoints,
[Weight] = @Weight,
[TimeBetweenPoints] = @TimeBetweenPoints,
[TimeFrom] = @TimeFrom,
[TimeTo] = @TimeTo,
[TotalDistance] = @TotalDistance,
[Display] = @Display,
[Volume] = @Volume,
[VehicleID] = @VehicleID,
[Pallets] = @Pallets,
[AddressID] = @AddressID,
[CD] = @CD,
[ForkliftRequired] = @ForkLiftRequired,
[HazardousRequired] = @HazardousRequired,
[FridgeRequired] = @FridgeRequired,
[TailLiftRequired] = @TailLiftRequired,
[CurtinsideRequired] = @CurtinsideRequired,
[CDID] = @CDID,
[CDOrder] = @CDOrder,
[OriginalDate] = @OriginalDate,
[ArivalTime] = @ArivalTime,
[TimeForStage] = @TimeForStage,
[Optimise] = @Optimise,
[ForVehicleGroupID] = @ForVehicleGroupID,
[DepotID] = @DepotID
WHERE ReservationsID = @ResID
END
ELSE
IF NOT EXISTs(SELECT * FROM optimise WITH (READUNCOMMITTED)
Where ReservationsID = @ResID)
BEGIN
INSERT INTO [Optimise]
([PostCode],
[DriverID],
[droporder],
[X],
[Y],
[Calculate],
[uidref],
[Description1],
[Description2],
[Description3],
[Uiddriver],
[reservationsid],
[Time],
[TimeInPoint],
[UniqueString],
[DistanceBetweenTwoPoints],
[Weight],
[TimeBetweenPoints],
[TimeFrom],
[TimeTo],
[TotalDistance],
[Display],
[Volume],
[VehicleID],
[Pallets],
[AddressId],
[CD],
[ForkliftRequired],
[HazardousRequired],
[FridgeRequired],
[TailLiftRequired],
[CurtinsideRequired],
[CDID],
[CDOrder],
[OriginalDate],
[ArivalTime],
[TimeForStage],
[Optimise],
[ForVehicleGroupID],
[DepotID] )
VALUES (
@PostCode,
@DriverID,
@DropOrder,
@X,
@Y,
@Calculate,
@UIDRef,
@Description1,
@Description2,
@Description3,
@UIDDriver,
@ReservationsID,
@Time,
@TimeInPoint,
@UniqueString,
@DistanceBetweenTwoPoints,
@Weight,
@TimeBetweenPoints,
@TimeFrom,
@TimeTo,
@TotalDistance,
@Display,
@Volume,
@VehicleID,
@Pallets,
@AddressId,
@CD,
@ForkLiftRequired,
@HazardousRequired,
@FridgeRequired,
@TailLiftRequired,
@CurtinsideRequired,
@CDID,
@CDOrder,
@OriginalDate,
@ArivalTime,
@TimeForStage,
@Optimise,
@ForVehicleGroupID,
@DepotID)
END
GO
答案 0 :(得分:1)
我看不出有任何错误,所以如果这看起来很光顾那么对不起,回到基本要求:
您是否正在拨打正确的SP?我注意到上面的SP附加了_new usp_UpdateInsertOptimise_new
您是否传递了正确的ResID?它们是否存在,您是否在调用proc时跟踪它们。
简化proc以返回true / false,查看哪个触发,删除任何不必要的内容,只检查ID是否存在。尝试切换IF EXISTS为IF NOT EXISTS看看会发生什么。
答案 1 :(得分:0)
@ResID在顶部默认为null。不知道为什么,它因为它而无效!