views:

162

answers:

1

Is the following possible:

EXEC sp_Edu3_DeleteTreeStructure (SELECT TreeStructureId FROM TreeStructures)

The SP normally takes one argument. What I want is that the SP is executed for each TreeStructureId found by the Query.

thx, Lieven Cardoen

+1  A: 

You can use a CURSOR to do this.

DECLARE @treeStructureId int

DECLARE TreeStructureCursor CURSOR FOR
    SELECT
     TreeStructureId
    FROM
     TreeStructures

OPEN TreeStructureCursor

FETCH NEXT FROM TreeStructureCursor
INTO @treeStructureId

WHILE @@FETCH_STATUS = 0
BEGIN
    EXEC sp_Edu3_DeleteTreeStructure(@treeStructureId)

    FETCH NEXT FROM TreeStructureCursor
    INTO @treeStructureId
END

CLOSE TreeStructureCursor
DEALLOCATE TreeStructureCursor
Robin Day
Thx, Robin, I knew I could do it with a cursor but I was wondering if it could be done without a cursor. (I don't think it can be done...)
Lieven Cardoen
You could change your SELECT statement to return XML containing all of the ID's and then modify your Stored Proc to accept this XML as it's parameter and work with 1-many id's. You're right though, a stored procedure cannot do as you ask. A function is closer, but will be unable to delete the data.
Robin Day