Question

I am doing a project using sql cursors, for one cursor i want to loop through a dataset of Company ids.

How would you use logic such as

  1. Tables without CompanyID need all data copying.
  2. Always copy data where CompanyID = 0

How would you perform this is a while loop of a cursor.

I have been looking at this tutorial

http://examplesql.com/sqlserver-transact-t-sql/how-to-loop-using-sql-server-cursor-fetch-fast_forward/

Was it helpful?

Solution

For one it's always recommended in all cases to absolutely avoid the use of cursors. They are known to experience very poor performance issues and 95% of the time it's possible to perform the necessary function with set-based logic.

To solve this particular problem using psuedo would look something like this:

INSERT INTO DestinationTable
SELECT CompanyID, SomeDataColumn
FROM SourceTable
WHERE ISNULL(CompanyID, 0) = 0
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top