如何声明variables并在同一个SQL脚本中使用它? (Oracle SQL)

我正在做一些用SQL编写的testing。 我想编写可重用的代码,因此想要在开始时声明一些variables,并在脚本中重新使用它们,如下所示:

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

我如何声明一个variables并在随后的语句中重用它? (我正在使用SQLDeveloper来访问Oracle数据库。)

我到目前为止尝试过:

  • 使用DECLARE部分并在BEGINEND;插入以下SELECT语句END; 。 使用&stupidvar访问variables。
  • 使用关键字DEFINE并访问variables。
  • 使用关键字VARIABLE并访问variables。

但是我在尝试期间遇到了各种错误(Unboundvariables,语法错误,Expected SELECT INTO …)。

在searchnetworking时,我发现了很多不同的解释,涉及SQL,PL / SQL。

先谢谢你!

在SQL * Plus脚本中有几种声明variables的方法。

首先是使用VAR。 赋值给VAR的机制是用EXEC调用:

 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特别有用。

Alternativley我们可以使用替代variables。 这些对于交互模式是很好的:

 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> 

当我们编写一个调用其他脚本的脚本时,可以预先定义variables:

 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块。 正如你所看到的,我们仍然可以交互地给已声明的variables赋值:

 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> 

尝试使用双引号,如果它是一个字符variables:

 DEFINE stupidvar = "'stupidvarcontent'"; 

要么

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

UPD:

 SQL*Plus: Release 10.2.0.1.0 - Production on Wed Aug 25 17:13:26 2010 Copyright (c) 1982, 2005, Oracle. All rights reserved. SQL> conn od/od@etalon Connected. SQL> define var = "'FL-208'"; SQL> select code from product where code = &var; old 1: select code from product where code = &var new 1: select code from product where code = 'FL-208' CODE --------------- FL-208 SQL> define var = 'FL-208'; SQL> select code from product where code = &var; old 1: select code from product where code = &var new 1: select code from product where code = FL-208 select code from product where code = FL-208 * ERROR at line 1: ORA-06553: PLS-221: 'FL' is not a procedure or is undefined 

在PL / SQL v.10中

关键字声明用于声明variables

 DECLARE stupidvar varchar(20); 

分配一个值,你可以设置它当你声明

 DECLARE stupidvar varchar(20) := '12345678'; 

或者select你使用INTO语句的variables,但是你需要将语句换成BEGINEND ,还需要确保只返回单个值,并且不要忘记分号。

所以完整的声明如下:

 DECLARE stupidvar varchar(20); BEGIN SELECT stupid into stupidvar FROM stupiddata CC WHERE stupidid = 2; END; 

你的variables只能在BEGINEND所以如果你想使用多个,你将不得不做多个BEGIN END包装

 DECLARE stupidvar varchar(20); BEGIN SELECT stupid into stupidvar FROM stupiddata CC WHERE stupidid = 2; DECLARE evenmorestupidvar varchar(20); BEGIN SELECT evenmorestupid into evenmorestupidvar FROM evenmorestupiddata CCC WHERE evenmorestupidid = 42; INSERT INTO newstupiddata (newstupidcolumn, newevenmorestupidstupidcolumn) SELECT stupidvar, evenmorestupidvar FROM dual END; END; 

希望这可以为你节省一些时间

如果你想声明date,然后在SQL Developer中使用它。

 DEFINE PROPp_START_DT = TO_DATE('01-SEP-1999') SELECT * FROM proposal WHERE prop_start_dt = &PROPp_START_DT 

只是想添加Matas的答案。 也许这是显而易见的,但是我search了很长时间才发现这个variables只能在BEGIN-END结构中访问,所以如果以后需要在某些代码中使用它,则需要将这些代码放入BEGIN -END块

请注意,这些块可以嵌套

 DECLARE x NUMBER; BEGIN SELECT PK INTO x FROM table1 WHERE col1 = 'test'; DECLARE y NUMBER; BEGIN SELECT PK INTO y FROM table2 WHERE col2 = x; INSERT INTO table2 (col1, col2) SELECT y,'text' FROM dual WHERE exists(SELECT * FROM table2); COMMIT; END; END; 

这是你的答案:

DEFINE num:= 1; – 默认值需要冒号。 SELECT&num FROM dual;