Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- declare @x table (tableID int not null primary key identity (1,1), datavalue varchar(10) null)
- INSERT INTO @x values ('one')
- INSERT INTO @x values ('aaaa')
- INSERT INTO @x values ('cccc')
- declare @y table (tableID int not null primary key , datavalue varchar(10) null)
- declare @count int ---------------FROM HERE, see comment
- set @count=5;
- WITH hier(cnt) AS
- (
- SELECT 1 AS cnt
- UNION ALL
- SELECT cnt + 1
- FROM hier
- WHERE cnt < @count
- ) -----------------------To HERE, see comment
- INSERT INTO @x
- (datavalue)
- OUTPUT INSERTED.tableID, INSERTED.datavalue
- INTO @y
- SELECT
- 'value='+CONVERT(varchar(5),h.cnt)
- FROM hier h
- ORDER BY cnt DESC
- select '@x',* from @x --table you just inserted into
- select '@y',* from @y --captured data, including identity
- tableID datavalue
- ---- ----------- ----------
- @x 1 one
- @x 2 aaaa
- @x 3 cccc
- @x 4 value=5
- @x 5 value=4
- @x 6 value=3
- @x 7 value=2
- @x 8 value=1
- (8 row(s) affected)
- tableID datavalue
- ---- ----------- ----------
- @y 4 value=5
- @y 5 value=4
- @y 6 value=3
- @y 7 value=2
- @y 8 value=1
Add Comment
Please, Sign In to add comment