?
Solved

CURSOR FOR versus While loop

Posted on 2011-09-19
5
Medium Priority
?
355 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 2000 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

762 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