简体   繁体   English

用于选择随机ID的功能

[英]Function to select random Id

I want to select a random id where my column Guy_Checked is not 1. 我想选择一个随机ID,我的列Guy_Checked不是1。

Here's my code: 这是我的代码:

CREATE FUNCTION dbo.get_rand_id()  
RETURNS int   
AS   
BEGIN  
    DECLARE @ret int;  
    set @ret = (select top 1 Guy_ID
    FROM SantaGuys
    WHERE Guy_Checked <> 1
    ORDER BY RAND())
RETURN @ret;  
END; 

SQL Server returns error 443 with something like... SQL Server返回错误443,如...

"Invalid use of the "rand" operator, which has side effects, in a function." 

What am I doing wrong? 我究竟做错了什么?

Use NEWID() instead: 使用NEWID()代替:

CREATE FUNCTION dbo.get_rand_id()  
RETURNS int   
AS   
BEGIN  
    DECLARE @ret int;  
    SET @ret = (SELECT top 1 Guy_ID
                FROM SantaGuys
                WHERE Guy_Checked <> 1
                ORDER BY NEWID()
               )
    RETURN @ret;  
END; 

RAND() is really a constant through the course of the query -- it is evaluated once before the query starts processing. 在查询过程中, RAND()实际上是一个常量 - 它在查询开始处理之前被评估一次。 NEWID() generates a different id each time it is called. NEWID()每次调用时都会生成一个不同的id。

you can use newid() check this it will create a unique value of type uniqueidentifier. 你可以使用newid() 检查它,它将创建uniqueidentifier类型的唯一值。

CREATE FUNCTION dbo.get_rand_id()  
RETURNS int   
AS   
BEGIN  
    DECLARE @ret int;  
    set @ret = (select top 1 Guy_ID
    FROM SantaGuys
    WHERE Guy_Checked <> 1
    ORDER BY NEWID())
RETURN @ret;  
END; 

I offer this answer which I adapted from this post ... 我提供了这个答案,我从这篇文章中改编了......

-- Build the table
SELECT 123 as Guy_ID
      ,0 as Guy_Checked INTO SantaGuys;

INSERT INTO SantaGuys VALUES (234, 1);
INSERT INTO SantaGuys VALUES (456, 0);
INSERT INTO SantaGuys VALUES (567, 1);

GO


-- Create a view of RAND() to work around the Invalid use of side-effecting error
CREATE VIEW v_get_rand_id
AS
SELECT RAND() as rand_id;
GO


-- Build the function with parameters that will be in your SELECT query
CREATE FUNCTION dbo.get_rand_id(@my_Guy_ID as int, @my_Guy_Checked as int)  
RETURNS float    
AS   
BEGIN  
 DECLARE @my_rand_id float; 

 SET @my_rand_id = (SELECT CASE WHEN @my_Guy_Checked <> 1 
                                THEN v.rand_id 
                                ELSE 0 END as my_rand_id 
                      FROM v_get_rand_id v)

 RETURN @my_rand_id;  

END; 

GO

-- Run your query and enjoy the results
SELECT sg.Guy_ID
      ,sg.Guy_Checked
      ,dbo.get_rand_id(sg.Guy_ID, sg.Guy_Checked) as my_rand_id
  FROM SantaGuys sg;

Here is one result... 这是一个结果......

+--------+-------------+-----------------+
| Guy_ID | Guy_Checked |   my_rand_id    |
+--------+-------------+-----------------+
|    123 |           0 | 0.5537264103585 |
|    234 |           1 |               0 |
|    456 |           0 |  0.227823559345 |
|    567 |           1 |               0 |
+--------+-------------+-----------------+

Generate ASCII tables easily from this link . 通过此链接轻松生成ASCII表。 Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM