Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. iterate resultset in sql procedure

iterate resultset in sql procedure

Scheduled Pinned Locked Moved Database
databasesql-serversysadminquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sujithkumarsl
    wrote on last edited by
    #1

    How can i iterate each rows for my result set in a sql server stored procedure. My intention is to call a stored procedure in different databases and combine the result. Like

    GetDatabases()
    Foreach database -- I am here now
    CallStoredProcedure()
    StoreTheResultInTempDB()
    Return the resultset

    My small attempt...

    M N 2 Replies Last reply
    0
    • S sujithkumarsl

      How can i iterate each rows for my result set in a sql server stored procedure. My intention is to call a stored procedure in different databases and combine the result. Like

      GetDatabases()
      Foreach database -- I am here now
      CallStoredProcedure()
      StoreTheResultInTempDB()
      Return the resultset

      My small attempt...

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      Look into cursors [^]- they are evil but necessary for this type of processing First hit looks promising Alternative use a While loop[^]

      1 Reply Last reply
      0
      • S sujithkumarsl

        How can i iterate each rows for my result set in a sql server stored procedure. My intention is to call a stored procedure in different databases and combine the result. Like

        GetDatabases()
        Foreach database -- I am here now
        CallStoredProcedure()
        StoreTheResultInTempDB()
        Return the resultset

        My small attempt...

        N Offline
        N Offline
        Niladri_Biswas
        wrote on last edited by
        #3

        Give a try with this... I made this based on my understanding

        Declare @GetDBNames sysname
        Declare @DynSql nvarchar(max)
        declare @result table ([Database_Name] nvarchar(128), [Stored Procedure Name] sysname)

        Declare DBNames cursor for
        Select '['+name+']' from master.dbo.sysdatabases

        open DBNames
        FETCH NEXT FROM DBNames into @GetDBNames

        WHILE @@FETCH_STATUS=0
        BEGIN

        SET @DynSql = '
        Select Specific_Catalog as Database_Name, Routine_Name as ''Stored Procedure Name''
        From '+ @GetDBNames+'.Information_Schema.Routines '

        insert @result exec sp_executesql @DynSql

        FETCH NEXT FROM DBNames into @GetDBNames
        END
        Close DBNames
        Deallocate DBNames
        select * from @result

        It gives me all the stored procs names pertaining to the databases. Hope this helps. :)

        Niladri Biswas

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups