0
0

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で固定行を動的に生成する(再帰With句)

0
Last updated at Posted at 2017-03-07

昔調べた再帰Withのブログページがどこだったのかわからなくなったので解説をメモ

SQLで固定行を実現する場合、下記のように再帰With句で実現できる

SQLで固定行(サンプル)

declare @count int = 100;

--再帰With句(with ~ as の間が再帰式)
with ROW_IDX(idx) as(
select 1 --ここは再帰されない普通のSQL
union all --Union allから上が非再帰項、下が再帰項となる
select idx+1
from ROW_IDX
where idx+1 <= @Count --この条件の取得件数がが0になるまで再帰する
)
--再帰With句を呼び出し
select ROW_IDX.idx
from ROW_IDX
order by ROW_IDX.idx

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?