PLSQL生成随机整数 [英] PLSQL generate random integer

查看:620
本文介绍了PLSQL生成随机整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Oracle Sql开发人员11g中,如何生成随机整数并将其分配给变量?到目前为止,这是我尝试过的:

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:

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?

推荐答案

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

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.

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; 

这篇关于PLSQL生成随机整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆