3

I am trying to create a stored procedure that will allow me to update the ActualArrivalDate and ActualDepartureDate in a TripStop table where StopTypeCode = Drop in the TripStopOrder table.

Create Proc spUpdateTable
@OrderID int, @ArrivalDate datetime, @DepartureDate datetime

As

Begin 

   Begin Transaction
   Select * From dbo.TripStopOrder
   Join dbo.TripStop ON dbo.TripStopOrder.TripStopID = dbo.TripStop.TripStopID
   Where OrderID = ''and StopTypeCode ='Drop'

Once I find this record I need to grab the TripStopId and pass it into the Update statement. Not sure how to this...can I use a temp table then run another Select statement to pick up the TripStopID?

   Update dbo.TripStop SET ArrivalDate='',DepartureDate=''
   Where TripStopID = ''

End
Commit

Any ideas or suggestions would be greatly appreciated. ~Newbie~

4

2 回答 2

4

您可以将值分配给变量,例如@TripStopId

DECLARE @TripStopId INT

Select @TripStopid = TripStopId
From dbo.TripStopOrder
Join dbo.TripStop ON dbo.TripStopOrder.TripStopID = dbo.TripStop.TripStopID
Where OrderID = ''and StopTypeCode ='Drop'

然后你可以在你的UPDATE声明中使用它。

Update dbo.TripStop SET ArrivalDate='',DepartureDate=''
Where TripStopID = @TripStopId
于 2013-04-25T15:30:48.887 回答
2

不要执行 SELECT 然后执行 UPDATE,只需更改 UPDATE 语句中的 WHERE 子句:

WHERE TripStopID = (SELECT T.TripStopID FROM TripStopOrder O
INNER JOIN TripStop T ON O.TripStopID = T.TripStopID
WHERE OrderID = @OrderID AND StopTypeCode = 'Drop')
于 2013-04-25T15:32:32.137 回答