Guest User

Untitled

a guest
Jun 18th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. declare @x table (tableID int not null primary key identity (1,1), datavalue varchar(10) null)
  2. INSERT INTO @x values ('one')
  3. INSERT INTO @x values ('aaaa')
  4. INSERT INTO @x values ('cccc')
  5.  
  6. declare @y table (tableID int not null primary key , datavalue varchar(10) null)
  7.  
  8. declare @count int ---------------FROM HERE, see comment
  9. set @count=5;
  10. WITH hier(cnt) AS
  11. (
  12. SELECT 1 AS cnt
  13. UNION ALL
  14. SELECT cnt + 1
  15. FROM hier
  16. WHERE cnt < @count
  17. ) -----------------------To HERE, see comment
  18. INSERT INTO @x
  19. (datavalue)
  20. OUTPUT INSERTED.tableID, INSERTED.datavalue
  21. INTO @y
  22. SELECT
  23. 'value='+CONVERT(varchar(5),h.cnt)
  24. FROM hier h
  25. ORDER BY cnt DESC
  26.  
  27.  
  28. select '@x',* from @x --table you just inserted into
  29. select '@y',* from @y --captured data, including identity
  30.  
  31. tableID datavalue
  32. ---- ----------- ----------
  33. @x 1 one
  34. @x 2 aaaa
  35. @x 3 cccc
  36. @x 4 value=5
  37. @x 5 value=4
  38. @x 6 value=3
  39. @x 7 value=2
  40. @x 8 value=1
  41.  
  42. (8 row(s) affected)
  43.  
  44. tableID datavalue
  45. ---- ----------- ----------
  46. @y 4 value=5
  47. @y 5 value=4
  48. @y 6 value=3
  49. @y 7 value=2
  50. @y 8 value=1
Add Comment
Please, Sign In to add comment