User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the MS SQL section within the Web Development category of DaniWeb, a massive community of 373,933 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,179 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our MS SQL advertiser:
Views: 824 | Replies: 2 | Solved
Reply
Join Date: Sep 2006
Posts: 104
Reputation: bhavna_816 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
bhavna_816 bhavna_816 is offline Offline
Junior Poster

looping in SQL Server2005

  #1  
Mar 20th, 2008
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 in SQL SERVER 2005?

I don't know how to use cursors...
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2008
Posts: 68
Reputation: cmhampton is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 9
cmhampton's Avatar
cmhampton cmhampton is offline Offline
Junior Poster in Training

Re: looping in SQL Server2005

  #2  
Mar 24th, 2008
Not sure if this is what you are looking for:

  1. DECLARE tempCursor CURSOR FOR SELECT col3 FROM table1 WHERE col2 = @col2
  2. OPEN tempCursor
  3. DECLARE @value AS int --or whatever your datatype is
  4.  
  5. FETCH NEXT FROM tempCursor INTO @value
  6.  
  7. WHILE @@FETCH_STATUS = 0
  8. BEGIN
  9. PRINT @value --Do whatever you need to do with the values here...
  10. FETCH NEXT FROM tempCursor INTO @value
  11. END
  12. CLOSE tempCursor
  13. DEALLOCATE tempCursor

Hope this helps.
Reply With Quote  
Join Date: Sep 2006
Posts: 104
Reputation: bhavna_816 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
bhavna_816 bhavna_816 is offline Offline
Junior Poster

Re: looping in SQL Server2005

  #3  
Mar 25th, 2008
Originally Posted by cmhampton View Post
Not sure if this is what you are looking for:

  1. DECLARE tempCursor CURSOR FOR SELECT col3 FROM table1 WHERE col2 = @col2
  2. OPEN tempCursor
  3. DECLARE @value AS int --or whatever your datatype is
  4.  
  5. FETCH NEXT FROM tempCursor INTO @value
  6.  
  7. WHILE @@FETCH_STATUS = 0
  8. BEGIN
  9. PRINT @value --Do whatever you need to do with the values here...
  10. FETCH NEXT FROM tempCursor INTO @value
  11. END
  12. CLOSE tempCursor
  13. DEALLOCATE tempCursor

Hope this helps.


Thanks ....
I have done exactly same and my problem is solved...
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb MS SQL Marketplace
Thread Tools Display Modes

Other Threads in the MS SQL Forum

All times are GMT -4. The time now is 6:17 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC