Advertisement
Guest User

Aredna #130 Easy

a guest
Jun 18th, 2013
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. declare @input nvarchar(9) = '100d100'
  2.  
  3. ;with parse as
  4. (
  5. select
  6. rolls = cast(left(@input,charindex('d',@input)-1) as int)
  7. ,die = cast(substring(@input,charindex('d',@input)+1,9) as int)
  8. )
  9. ,roll as
  10. (
  11. select
  12. n = 1
  13. ,roll = crypt_gen_random(2)%die+1
  14. from parse
  15.  
  16. union all
  17.  
  18. select n+1, crypt_gen_random(2)%die+1
  19. from roll, parse
  20. where n+1 < rolls
  21. )
  22. ,combine as
  23. (
  24. select
  25. row = 1
  26. ,result = cast(roll as nvarchar(max))
  27. from roll
  28. where n = 1
  29.  
  30. union all
  31.  
  32. select
  33. row+1
  34. ,cast(result + ' ' + cast(roll as nvarchar(max)) as nvarchar(max))
  35. from combine,roll
  36. where row = n
  37. )
  38. select result
  39. from combine
  40. where row = (select max(row) from combine)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement