본문 바로가기
MSSQL

MSSQL] CURSOR VS WHILE

by Fastlane 2023. 3. 21.
728x90
반응형

CURSOR

DECLARE @database VARCHAR(50)
DECLARE db_cursor CURSOR FOR select name FROM sys.databases
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @database
WHILE @@FETCH_STATUS = 0
BEGIN
	PRINT @database
	FETCH NEXT FROM db_cursor INTO @database
END
CLOSE db_cursor
DEALLOCATE db_cursor

WHILE

with CTE AS (
	select name from sys.databases
)
select name into #query from CTE
DECLARE @name VARCHAR(50)
WHILE EXISTS (select top (1) name FROM #query)
BEGIN
	set @name = (select top(1) name from #query)
	print @name
	delete from #query where name = @name
END
DROP TABLE #query​

 

728x90
반응형

댓글