SQL PLSQL 生成随机整数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30417788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 03:40:22  来源:igfitidea点击:

PLSQL generate random integer

sqloracleplsqloracle11goracle-sqldeveloper

提问by Justin

In Oracle Sql developer 11g, how do I generate a random integer and assign it to a variable? This is what I've tried so far:

在 Oracle Sql developer 11g 中,如何生成随机整数并将其分配给变量?这是我迄今为止尝试过的:

S_TB := SELECT dbms_random.value(1,10) num FROM dual;

With this code I got error:

使用此代码我收到错误:

S_TB := SELECT dbms_random.value(1,10) num FROM dual
Error report -
Unknown Command

What is the proper way to solve my issue?

解决我的问题的正确方法是什么?

回答by APC

Variables require PL/SQL; it's not clear from your question whether your code is a proper PL/SQL block. In PL/SQL variables are populated from queries using the INTO syntax rather than the assignment syntax you're using.

变量需要PL/SQL;从您的问题中不清楚您的代码是否是正确的 PL/SQL 块。在 PL/SQL 中,变量是使用 INTO 语法而不是您使用的赋值语法从查询中填充的。

declare
    txt varchar2(128);
    n pls_integer;
begin
    --  this is how to assign a literal
    txt := 'your message here';

    --  how to assign the output from a query
    SELECT dbms_random.value(1,10) num 
    into n
    FROM dual;

end;

Although, you don't need to use the query syntax. This is valid, and better practice:

虽然,您不需要使用查询语法。这是有效的,更好的做法:

declare
    n pls_integer;
begin
    n := dbms_random.value(1,10);
end; 

回答by mahi_0707

Alternatively, You can create a function for generating random numbers. This can be used in anywhere in the code.

或者,您可以创建一个用于生成随机数的函数。这可以在代码的任何地方使用。

    create or replace function RANDOM
    return number 
    is 
    a number ; 
    begin
    select round(dbms_random.value(1,10)) rnum
    into a 
    from dual;
    return a  ;
    end;
    /

OUTPUT:

输出:

Function created.

函数创建。

SQL> select Random from dual;

SQL> 从双中选择随机;

RANDOM

随机的

     6                                                                                                                  

SQL> select Random from dual;

SQL> 从双中选择随机;

RANDOM

随机的

     9                                                                                                                  

回答by TastySlowCooker

For a set of consecutive integers randomly distributed uniformly (in the example below between 1 and 10), I suggest:

对于一组随机均匀分布的连续整数(在下面的示例中,介于 1 和 10 之间),我建议:

select round(dbms_random.value(0.5,10.49999999999),0) from dual

Otherwise I'll unintentionally restrict the first and last number in the set to half the probability of being chosen as the rest of the set.

否则我会无意中将集合中的第一个和最后一个数字限制为被选为集合其余部分的概率的一半。

回答by Monicalh

If you want to get a random number of n digits you can do this

如果你想得到一个随机数的 n 位数字,你可以这样做

CREATE OR REPLACE FUNCTION NUM_RANDOM(N IN NUMBER)
RETURN NUMBER 
AS 
BEGIN
    RETURN TRUNC (DBMS_RANDOM.VALUE(POWER(10, N - 1), POWER(10, N) - 1));
END NUM_RANDOM;

回答by ThePolyhedric

Integers:

整数:

select dbms_random.random from dual

Positive integers:

正整数:

select abs(dbms_random.random) from dual

回答by Krishna

DECLARE
l_check Integer:=1;
BEGIN
    WHILE l_check < 5 LOOP
        l_check := DBMS_RANDOM.VALUE(1,10);
        DBMS_OUTPUT.PUT_LINE(l_check);
    END LOOP;
END;

-- DBMS_RANDOM.VALUE Gives the random value within the range.

-- DBMS_RANDOM.VALUE 给出范围内的随机值。