我正在尝试将我的 SQL 语句保存在 SQL Server 上,而不是将其输入到我的 C# 代码中。我的问题是我应该将此 SQL 语句保存为什么?我试图将其保存为表值函数和存储过程,但没有这样做。有没有其他方法可以在 SQL Server 中保存它?
这是我的声明:
DECLARE @PickupDate datetime
DECLARE @SendingID int
DECLARE @PickupCountryID int, @PickupPostalID int, @PickupStreetID int, @PickupAdrID int;
DECLARE @DeliveryCountryID int, @DeliveryPostalID int, @DeliveryStreetID int, @DeliveryAdrID int, @PickupType varchar(2)
DECLARE @RouteID int, @RoutePartNo int
DECLARE @weekday varchar(50)
DECLARE caclulate_pickup_route CURSOR FOR
SELECT
SendingID,
PickupPostal.CountryID,
PickupStreet.PostalID,
PickupAdr.StreetID,
PickupAdrID,
PickupDate,
PickupType,
DeliveryPostal.CountryID,
DeliveryStreet.PostalID,
DeliveryAdr.StreetID,
DeliveryAdrID
FROM Sending
INNER JOIN Adr AS PickupAdr ON PickupAdrID=PickupAdr.AdrID
INNER JOIN Street AS PickupStreet ON PickupAdr.StreetID=PickupStreet.StreetID
INNER JOIN Postal AS PickupPostal ON PickupStreet.PostalID=PickupPostal.PostalID
INNER JOIN Adr AS DeliveryAdr ON DeliveryAdrID=DeliveryAdr.AdrID
INNER JOIN Street AS DeliveryStreet ON DeliveryAdr.StreetID=DeliveryStreet.StreetID
INNER JOIN Postal AS DeliveryPostal ON DeliveryStreet.PostalID=DeliveryPostal.PostalID
WHERE PickupRouteID=0 --and DeliveryDate = '2013-07-31' and DeliveryType = 'N'
OPEN caclulate_pickup_route
FETCH NEXT FROM caclulate_pickup_route INTO @SendingID, @PickupCountryID,@PickupPostalID, @PickupStreetID, @PickupAdrID, @PickupDate, @PickupType,
@DeliveryCountryID, @DeliveryPostalID, @DeliveryStreetID, @DeliveryAdrID
WHILE @@FETCH_STATUS = 0
BEGIN
SET @weekday = DATENAME(weekday, dateadd(day,-1, @PickupDate))
SELECT @RouteID = RouteID, @RoutepartNo = RoutePartNo FROM RoutePartPart AS RoutePartPart WHERE
PickupAreaGrpID IN (SELECT AreaGrpID AS PickupAreaGrpID FROM AreaGrpSpec WHERE
(CountryID=@PickupCountryID OR PostalID=@PickupPostalID OR (PostalID IN
(SELECT PostalID FROM Postal WHERE
Postal<=(SELECT Postal FROM Postal WHERE PostalID=@PickupPostalID) AND
CountryID=(SELECT CountryID FROM Postal WHERE PostalID=@PickupPostalID)) AND PostalIDto IN
(SELECT PostalID FROM Postal WHERE Postal>=(SELECT Postal FROM Postal WHERE
PostalID=@PickupPostalID) AND CountryID=(SELECT CountryID FROM Postal WHERE
PostalID=@PickupPostalID))) OR StreetID=@PickupStreetID OR AdrID=@PickupAdrID) AND DenyType=0) AND
PickupAreaGrpID NOT IN
(SELECT AreaGrpID AS PickupAreaGrpID FROM AreaGrpSpec WHERE
(CountryID=@PickupCountryID OR PostalID=@PickupPostalID OR (PostalID IN
(SELECT PostalID FROM Postal WHERE Postal<=(SELECT Postal FROM Postal WHERE PostalID=@PickupPostalID) AND
CountryID=(SELECT CountryID FROM Postal WHERE PostalID=@PickupPostalID)) AND PostalIDto IN
(SELECT PostalID FROM Postal WHERE Postal>=(SELECT Postal FROM Postal WHERE PostalID=@PickupPostalID) AND
CountryID=(SELECT CountryID FROM Postal WHERE PostalID=@PickupPostalID))) OR StreetID=@PickupStreetID OR AdrID=@PickupAdrID) AND DenyType=1) AND
DeliveryAreaGrpID IN
(SELECT AreaGrpID AS DeliveryAreaGrpID FROM AreaGrpSpec WHERE
(CountryID=@DeliveryCountryID OR PostalID=@DeliveryPostalID OR (PostalID IN
(SELECT PostalID FROM Postal WHERE Postal<=(SELECT Postal FROM Postal WHERE PostalID=@DeliveryPostalID) AND
CountryID=(SELECT CountryID FROM Postal WHERE PostalID=@DeliveryPostalID)) AND PostalIDto IN
(SELECT PostalID FROM Postal WHERE Postal>=(SELECT Postal FROM Postal WHERE PostalID=@DeliveryPostalID) AND
CountryID=(SELECT CountryID FROM Postal WHERE PostalID=@DeliveryPostalID))) OR StreetID=@DeliveryStreetID OR AdrID=@DeliveryAdrID) AND DenyType=0) AND
DeliveryAreaGrpID NOT IN
(SELECT AreaGrpID AS DeliveryAreaGrpID FROM AreaGrpSpec WHERE
(CountryID=@DeliveryCountryID OR PostalID=@DeliveryPostalID OR (PostalID IN (SELECT PostalID FROM Postal WHERE
Postal<=(SELECT Postal FROM Postal WHERE PostalID=@DeliveryPostalID) AND CountryID=(SELECT CountryID FROM Postal WHERE
PostalID=@DeliveryPostalID)) AND PostalIDto IN (SELECT PostalID FROM Postal WHERE Postal>=(SELECT Postal FROM Postal WHERE
PostalID=@DeliveryPostalID) AND CountryID=(SELECT CountryID FROM Postal WHERE
PostalID=@DeliveryPostalID))) OR StreetID=@DeliveryStreetID OR AdrID=@DeliveryAdrID) AND DenyType=1) AND
Pickup=1 AND(
(@weekday = 'Monday' AND [Monday] = 1)
OR (@weekday = 'Tuesday' AND [Tuesday] = 1)
OR (@weekday = 'Wednesday' AND [Wednesday] = 1)
OR (@weekday = 'Thursday' AND [Thursday] = 1)
OR (@weekday = 'Friday' AND [Friday] = 1)
OR (@weekday = 'Saturday' AND [Saturday] = 1)
OR (@weekday = 'Sunday' AND [Sunday] = 1))AND
Types LIKE '%'+@PickupType+'%'
Update Sending SET PickupRouteID=@RouteID, PickupRoutePartNo=@RoutePartNo WHERE SendingID=@SendingID
FETCH NEXT FROM caclulate_pickup_route INTO @SendingID, @PickupCountryID,@PickupPostalID, @PickupStreetID, @PickupAdrID, @PickupDate, @PickupType,
@DeliveryCountryID, @DeliveryPostalID, @DeliveryStreetID, @DeliveryAdrID
END
CLOSE caclulate_pickup_route
DEALLOCATE caclulate_pickup_route
谢谢!