1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【SQL server】T-SQL 学習メモ / CURSOR

Posted at

T-SQLで使われているCURSORというものについて調べてみた。

CURSORとは

SELECTで取得したデータを1行ずつ取り出す事ができるという機能。それにより1行毎に処理を書く事ができる。

--カーソル宣言
DECLARE カーソル名 CURSOR FOR 
SELECT  FROM テーブル名 WHERE 条件

--カーソルOPEN
OPEN カーソル名
FETCH NEXT FROM カーソル名 INTO 

--FETCHで1行ずつ取り出す
WHILE @@FETCH STATUS = 0
BEGIN
--目的の処理
FETCH NEXT FROM カーソル名 INTO 
END

--カーソル解除
CLOSE カーソル名 
DEALLOCATE カーソル名
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?