I have a parent-child relationship in the database. What I need to do is loop through the parent's query, and using the parent's primary key, got get its children. The issue I am having is that I need to use a parameterized cursor (pass in the key) to do this.
Is there such a thing in SQL Server or a trick to mimic this? I tried doing this, but it didn't work:
DECLARE @value VARCHAR(20);
DECLARE @someKey NUMERIC(19,0);
DECLARE main_curs
CURSOR FOR SELECT value FROM someTable where key = @someKey;
SET @someKey = 12345;
OPEN main_curs
FETCH NEXT FROM main_curs INTO @value;
CLOSE main_curs
DEALLOCATE main_curs
But it seems that it doesn't pick up me setting the @someKey.
Any help on this would be greatly appreciated. Thanks!
UPDATE
I should include more information as I made the example seem too simple. I have multiple @someKey values that I need to use. As mentioned before, I have a parent-child relationship and I can have up to 6 children. So I am getting a list of parents and it's respective columns and iterating through it. While in the WHILE-LOOP, I wanted to get the primary key from the parent and call another cursor to get the child information (different columns returned). So I would do multiple calls to the child cursor with different @someKey values set. Hope that makes sense.