2

我正在使用以下查询使用 CTE(而不是循环)生成随机数。奇怪的是,在 Indexer 值为 2 之后查询不会生成随机数。您可以在此处查看演示。CTE的这种行为有什么解释吗?

   ;with LoopCounter
    as(
       select 1 Indexer, RAND() RandNumber
        union  all
        select Indexer + 1, RAND()  RandNumber
        from LoopCounter
        where 
          Indexer <= 1000
      )
    select *
    from LoopCounter  
    OPTION(MAXRECURSION 0)
4

1 回答 1

4
   ;with LoopCounter
    as(
        select 1 Indexer,  ABS(Cast(Cast(CRYPT_GEN_RANDOM(4) as INT) as Float)) / Cast(0x7FFFFFFF as int) RandNumber
        union  all
        select Indexer + 1,  ABS(Cast(Cast(CRYPT_GEN_RANDOM(4) as INT) as Float)) / Cast(0x7FFFFFFF as int)  RandNumber
        from LoopCounter
        where 
          Indexer <= 1000
      )
    select *
    from LoopCounter  
    OPTION(MAXRECURSION 0)
于 2013-07-01T21:46:50.527 回答