Click here to Skip to main content
15,903,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi every one

I have a view consist of two Column F1 and F2.

I want to loop through this View from first record To last record and select for each record the value of F2 if it satisfy some condition, do some actions and continue to the next record, otherwise continue to next record too.

Can any one help me in looping through the View From the first record to the last one as I mentioned above
using SQL ?

Thanks
Posted
Updated 3-Mar-11 14:45pm
v3
Comments
Henry Minute 3-Mar-11 20:43pm    
You do not say which language you want to use to do this.

Do you want to do it in one of the .NET languages, or SQL or what? :)
Yasser El Shazly 3-Mar-11 20:44pm    
SQL
Albin Abel 3-Mar-11 20:59pm    
It is a general question. You can use the view just like a table in sql. So for the loop and inner conditions you need to learn how to write stored procedure. First try yourself and post here if you get any problem.

1 solution

This is untested - from memory so excuse any errors - but it should get you started.

In your stored procedure do something like this..


DECLARE cursorForView
    CURSOR LOCAL FAST FORWARD FOR
       SELECT F1, F2 from yourView

OPEN cursorForView

Declare @f1 as int
Declare @f2 as int

Fetch next from cursorForView into @F1, @F2

WHILE @@FETCH_STATUS = 0
BEGIN
  IF @F2 = somecondition
  BEGIN
    dosomething
  END


Fetch next from cursorForView into @F1, @F2

END

CLOSE cursorForView
DEALLOCATE cursorForView
 
Share this answer
 
Comments
Yasser El Shazly 4-Mar-11 7:34am    
when i try that this error is comen
Msg 155, Level 15, State 2, Procedure SP_add_crossrefernce71111, Line 21
'FAST' is not a recognized CURSOR option.
Msg 102, Level 15, State 1, Procedure SP_add_crossrefernce71111, Line 52
Incorrect syntax near 'cursorForView'.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900