SQL Server在行之间交换数据问题

时间:2010-05-09 12:50:28

标签: sql-server swap

我问b4有关交换查询以在同一个表中的行之间交换数据而我得到了该查询

ALTER PROCEDURE [dbo].[VehicleReservationsSwap]
    (@FirstVehicleID int, 
     @secondVehicleID int,
     @WhereClause nvarchar(2000))
AS
BEGIN
     Create Table #Temp (VehicleID int, 
                         VehicleType nvarchar(100),
                         JoinId int)

     DECLARE @SQL varchar(8000)

     SET @SQL = 'Insert into #Temp (VehicleID, VehicleType, JoinId) SELECT 
        VehicleID,VehicleType,CASE WHEN VehicleID = ' + Cast(@FirstVehicleID as varchar(10)) + ' then ' + Cast(@secondVehicleID as varchar(10)) + ' ELSE ' + Cast(@FirstVehicleID as varchar(10)) + ' END AS JoinId
        FROM Reservations
        WHERE VehicleID in ( ' + Cast(@FirstVehicleID as varchar(10)) + ' , ' + Cast(@secondVehicleID as varchar(10)) + ' )' + @WhereClause

     EXEC(@SQL)

    --swap values 
    UPDATE y 
    SET y.VehicleID = #Temp.VehicleID,
        y.VehicleType = #Temp.VehicleType
    FROM Reservations y 
    INNER JOIN #Temp ON y.VehicleID = #Temp.JoinId 
    WHERE y.VehicleID in (@FirstVehicleID, @SecondVehicleID) 

    Drop Table #Temp
END

此查询采用2个参数,并交换为每个参数返回的所有行。

问题是如果每个参数(外键)都有值,则查询交换 如果其中一个没有价值,我需要进行交换。

我希望有人可以帮助我。

谢谢,

1 个答案:

答案 0 :(得分:0)

我明白了

ALTER PROCEDURE [dbo].[VehicleReservationsSwap]
    -- Add the parameters for the stored procedure here
    (@FirstVehicleID int, 
    @secondVehicleID int,
    @WhereClause nvarchar(2000))
AS
BEGIN

Create Table #Temp
(
    VehicleID int 
    ,VehicleType nvarchar(100) 
    ,JoinId int
)


DECLARE @SQL varchar(8000)
SET @SQL ='Insert into #Temp (VehicleID,VehicleType,JoinId) SELECT 
        VehicleID,VehicleType,CASE WHEN VehicleID = ' + Cast(@FirstVehicleID as varchar(10)) + ' then ' + Cast(@secondVehicleID as varchar(10)) + ' ELSE ' + Cast(@FirstVehicleID as varchar(10)) + ' END AS JoinId
        FROM Reservations
        WHERE VehicleID in ( ' + Cast(@FirstVehicleID as varchar(10)) + ' , ' + Cast(@secondVehicleID as varchar(10)) + ' )' + @WhereClause
EXEC(@SQL)

--swap values 
declare @count1 int
declare @count2 int
set @count1 = (select COUNT(reservationid) as count1 from Reservations where VehicleID = @FirstVehicleID)
set @count2 = (select COUNT(reservationid) as count2 from Reservations where VehicleID = @secondVehicleID)

if @count1 > 0 and @count2 > 0
 begin
    UPDATE y 
    SET y.VehicleID = #Temp.VehicleID
        ,y.VehicleType = #Temp.VehicleType
    FROM Reservations     y 
        INNER JOIN #Temp  ON y.VehicleID = #Temp.JoinId 
    WHERE y.VehicleID in (@FirstVehicleID,@secondVehicleID)     
 end
 else if @count1 <= 0 and @count2 > 0
 begin
  UPDATE y 
    SET y.VehicleID = #Temp.JoinId
        ,y.VehicleType = #Temp.VehicleType
    FROM Reservations     y 
        INNER JOIN #Temp  ON y.VehicleID = #Temp.VehicleID 
    WHERE y.VehicleID = @secondVehicleID    
 end
 else if @count1 > 0 and @count2 <= 0
 begin
  UPDATE y 
    SET y.VehicleID = #Temp.JoinId
        ,y.VehicleType = #Temp.VehicleType
    FROM Reservations     y 
        INNER JOIN #Temp  ON y.VehicleID = #Temp.VehicleID 
    WHERE y.VehicleID = @FirstVehicleID     
 end

Drop Table #Temp


END