How do I generate random number for each row in a TSQL Select?

前端 未结 19 929
逝去的感伤
逝去的感伤 2020-11-22 07:03

I need a different random number for each row in my table. The following seemingly obvious code uses the same random value for each row.

SELECT table_name,          


        
19条回答
  •  囚心锁ツ
    2020-11-22 07:41

    It's as easy as:

    DECLARE @rv FLOAT;
    SELECT @rv = rand();
    

    And this will put a random number between 0-99 into a table:

    CREATE TABLE R
    (
        Number int
    )
    
    DECLARE @rv FLOAT;
    SELECT @rv = rand();
    
    INSERT INTO dbo.R
    (Number)
        values((@rv * 100));
    
    SELECT * FROM R
    

提交回复
热议问题