PLSQL generate random integer

后端 未结 6 1177
离开以前
离开以前 2021-02-08 11:53

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,         


        
6条回答
  •  北荒
    北荒 (楼主)
    2021-02-08 12:36

    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; 
    

提交回复
热议问题