StoreProcedure :: Cursor

-- ## Create New Table Name is "tbl_cursor"
CREATE TABLE tbl_cursor ( f1 varchar(10),f2 varchar(10) )


-- ## Insert data to tbl_cursor
INSERT INTO tbl_cursor(f1,f2) VALUES ('KEY1','DATA1')


-- ## Declare variables
DECLARE @variable1 VARCHAR(10)
DECLARE @variable2 VARCHAR(10)


-- ## Set data to variables
SET @variable1 = 'Hello'
SET @variable2 = ''


-- ## Declare cursor variables
DECLARE curs CURSOR
FOR SELECT f2 FROM tbl_cursor -- ## Select data
OPEN curs -- ## Open cursor variable


-- ## Select data to cursor variable
FETCH NEXT FROM curs INTO @variable2


-- ## While Loop Data All Record.
WHILE @@FETCH_STATUS=0
BEGIN
  -- ## IF Statement
  IF @variable1 = 'Hello'
      SET @variable2 = 'TRUE'
  ELSE -- ## Else Statement
      SET @variable2 = @variable2


  -- ## Display
  PRINT @variable1
  PRINT @variable2


  -- ## Select data to cursor variable
  FETCH NEXT FROM curs INTO @variable2
END
-- ## Close cursor variables
CLOSE curs
-- ## Destroy cursor variables
DEALLOCATE curs
GO

Comments

Popular posts from this blog

การตั้งเวลาระหว่าง Server และ Client

วิธีตั้งค่า NTP บน Primary Domain Controller

Installation and Run Node.JS on IIS