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. how to loop over results of a select statement ?

how to loop over results of a select statement ?

Scheduled Pinned Locked Moved Database
databasehelptutorialquestion
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.
  • Y Offline
    Y Offline
    yousefshokati
    wrote on last edited by
    #1

    I have a select query that returns a some rows . i want to send each of the rows in returned result to a function but i do not know how ? can you help me "Note that i do not want to use Cursor "

    select PersonelCode , ...
    from PersonnelTable


    and the function Need the PersonelCode.For each row this function should be executed

    Create Function MyFunction ( @PersonelCode )
    begin
    body of the function
    end

    G J 2 Replies Last reply
    0
    • Y yousefshokati

      I have a select query that returns a some rows . i want to send each of the rows in returned result to a function but i do not know how ? can you help me "Note that i do not want to use Cursor "

      select PersonelCode , ...
      from PersonnelTable


      and the function Need the PersonelCode.For each row this function should be executed

      Create Function MyFunction ( @PersonelCode )
      begin
      body of the function
      end

      G Offline
      G Offline
      gvprabu
      wrote on last edited by
      #2

      Hi, Check the following Script, It will be use full for ur Query..........

      SET NOCOUNT ON
      -- Declaration
      DECLARE @Employee TABLE(ID INT , EmpName VARCHAR(40))
      DECLARE @I INT, @Count INT, @Temp VARCHAR(50)

      SET @I = 1
      -- Data
      INSERT INTO @Employee(ID,EmpName)
      SELECT 123,'Prabu'
      UNION ALL SELECT 234, 'Raja'
      UNION ALL SELECT 236, 'Kartik'
      UNION ALL SELECT 1234, 'Venkat'

      -- TSQL Script
      SELECT @Count=COUNT(*) FROM @Employee
      PRINT '~'
      PRINT 'Employee Details'
      PRINT '
      ~'
      WHILE @I <= @Count
      BEGIN
      WITH Emp AS
      (
      SELECT row_number() OVER ( ORDER BY ID ) 'RowNum', ID, EmpName
      FROM @Employee
      )
      SELECT @Temp =CAST(ID AS VARCHAR) +' - '+EmpName
      FROM Emp
      WHERE RowNum =@I
      PRINT @Temp
      SET @I=@I+1
      END
      PRINT '~~~~~~~~~~~~~~~~~'
      SET NOCOUNT OFF

      1 Reply Last reply
      0
      • Y yousefshokati

        I have a select query that returns a some rows . i want to send each of the rows in returned result to a function but i do not know how ? can you help me "Note that i do not want to use Cursor "

        select PersonelCode , ...
        from PersonnelTable


        and the function Need the PersonelCode.For each row this function should be executed

        Create Function MyFunction ( @PersonelCode )
        begin
        body of the function
        end

        J Offline
        J Offline
        jschell
        wrote on last edited by
        #3

        And the following doesn't do it? select PersonelCode , MyFunction(PersonelCode), ... from PersonnelTable

        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