カーソルを使用してテーブルを反復処理できます。カーソルは、行の列の値を SQL 変数に割り当てることができます。次に、すべての行に対してフィルタリングを行い、結果で必要なものを作成します。
-- Create a cursor that iterates through the table and only reads the 'mass' column
DECLARE C1 CURSOR FOR
SELECT mass FROM masses
DECLARE @current decimal
OPEN C1
-- Copy the value from the 'mass' column to a vaiable
FETCH NEXT FROM C1 INTO @current
WHILE @@fetch_status = 0
BEGIN
-- Select the rows that have mass = current row mass + 21
SELECT * -- This ca be improved by selecting only the rows you need
FROM masses
WHERE mass = @current + 21
FETCH NEXT FROM C1 INTO @current
END
CLOSE C1
DEALLOCATE C1
ネストされた選択では、次の行を変更するなどして、すべての行を宛先テーブルにコピーできます。
INSERT INTO DESTINATION_TABLE(row1, row2, ..., rown)
SELECT (row1, row2, ..., rown)
FROM masses
WHERE mass = @current + 21