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. looping in SQL Server2005

looping in SQL Server2005

Scheduled Pinned Locked Moved Database
questiondatabasetutorial
4 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
    salon
    wrote on last edited by
    #1

    I have a table having columns like values, like this col1 col2 col3 1000 10 2 1000 20 3 1000 30 4 2000 10 5 2000 20 6 2000 30 7 3000 10 8 3000 20 9 3000 30 10 I want to fetch records like if i enter col2 as 20 and col1 as 1500 then with "SELECT col3 FROM table1 WHERE col2 = @col2" I will get 3 rows having respective records of value 20 for col2(col2 values are fixed) but col1 values are variable i.e. can fall into different ranges like 1400,1500,2300 etc. but col2 values are fixed I want to return only one row for eg. col2 as 20 and col1 as 1500 then 1000 20 3 this row should get returned How can i do this? I don't know how to use cursors...

    N 1 Reply Last reply
    0
    • S salon

      I have a table having columns like values, like this col1 col2 col3 1000 10 2 1000 20 3 1000 30 4 2000 10 5 2000 20 6 2000 30 7 3000 10 8 3000 20 9 3000 30 10 I want to fetch records like if i enter col2 as 20 and col1 as 1500 then with "SELECT col3 FROM table1 WHERE col2 = @col2" I will get 3 rows having respective records of value 20 for col2(col2 values are fixed) but col1 values are variable i.e. can fall into different ranges like 1400,1500,2300 etc. but col2 values are fixed I want to return only one row for eg. col2 as 20 and col1 as 1500 then 1000 20 3 this row should get returned How can i do this? I don't know how to use cursors...

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      salon wrote:

      I don't know how to use cursors...

      Any problem for searching and learning how it works ?

      All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

      S 1 Reply Last reply
      0
      • N N a v a n e e t h

        salon wrote:

        I don't know how to use cursors...

        Any problem for searching and learning how it works ?

        All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

        S Offline
        S Offline
        salon
        wrote on last edited by
        #3

        No issues to learn... I am just asking is there anyone have idea to do it in optimized way? as I am trying col1 have variable inputs like 1200,1300,2100 etc... and col2 have fixed inputs I am doing like this calculating min and max values for col1 and trying to put col2 in that range but till I m not able to achieve...

        T 1 Reply Last reply
        0
        • S salon

          No issues to learn... I am just asking is there anyone have idea to do it in optimized way? as I am trying col1 have variable inputs like 1200,1300,2100 etc... and col2 have fixed inputs I am doing like this calculating min and max values for col1 and trying to put col2 in that range but till I m not able to achieve...

          T Offline
          T Offline
          Tobias Schoenig
          wrote on last edited by
          #4

          There's no problem in using a cursor within a while loop -> have a look at the MSDN, to review the example and you'll have your're problem solved.

          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