Thursday, December 17, 2009

SQL Cursors

In cases where you would like to loop through a records and based on certain conditions you would like to insert, updates or delete cursors can be used.

But having said it is not recomended from performance point of view. Here is the script in case you would like to use where we cannot avoid.

Declare hello Cursor LOCAL For

Select id from table

Open hello

Fetch Next From hello into @Id

While (@@Fetch_Status=0)
begin

-- here you can write the query to loop through id and put in your update conditions


Fetch Next From hello into @Id

end

Close hello

DeAllocate hello

2 comments:

  1. Don't you find it redundant to repeat "Fetch Next From hello into @Id" line twice? Can you refactor the repeating part?

    ReplyDelete
  2. get the online dbf repair utility and start the parsing of affected documents if you’d like to repair not openable files. It does not take a lot of time

    ReplyDelete