使用表值参数进行查询
我需要编写存储过程的帮助,它需要一个表值参数@Locations,其类型定义如下:
CREATE TYPE [dbo].[tvpLocation] AS TABLE(
[CountryId] [int] NULL,
[ResortName] [nvarchar](100) NULL,
[Ordinal] [int] NOT NULL,
PRIMARY KEY CLUSTERED
(
[Ordinal] ASC
)WITH (IGNORE_DUP_KEY = OFF)
)
@Locations 将包含至少1 行。每行将有一个非空的 CountryId,并且可以有一个非空的 ResortName。 每行都有一个唯一的序数,第一个为 0。@Locations 中的 CountryId 和 ResortName 组合将是唯一的。
存储过程需要根据以下表结构进行搜索。
右键单击图像并查看图像或类似操作(具体取决于您的浏览器)可以更好地查看图像。
现在这就是我陷入困境的地方,存储过程应该能够在其中找到 Tours:
- The Tour 的第 1 个 TourHotel(序号 0) 具有相同的 CountryId(和 ResortName 如果指定)的第一行 @Locations(序数 0)。
- 并且如果 @Locations 有 > 1 行, 旅游必须有额外的 TourHotels,所有这些都必须位于 这些剩余 @Locations 行的剩余 CountryId(以及 ResortNames,如果指定)。
编辑 这是我根据 Anthony Faull 的建议最终使用的代码。非常感谢你,安东尼:
select distinct T.Id
from tblTour T
join tblTourHotel TH on TH.TourId = T.Id
join tblHotel H ON H.Id = TH.HotelId
JOIN @Locations L ON
(
(
L.Ordinal = 0
AND TH.Ordinal = 0
)
OR
(
L.Ordinal > 0
AND TH.Ordinal > 0
)
)
AND L.CountryId = H.CountryId
AND
(
L.ResortName = H.ResortName
OR L.ResortName IS NULL
)
cross apply( select COUNT(TH2.Id) AS [Count] FROM tblTourHotel TH2 where TH2.TourId = TH.TourId ) TourHotelCount
where
TourHotelCount.[Count] = @LocationCount
group by T.Id, T.TourRef, T.Description, T.DepartureDate, T.NumNights, T.DepartureAirportId, T.DestinationAirportId, T.AirlineId, T.FEPrice
having COUNT(distinct TH.Id) = @LocationCount
I need help please with writing a sproc, it takes a table-valued parameter @Locations, whose Type is defined as follows:
CREATE TYPE [dbo].[tvpLocation] AS TABLE(
[CountryId] [int] NULL,
[ResortName] [nvarchar](100) NULL,
[Ordinal] [int] NOT NULL,
PRIMARY KEY CLUSTERED
(
[Ordinal] ASC
)WITH (IGNORE_DUP_KEY = OFF)
)
@Locations will contain at least 1 row. Each row WILL have a non-null CountryId, and MAY have a non-null ResortName.
Each row will have a unique Ordinal, the first being 0. The combinations of CountryId and ResortName in @Locations will be unique.
The sproc needs to search against the following table structure.
The image can be seen better by right-clicking it and View Image, or similar depending on your browser.
Now this is where I'm stuck, the sproc should be able to find Tours where:
- The Tour's 1st TourHotel (Ordinal 0)
has the same CountryId (and ResortName
if specified) of the 1st row of
@Locations (Ordinal 0). - And also if @Locations has > 1 row,
the Tour must have additional
TourHotels, ALL of which must be in the
remaining CountryIds (and ResortNames if specified) of these remaining @Locations rows.
Edit This is the code I finally used, based on Anthony Faull's suggestion. Thank you so much Anthony:
select distinct T.Id
from tblTour T
join tblTourHotel TH on TH.TourId = T.Id
join tblHotel H ON H.Id = TH.HotelId
JOIN @Locations L ON
(
(
L.Ordinal = 0
AND TH.Ordinal = 0
)
OR
(
L.Ordinal > 0
AND TH.Ordinal > 0
)
)
AND L.CountryId = H.CountryId
AND
(
L.ResortName = H.ResortName
OR L.ResortName IS NULL
)
cross apply( select COUNT(TH2.Id) AS [Count] FROM tblTourHotel TH2 where TH2.TourId = TH.TourId ) TourHotelCount
where
TourHotelCount.[Count] = @LocationCount
group by T.Id, T.TourRef, T.Description, T.DepartureDate, T.NumNights, T.DepartureAirportId, T.DestinationAirportId, T.AirlineId, T.FEPrice
having COUNT(distinct TH.Id) = @LocationCount
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)