我正在尝试编写一个存储的proicedure,它从Region_TownPage表获取所有的townID。然后我应该获得所有townID的City,Stateinitials。
Alter PROCEDURE [dbo].[GetTownDetailsforRegionID]
@RegionID int
AS
BEGIN
Declare @townID int
set @townID = (Select townID from Region_TownPage where regionID =@RegionID)
SET NOCOUNT ON;
Select City, StateInitials,TownID from TownPage where TownID =@townID
END
我不知道如何在sql中使用数组。如果有人可以帮我这样做,我真的很感激。
提前致谢!!
答案 0 :(得分:5)
我认为你不需要数组 - 你只需要加入表格吗?
Select r.RegionId,
t.TownId,
t.City,
t.StateInitials
From Region_TownPage r
Join TownPage t on r.TownId = t.TownId
Where r.RegionId = @RegionId
答案 1 :(得分:4)
你会声明一个表变量而不是一个int。所以它会像
DECLARE @tab table(townID int)
INSERT INTO @tab
SELECT townID from Region_TownPage WHERE regionID = @RegionID
Select * From TownPage WHERE TownID IN(SELECT townID FROM @tab)