10

I need help with writing stored procedure that calls another stored procedure and passes values to it. So far this was done in C#, now I want to move it to stored procedure and make an SQL agent job that calls it at specific time. Any ideas? This is the case.

Table A:

PK_TableA_ID

Table B:

PK_TableB_ID

Stored procedure SP1:

@TableA_ID
@TableB_ID

I need this but in T-SQL

foreach(var TableAId in TableA)
{
foreach(var TableBId in TableB)
{
//call stored procedure 
SP1(TableAId, TableBId);
}
}
3
  • 1
    Look into cursors Commented Mar 16, 2014 at 9:34
  • It's a stored procedure (not a storage procedure) - as in a procedure stored inside SQL Server .... Commented Mar 16, 2014 at 9:38
  • I am new in T-SQL. SO if somebody can give me an example according to the case that I mention, it would be great. Thank you. Commented Mar 16, 2014 at 9:45

2 Answers 2

13

Here's an example of how you can use cursors to do loops:

-- set up some test data
declare @table_a table (PK_TableA_ID int)
declare @table_b table (PK_TableB_ID int)
insert @table_a values (1),(2),(3)
insert @table_b values (4),(5),(6)    

-- do the actual processing
SET NOCOUNT ON

DECLARE @TableA_ID int, @TableB_ID int

DECLARE TableA_cursor CURSOR FOR SELECT PK_TableA_ID FROM @table_a

OPEN TableA_cursor
FETCH NEXT FROM TableA_cursor INTO @TableA_ID

WHILE @@FETCH_STATUS = 0
BEGIN
    DECLARE TableB_cursor CURSOR FOR SELECT PK_TableB_ID FROM @table_b

    OPEN TableB_cursor
    FETCH NEXT FROM TableB_cursor INTO @TableB_ID

    WHILE @@FETCH_STATUS = 0
    BEGIN
        PRINT CAST(@TableA_ID AS CHAR(1)) + ':' + CAST(@TableB_ID AS CHAR(1))
        -- execute your stored procedure here:
        -- EXEC Your_stored_procedure (@TableA_ID, @TableB_ID) 
        FETCH NEXT FROM TableB_cursor INTO @TableB_ID
        END

    CLOSE TableB_cursor
    DEALLOCATE TableB_cursor

    FETCH NEXT FROM TableA_cursor INTO @TableA_ID
END 
CLOSE TableA_cursor
DEALLOCATE TableA_cursor

The cursor above (with the test data in the temporary tables) will generate this output:

1:4
1:5
1:6
2:4
2:5
2:6
3:4
3:5
3:6

Using cursors might not be the best way to solve your problem though.

Sign up to request clarification or add additional context in comments.

Comments

1

I have a clean and clear option without cursors for this case using the table ids.

DECLARE 
@Counter1 INT,@MaxId1 INT,
@Counter2 INT, @MaxId2 INT 

SELECT @Counter1 = min(PK_TableA_ID) , @MaxId1 = max(PK_TableA_ID) 
FROM TableA
SELECT @Counter2 = min(PK_TableB_ID) , @MaxId2 = max(PK_TableB_ID) 
FROM TableB

WHILE(@Counter1 IS NOT NULL AND @Counter1 <= @MaxId1)
BEGIN 
      WHILE(@Counter2 IS NOT NULL AND @Counter2 <= @MaxId2)
      BEGIN
          //call stored procedure 
          SP1(@Counter1, @Counter2);

          SET @Counter2  = @Counter2  + 1
      END;
      
      SELECT @Counter2 = min(PK_TableB_ID) , @MaxId2 = max(PK_TableB_ID) 
      FROM TableB
      SET @Counter1  = @Counter1  + 1             
END;

1 Comment

This approach will only work fine when the numbers used for PK_TableA_ID and PK_TableB_ID are consecutive, and are numbers. This is not mentioned in the original question, so in case that columns are of type uniqueidentifier, the approach with the cursors will still work while this one does not. And in this code, re-selecting Counter2 and MaxId2 in the outer loop should not be required, at least if SP1 does not insert values into TableB.

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.