DEFINITIONS

Definitions More Info.
Definition ID761
TitleSQL
CategoryNOTES
DefinitionIf-Else Condition inside cursor
Definition Descriptionhttp://stackoverflow.com/questions/4782813/if-else-condition-inside-cursor

DECLARE @FetchColumn varchar(10)

If @Parameter1 is NULL

BEGIN
DECLARE YourCursor CURSOR FOR
SELECT
Column1
FROM YourTable
WHERE ...
FOR READ ONLY
END
ELSE
BEGIN
DECLARE YourCursor CURSOR FOR
SELECT
ColumnB
FROM YourTable
WHERE ...
FOR READ ONLY
END
--populate and allocate resources to the cursor
OPEN YourCursor

--process each row
WHILE 1=1
BEGIN

FETCH NEXT FROM YourCursor
INTO @FetchColumn

--finished fetching all rows?
IF @@FETCH_STATUS <> 0
BEGIN --YES, all done fetching
--exith the loop
BREAK
END --IF finished fetching

--do something here--
--do something here--
PRINT @FetchColumn

END --WHILE

--close and free the cursor's resources
CLOSE YourCursor
DEALLOCATE YourCursor
RecordBycunay
Record Date31-10-2016 14:31:02
Düzenle
Kopyala
Sil