<tfoot id='X1wuq'></tfoot>

  1. <legend id='X1wuq'><style id='X1wuq'><dir id='X1wuq'><q id='X1wuq'></q></dir></style></legend>
    • <bdo id='X1wuq'></bdo><ul id='X1wuq'></ul>

    1. <i id='X1wuq'><tr id='X1wuq'><dt id='X1wuq'><q id='X1wuq'><span id='X1wuq'><b id='X1wuq'><form id='X1wuq'><ins id='X1wuq'></ins><ul id='X1wuq'></ul><sub id='X1wuq'></sub></form><legend id='X1wuq'></legend><bdo id='X1wuq'><pre id='X1wuq'><center id='X1wuq'></center></pre></bdo></b><th id='X1wuq'></th></span></q></dt></tr></i><div id='X1wuq'><tfoot id='X1wuq'></tfoot><dl id='X1wuq'><fieldset id='X1wuq'></fieldset></dl></div>

    2. <small id='X1wuq'></small><noframes id='X1wuq'>

      如何声明变量并在同一个 Oracle SQL 脚本中使用它?

      How to declare variable and use it in the same Oracle SQL script?(如何声明变量并在同一个 Oracle SQL 脚本中使用它?)

            <tbody id='LLhcT'></tbody>
          <tfoot id='LLhcT'></tfoot>

          • <legend id='LLhcT'><style id='LLhcT'><dir id='LLhcT'><q id='LLhcT'></q></dir></style></legend>
          • <small id='LLhcT'></small><noframes id='LLhcT'>

              <i id='LLhcT'><tr id='LLhcT'><dt id='LLhcT'><q id='LLhcT'><span id='LLhcT'><b id='LLhcT'><form id='LLhcT'><ins id='LLhcT'></ins><ul id='LLhcT'></ul><sub id='LLhcT'></sub></form><legend id='LLhcT'></legend><bdo id='LLhcT'><pre id='LLhcT'><center id='LLhcT'></center></pre></bdo></b><th id='LLhcT'></th></span></q></dt></tr></i><div id='LLhcT'><tfoot id='LLhcT'></tfoot><dl id='LLhcT'><fieldset id='LLhcT'></fieldset></dl></div>

              • <bdo id='LLhcT'></bdo><ul id='LLhcT'></ul>
                本文介绍了如何声明变量并在同一个 Oracle SQL 脚本中使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我想写可复用的代码,需要在开头声明一些变量并在脚本中复用,比如:

                I want to write reusable code and need to declare some variables at the beginning and reuse them in the script, such as:

                DEFINE stupidvar = 'stupidvarcontent';
                
                SELECT stupiddata
                FROM stupidtable
                WHERE stupidcolumn = &stupidvar;
                

                如何声明一个变量并在随后的语句中重用它,例如在 SQLDeveloper 中使用它.

                How can I declare a variable and reuse it in statements that follow such as in using it SQLDeveloper.

                尝试

                • 使用 DECLARE 部分并在 BEGINEND; 中插入以下 SELECT 语句.使用 &stupidvar 访问变量.
                • 使用关键字 DEFINE 并访问变量.
                • 使用关键字 VARIABLE 并访问变量.
                • Use a DECLARE section and insert the following SELECT statement in BEGIN and END;. Acces the variable using &stupidvar.
                • Use the keyword DEFINE and access the variable.
                • Using the keyword VARIABLE and access the the variable.

                但是我在尝试过程中遇到了各种各样的错误(未绑定变量、语法错误、预期的 SELECT INTO...).

                But I am getting all kinds of errors during my tries (Unbound variable, Syntax error, Expected SELECT INTO...).

                推荐答案

                在 SQL*Plus 脚本中有多种声明变量的方法.

                There are a several ways of declaring variables in SQL*Plus scripts.

                首先是使用VAR,来声明一个绑定变量.为 VAR 赋值的机制是通过 EXEC 调用:

                The first is to use VAR, to declare a bind variable. The mechanism for assigning values to a VAR is with an EXEC call:

                SQL> var name varchar2(20)
                SQL> exec :name := 'SALES'
                
                PL/SQL procedure successfully completed.
                
                SQL> select * from dept
                  2  where dname = :name
                  3  /
                
                    DEPTNO DNAME          LOC
                ---------- -------------- -------------
                        30 SALES          CHICAGO
                
                SQL>
                

                当我们想要调用具有 OUT 参数或函数的存储过程时,VAR 特别有用.

                A VAR is particularly useful when we want to call a stored procedure which has OUT parameters or a function.

                或者,我们可以使用替换变量.这些适用于交互模式:

                Alternatively we can use substitution variables. These are good for interactive mode:

                SQL> accept p_dno prompt "Please enter Department number: " default 10
                Please enter Department number: 20
                SQL> select ename, sal
                  2  from emp
                  3  where deptno = &p_dno
                  4  /
                old   3: where deptno = &p_dno
                new   3: where deptno = 20
                
                ENAME             SAL
                ---------- ----------
                CLARKE            800
                ROBERTSON        2975
                RIGBY            3000
                KULASH           1100
                GASPAROTTO       3000
                
                SQL>
                

                当我们编写一个调用其他脚本的脚本时,预先定义变量会很有用.此代码段运行时不提示我输入值:

                When we're writing a script which calls other scripts it can be useful to DEFine the variables upfront. This snippet runs without prompting me to enter a value:

                SQL> def p_dno = 40
                SQL> select ename, sal
                  2  from emp
                  3  where deptno = &p_dno
                  4  /
                old   3: where deptno = &p_dno
                new   3: where deptno = 40
                
                no rows selected
                
                SQL>
                

                最后是匿名 PL/SQL 块.如您所见,我们仍然可以交互地为声明的变量赋值:

                Finally there's the anonymous PL/SQL block. As you see, we can still assign values to declared variables interactively:

                SQL> set serveroutput on size unlimited
                SQL> declare
                  2      n pls_integer;
                  3      l_sal number := 3500;
                  4      l_dno number := &dno;
                  5  begin
                  6      select count(*)
                  7      into n
                  8      from emp
                  9      where sal > l_sal
                 10      and deptno = l_dno;
                 11      dbms_output.put_line('top earners = '||to_char(n));
                 12  end;
                 13  /
                Enter value for dno: 10
                old   4:     l_dno number := &dno;
                new   4:     l_dno number := 10;
                top earners = 1
                
                PL/SQL procedure successfully completed.
                
                SQL>
                

                这篇关于如何声明变量并在同一个 Oracle SQL 脚本中使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致
                SQL query to group by day(按天分组的 SQL 查询)
                What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)
                MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)
                MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)
                Include missing months in Group By query(在 Group By 查询中包含缺失的月份)

                  • <bdo id='TLF93'></bdo><ul id='TLF93'></ul>

                    <tfoot id='TLF93'></tfoot>
                      • <small id='TLF93'></small><noframes id='TLF93'>

                        <legend id='TLF93'><style id='TLF93'><dir id='TLF93'><q id='TLF93'></q></dir></style></legend>

                        1. <i id='TLF93'><tr id='TLF93'><dt id='TLF93'><q id='TLF93'><span id='TLF93'><b id='TLF93'><form id='TLF93'><ins id='TLF93'></ins><ul id='TLF93'></ul><sub id='TLF93'></sub></form><legend id='TLF93'></legend><bdo id='TLF93'><pre id='TLF93'><center id='TLF93'></center></pre></bdo></b><th id='TLF93'></th></span></q></dt></tr></i><div id='TLF93'><tfoot id='TLF93'></tfoot><dl id='TLF93'><fieldset id='TLF93'></fieldset></dl></div>
                            <tbody id='TLF93'></tbody>