Solved

CURSOR FOR versus While loop

Posted on 2011-09-19
5
347 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 4

Expert Comment

by:AnuTiji
ID: 36565039
0
 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

732 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