What is an alternative to cursors for sql loop?

Using SQL 2005/2008

I need to use a forward pointer, but I do not want to work badly. Is there a faster way that I can contact without using cursors?

+5
source share
8 answers

You can do a loop WHILE, however, you should strive to perform a more set-based operation, since everything that is iterative in SQL is prone to performance issues.

http://msdn.microsoft.com/en-us/library/ms178642.aspx

+4
source

, . , ?

, .

, .

, SQL Server , , .

CURSOR:

, , , , , , .

, CURSOR.

+2

, " SQL". , , , , , .

- .

, , (-Temp) , STATIC, SELECT Temp , , . STATIC , , , , WHILE Temp ( ).

+1

, , .

+1

Common Table , @Neil. Adventureworks:

WITH cte_PO AS 
(
SELECT [LineTotal]
  ,[ModifiedDate]
FROM [AdventureWorks].[Purchasing].[PurchaseOrderDetail]
),
minmax AS
(
    SELECT MIN([LineTotal]) as DayMin
        ,MAX([LineTotal]) as DayMax
        ,[ModifiedDate]
    FROM cte_PO
    GROUP BY [ModifiedDate]
)
SELECT * FROM minmax ORDER BY ModifiedDate

, :

DayMin     DayMax     ModifiedDate
135.36     8847.30    2001-05-24 00:00:00.000
129.8115   25334.925  2001-06-07 00:00:00.000
+1

, . ... ! , , - .

+1

, . cusrors, , .

0
source

All Articles