Solved

CURSOR FOR versus While loop

Posted on 2011-09-19
5
349 Views
Last Modified: 2012-05-12
Hi all
I have the table below MyTable
Given that I need to select the first row using <<select Top 1 from my table>>
How can I process the whole rows with 1 transaction.?

That is, select P_Id  1 row (Do some processing )
when done with processign P_Id 1 row
The select P_Id 2 row (Do some Processing ) and so on..

I need to accomplished this with a single storeprocedure transaction..
an Example code or tutorial will be nice

Thanks in Advance
P_Id    LastName     Address        validFrom  		Valid until             status
1	Trinity	   26Broad	 01.05.2010		29.06.2010		1
2	Mike	   20Broad	 01.01.2011		29.02.2011		1
3	James	   11Broad	 01.03.2011		29.04.2011		0
4	Phili	   13Broad	 01.05.2011		29.06.2011		0
5	Obama	   16Broad	 01.07.2011		29.08.2011		1
6	Clinton	   11Broad	 01.03.2010		29.04.2010		0
7	Janet	  26Broad	 01.01.2010		29.02.2010		1

Open in new window

0
Comment
Question by:ZURINET
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 9

Accepted Solution

by:
mimran18 earned 500 total points
ID: 36565075
Hi
   try this

Drop table test
Go
Create table test
(ID int,
[SName] nvarchar(50),
[Marks] int)
Go
Insert  into test values (1,'John',0)
Insert  into test values (2,'Robert',0)
Insert  into test values (3,'William',0)
GO
Drop PROCEDURE CursorSprocDemo
GO
CREATE PROCEDURE CursorSprocDemo
AS
		
	DECLARE @ID int
	DECLARE @SName nvarchar(50)

	DECLARE DemoCursor CURSOR FOR 
	SELECT ID, [SName] FROM test  Order By [ID]

	OPEN DemoCursor

	FETCH NEXT FROM DemoCursor INTO @ID, @SName

	WHILE @@FETCH_STATUS = 0
	BEGIN
		UPDATE test SET [Sname] =  [Sname] +  ' - ' + Convert(nvarchar(2),@ID)   ,[Marks]=30+@ID  where ID = @ID

		FETCH NEXT FROM DemoCursor INTO  @ID, @SName
	END
    Select * from test

CLOSE DemoCursor
DEALLOCATE DemoCursor
GO
CursorSprocDemo

Open in new window

0
 
LVL 18

Expert Comment

by:ShogunWade
ID: 36565308
It is much better to use set based operations if you can rather than itterative methods.   Perhaphs if you tell us what processing you need to do per row we can help you with a set based operation to do it across all rows.
0
 
LVL 21

Expert Comment

by:Alpesh Patel
ID: 36566580
Cursor is slow and consume memory than while loop . so it's good to use while loop.
0
 

Author Comment

by:ZURINET
ID: 36567794
Hi ShogunWade

Thanks for the feeback..
I just need to extract the value in each row and store it in another table..

i.e in Java

for (row i = 0; i < table.row; i++) {
    insert into  mynewTable  = table[row1];
}

Thanks in Advance
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question