Solved

CURSOR FOR versus While loop

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

760 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now