创建存储过程

时间:2013-04-25 15:27:10

标签: sql sql-server-2008 tsql stored-procedures

我正在尝试创建一个存储过程,允许我更新TripStop表中的ActualArrivalDate和ActualDepartureDate,其中TripStopOrder表中的StopTypeCode = Drop。

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'

找到此记录后,我需要获取TripStopId并将其传递给Update语句。 不确定如何...我可以使用临时表然后运行另一个Select语句来获取TripStopID吗?

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

End
Commit

任何想法或建议将不胜感激。 〜新手〜

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:2)

只需在UPDATE语句中更改WHERE子句,而不是执行SELECT,然后执行UPDATE:

WHERE TripStopID = (SELECT T.TripStopID FROM TripStopOrder O
INNER JOIN TripStop T ON O.TripStopID = T.TripStopID
WHERE OrderID = @OrderID AND StopTypeCode = 'Drop')