How can one iterate over stored procedure results from within another stored procedure…without cursors?

前端 未结 7 1821
半阙折子戏
半阙折子戏 2020-12-19 07:06

I\'m not sure if this is something I should do in T-SQL or not, and I\'m pretty sure using the word \'iterate\' was wrong in this context, since you should never iterate any

相关标签:
7条回答
  • 2020-12-19 07:48

    If you upgrade to SQL 2008 then you can pass table parameters I believe. Otherwise, you're stuck with a global temporary table or creating a permanent table that includes a column for some sort of process ID to identify which call to the stored procedure is relevant.

    How much room do you have in changing the stored procedure that generates the IDs? You could add code in there to handle it or have a parameter that lets you optionally flag the rows when it is called.

    0 讨论(0)
  • 2020-12-19 07:54

    Use temporary tables or a table variable (you are using SS2005).

    Although, that's not nest-able - if a stored proc uses that method then you can't dumpt that output into a temp table.

    0 讨论(0)
  • 2020-12-19 07:54

    An ugly solution would be to have your procedure return the "next" id each time it is called by using the other table (or some flag on the existing table) to filter out the rows that it has already returned

    0 讨论(0)
  • 2020-12-19 08:03

    You could also change your stored proc to a user-defined function that returns a table with your uniqueidentifiers. You can joing directly to the UDF and treat it like a table which avoids having to create the extra temp table explicitly. Also, you can pass parameters into the function as you're calling it, making this a very flexible solution.

    CREATE FUNCTION dbo.udfGetUniqueIDs
    ()
    RETURNS TABLE 
    AS
    RETURN 
    (
        SELECT uniqueid FROM dbo.SomeWhere
    )
    
    GO
    
    UPDATE dbo.TargetTable 
    SET a.FlagColumn = 1
    FROM dbo.TargetTable a INNER JOIN dbo.udfGetUniqueIDs() b 
        ON a.uniqueid = b.uniqueid
    

    Edit: This will work on SQL Server 2000 and up...

    0 讨论(0)
  • 2020-12-19 08:05

    This may not be the most efficient, but I would create a temp table to hold the results of the stored proc and then use that in a join against the target table. For example:

    CREATE TABLE #t (uniqueid int)
    INSERT INTO #t EXEC p_YourStoredProc
    
    UPDATE TargetTable 
    SET a.FlagColumn = 1
    FROM TargetTable a JOIN #t b 
        ON a.uniqueid = b.uniqueid
    
    DROP TABLE #t
    
    0 讨论(0)
  • 2020-12-19 08:11

    Insert the results of the stored proc into a temporary table and join this to the table you want to update:

    INSERT INTO #WorkTable
    EXEC usp_WorkResults
    
    UPDATE DataTable
      SET Flag = Whatever
    FROM DataTable
    INNER JOIN #WorkTable
      ON DataTable.Ket = #WorkTable.Key
    
    0 讨论(0)
提交回复
热议问题