1

I am trying to write a stored proicedure that gets all the townID's from Region_TownPage table. Then i should get the City, Stateinitials of all the townID's.

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

I do not know how to use an array here in sql. If someone could help me doing this, i really appreciate that.

Thanks in advance!!

2 Answers 2

5

I don't think you need an array - you just need to join the tables?

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
Sign up to request clarification or add additional context in comments.

2 Comments

Assuming this question is his actual query I would agree this is the best approach. Otherwise the other answer of using a table might be preferable depending on complexity of query
agreed. this would be the way to do it unless you needed to keep the townIDs in memory for something in the SP later on but for your small example, a join would be the better solution.
4

you would declare a table variable instead of an int. so it would be something like

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.