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?

Was it helpful?

Solution

Not sure what is your end goal, but here are 2 approaches:

  1. Use cursor to loop through each id
  2. You need not to loop through, rather use inner join

    SELECT ChildId FROM CHILD inner join Consumer ON child.Consumerid = consumer.consumerid

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top