아래와 같은 방법으로 CURSOR의 기능을 대체할 수 있다.


--Declare the Table variable 
DECLARE @Elements TABLE
(
    Number INT IDENTITY(1,1), --Auto incrementing Identity column
    ProductName VARCHAR(300) --The string value
)

--Decalre a variable to remember the position of the current delimiter
DECLARE @N INT 

--Decalre a variable to remember the number of rows in the table
DECLARE @Count INT

--Populate the TABLE variable using some logic
INSERT INTO @Elements SELECT Name FROM dbo.Products

--Initialize the looper variable
SET @N = 1

--Determine the number of rows in the Table
SELECT @Count=max(Number) from @Elements

--A variable to hold the currently selected value from the table
DECLARE @CurrentValue varchar(300);

--Loop through until all row processing is done
WHILE @N <= @Count

BEGIN
    --Load current value from the Table
    SELECT @CurrentValue = ProductName FROM @Elements WHERE Number = @N
    --Process the current value
    print @CurrentValue
    --Increment loop counter
    SET @N = @N + 1;
END

http://www.codeproject.com/Articles/34142/Understanding-Set-based-and-Procedural-approaches

'database > MSSQL' 카테고리의 다른 글

데이터 내보내기, 가져오기  (0) 2015.08.20
export schema  (0) 2014.10.21
데이터 타입과 크기  (0) 2014.08.01
동적쿼리  (0) 2014.07.16
메일 발송  (0) 2014.01.28
블로그 이미지

란마12

,