Solved

CURSOR FOR versus While loop

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
I have a large data set and a SSIS package. How can I load this file in multi threading?
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
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

943 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

6 Experts available now in Live!

Get 1:1 Help Now