具有多个输入值的SQL SP

时间:2011-11-02 13:30:40

标签: sql-server sql-server-2005 stored-procedures

我从其他开发人员那里继承了db等,需要一些帮助。

我有以下存储过程:

CREATE PROCEDURE [dbo].[TESTgetSearchResults] 
    (
    @ids varchar(100),
    @Date DateTime = Null, 
    @Date2 DATETIME = Null,
    @Sort VARCHAR(5), /* ASC or DESC */
    @SortBy VARCHAR(10), /* Sorting criteria */
    @Location VARCHAR(40)
    )
AS
    SELECT @Date = GetDate()
    SELECT @Date2 = DATEADD(day,-14,GETDATE())    

BEGIN
    SELECT Aircraft.Id AS AircraftID, AircraftManufacturers.Name, AircraftModels.ModelName, 
    Aircraft.ModelSuffix, Aircraft.ImageFileName, Aircraft.Year, Aircraft.SerialNo, 
    Locations.DescriptionForSite, Aircraft.Description, Aircraft.Description2, 
    Aircraft.InfoWebAddress, Aircraft.ImageDescription, Advertisers.Id AS AdvertisersID, 
    Advertisers.Name AS AdvertisersName, Aircraft.AircraftDataId, Aircraft.ForSale, Aircraft.ForLease, 
    Aircraft.TTAF, Aircraft.ReSend, Aircraft.ReSendReason, Aircraft.Registration, Aircraft.AdType,
    Aircraft.HasAlternateImage, Aircraft.AlternateImageDescription, 
    Aircraft.Price, AircraftModels.AircraftType, Advertisers.CurrentEMagLink, Aircraft.CurrentEMagLink, 
    Aircraft.Email, Aircraft.IsSold, Aircraft.SoldDate, Aircraft.DateAdded, Aircraft.ExtendedDetails, 
    Aircraft.LastUpdateDate, Aircraft.ImageCount, Aircraft.ContactTelephone, AircraftModels.id, Advertisers.IsPremiumAdvertiser,
    Aircraft.Location, Advertisers.ContactTelephone As AdvertisersTelephone, Aircraft.EndDate, Aircraft.VideoLink,
    Aircraft.Contact, Advertisers.WASSalesEmail, Advertisers.WASSalesEmail2, Aircraft.PriceNumeric, 
    Aircraft.PriceQualifier, Aircraft.Currency, Aircraft.AircraftDescription
    FROM (((Aircraft 
    INNER JOIN Advertisers ON Aircraft.AdvertiserId = Advertisers.Id) 
    INNER JOIN AircraftModels ON Aircraft.AircraftModelId = AircraftModels.Id) 
    INNER JOIN AircraftManufacturers ON AircraftModels.ManufacturerId = AircraftManufacturers.Id) 
    INNER JOIN Locations ON Aircraft.LocationId = Locations.Id
    JOIN iter$simple_intlist_to_tbles(@ids) i ON AircraftModels.id = i.number
    WHERE (Aircraft.IsActive=1 AND Advertisers.IsActive=1 AND Aircraft.IsSold=0 AND (Aircraft.EndDate>=@Date OR Aircraft.EndDate Is Null) AND Locations.Id = @Location)
    OR (Aircraft.IsActive=1 AND Advertisers.IsActive=1 AND Aircraft.IsSold=1 AND Aircraft.SoldDate>=@Date2 AND Locations.Id = @Location)
    ORDER BY Advertisers.IsPremiumAdvertiser ASC, Aircraft.DateAdded DESC, Aircraft.ListPosition DESC, 
    Aircraft.LastUpdateDate, AircraftManufacturers.Name, AircraftModels.ModelName, Aircraft.ModelSuffix, 
    Aircraft.Id DESC
END

iter $ simple_intlist_to_tbles(@ids)simple从@ids输入构建一个表。这个输入的形式是一个由',',例如,1,,2,,3,等等分隔的id号字符串...

现在我需要将@Location替换为以相同方式格式化的位置ID字符串,例如,1,2,3,,4等....

所以我的问题是这个...我如何调整上面的sql /存储过程,以便基于单个位置过滤的两个'WHERE'子句现在能够获取多个位置ID ????? ?

任何帮助都会非常感激。

感谢。

2 个答案:

答案 0 :(得分:1)

要解决您的问题,只需从子查询中的iter $ simple_intlist_to_tbles(@Location)中检索值,然后使用IN检查它们:

AND Locations.Id IN (SELECT * FROM iter$simple_intlist_to_tbles(@Location))

你的where子句也需要更复杂。每个OR中都有相同的AND要求,因此您可以将它们移到OR之外。它简化为:

WHERE Aircraft.IsActive=1 
AND Advertisers.IsActive=1 
AND ((Aircraft.IsSold=0 AND (Aircraft.EndDate>=@Date OR Aircraft.EndDate Is Null))
  OR (Aircraft.IsSold=1 AND Aircraft.SoldDate>=@Date2))
AND Locations.Id IN (SELECT * FROM iter$simple_intlist_to_tbles(@Location))

答案 1 :(得分:0)

使用子查询,例如: FROM blah ... AND Locations.Id IN(SELECT number FROM iter $ simple_intlist_to_tbles(@locations))