SQL Server行间交换数据问题

发布于 2024-09-01 01:00:57 字数 1261 浏览 6 评论 0原文

我向 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 个参数,并且正在交换为每个参数返回的所有行。

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

我希望有人能在这方面帮助我。

谢谢,

I was asking b4 about swapping query to swap data between rows in same table and I got that query

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

This query takes 2 parameters and is swapping all rows returned for each parameter.

The problem is the query swaps just if each parameter (foreign key) has values
I need to make swapping in case if one of them has no value.

I hope if some one can help me in that.

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

命比纸薄 2024-09-08 01:00:57

我得到了它

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

I got it

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文