Posted by matt as Code, MS SQL, Transact-SQL
There are cases where using Cursor would do you more harm than good. It uses considerably large amount of resources in your server and not to mention that it can cause leaks if not used correctly (Eg. Open without corresponding Close). That’s why most T-SQL developer often suggest to avoid it whenever possible. Here is a sample code on how to travese a result set and get values of each row using a While Loop instead of a Cursor.
–identity INT (1,1) - surrogate PRIMARY KEY
declare @tempTable table(id int identity(1,1),employeeName varchar(25),employeePosition varchar(25))insert into @tempTable
select ‘John’,'Manager’insert into @tempTable
select ‘Joe’,'Vice President’insert into @tempTable
select ‘Mary’,'Secretary’declare @rowCount int
select @rowCount = count(*) from @tempTabledeclare @counter int
select @counter = 1while @counter <= @rowCount
beginselect * from @tempTable where id = @counter
select @counter = @counter + 1
end
Tags: database guide • mssql sample script
.Net asp .net tutorial asp .net tutorial c# sample code CSS sample script database guide java sample code javascript sample code mssql sample script string reverse in C# t-sql telerik sample code user-defined function vb .net sample code visual studio tutorial
WP Cumulus Flash tag cloud by Roy Tanck requires Flash Player 9 or better.
RSS feed for comments on this post · TrackBack URI
Leave a reply