Not sure what is your end goal, but here are 2 approaches:
- Use cursor to loop through each id
You need not to loop through, rather use inner join
SELECT ChildId FROM CHILD inner join Consumer ON child.Consumerid = consumer.consumerid
Question
Here i have a table called consumer which has 23 rows. from this table i need to take the consumerid from this table and loop this consumerid and pass the value to another select query like a foreach loop in c#. I tried like this
declare @Id int
While (Select Count(*) From Consumer) > 0
Begin
Select Top 1 @Id = consumerid From Consumer
SELECT childid FROM CHILD where consumerid=@Id
End
any suggestion?
Solution
Not sure what is your end goal, but here are 2 approaches:
You need not to loop through, rather use inner join
SELECT ChildId FROM CHILD inner join Consumer ON child.Consumerid = consumer.consumerid