0

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...

2
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by bhavna_816
0

Not sure if this is what you are looking for:

DECLARE tempCursor CURSOR FOR SELECT col3 FROM table1 WHERE col2 = @col2
OPEN tempCursor
DECLARE @value As int --or whatever your datatype is

FETCH NEXT FROM tempCursor INTO @value

WHILE @@FETCH_STATUS = 0
BEGIN
	PRINT @value --Do whatever you need to do with the values here...
	FETCH NEXT FROM tempCursor INTO @value
END
CLOSE tempCursor
DEALLOCATE tempCursor

Hope this helps.

0

Not sure if this is what you are looking for:

DECLARE tempCursor CURSOR FOR SELECT col3 FROM table1 WHERE col2 = @col2
OPEN tempCursor
DECLARE @value As int --or whatever your datatype is

FETCH NEXT FROM tempCursor INTO @value

WHILE @@FETCH_STATUS = 0
BEGIN
	PRINT @value --Do whatever you need to do with the values here...
	FETCH NEXT FROM tempCursor INTO @value
END
CLOSE tempCursor
DEALLOCATE tempCursor

Hope this helps.

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.