DECLARE @RecordCount int
DECLARE @RowCount int
DECLARE @Field1 varchar(100)
DECLARE @Field2 int
DECLARE @Field3 char(2)
select distinct IDENTITY(int,1,1) as ID, Field1, Field2, Field3into #loop from dbo.TableName order by ID DESC
SET @RecordCount = (select max(ID) from #loop)
SET @RowCount = 1
WHILE @RowCount <= @RecordCount
BEGIN
select @Field1 = Field1, @Field2 = Field2, @Field3 = Field3 from #loop where ID = @RowCount
EXECUTE [dbo].[usp_RandomCodeToReplace]
@Field1
,@Field2
,@Field3
SET @RowCount = @RowCount + 1
END
DROP TABLE #loop
I started keeping notes on things I used a lot, or that took care of a difficult problem. On looking for something I couldn't remember how to do, and spending 30 minutes going through notes and SSIS projects, I decided to create this blog. Lots of SQL, BI, Web, Winforms, Spotifre, R, Python and whatever I happen to be working in when I decide to post something I find interesting.
Friday, 7 December 2012
While Loop - Cursor Alternative
I don't like cursors much. They're simple to write, easy to implement and good starting point for understanding loops. However, they are slow and memory-thirsty...(is that the right phrase?).
So here is an example of using a simple WHILE loop which has the same behaviour as a cursor but it much faster and uses less memory than a cursor.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment