简体   繁体   English

在Oracle SQL Developer中使用动态变量

[英]Using Dynamic Variables in Oracle SQL Developer

Trying to setup a WHILE LOOP in Oracle SQL Developer but I'm having a lot of trouble trying to set/define a variable. 尝试在Oracle SQL Developer中设置WHILE LOOP,但是尝试设置/定义变量时遇到很多麻烦。 I'm used to working in PHP where I would declare this on my PHP scripts. 我曾经在PHP中工作过,在PHP脚本中会对此进行声明。

Here is what I have below. 这是我下面的内容。 The script is unfinished I am basically going to have it loop through weeks when it is done. 该脚本尚未完成,基本上,我将在完成后的数周内将其循环。

VARIABLE MYDateVar2 varchar2(40);
EXEC :MYDateVar2 := '01-JAN-14';

select customer_name, 
sum(CASE when to_char(to_date(PLANNED_SHIP_DATE), 'WW') = 40 then (REVISED_QTY_DUE - QTY_SHIPPED) * SALE_UNIT_PRICE end) as Wk40
from customer_order_join 
where planned_ship_date >= :MYDateVar2 
group by customer_name;

So I am having trouble basically placing the variable 'MYDateVar2' back into the script. 所以我很难将变量'MYDateVar2'重新放回脚本中。 I've tried using @@ and : before but Oralce SQL Developer keeps prompting me for values. 我曾尝试使用@@和:,但是Oralce SQL Developer不断提示我输入值。 I also know I should probably set the varchar to be DATE but that should be fine for now. 我也知道我应该将varchar设置为DATE,但是现在应该没问题。

Can someone please let me know how I properly insert a variable into the script? 有人可以让我知道如何在脚本中正确插入变量吗? Thanks! 谢谢!

What you're doing is fine, as long as you tell SQL Developer to run the whole script, not just the select statement; 只要您告诉SQL Developer运行整个脚本,而不仅仅是select语句,您所做的就很好。 you need to Run Script (F5), rather than Run Statement (Ctrl+Enter). 您需要运行脚本(F5),而不是运行语句(Ctrl + Enter)。 If it runs the statement stand-alone then it will always prompt for the bind variable value. 如果它独立运行该语句,则它将始终提示您输入绑定变量值。

Not directly relevant, but... presumably you've used a date format that is valid for your client's NLS settings, but you shouldn't rely on that; 不直接相关,但是...大概您使用了对客户的NLS设置有效的日期格式,但是您不应该依赖该格式。 it's safer to always explicitly set the format: 始终明确设置格式是比较安全的:

where planned_ship_date >= to_date(:MYDateVar2, 'DD-MON-RR')

If you're going to loop, though, then you need to be writing PL/SQL, so you might as well declare the variable inside the block rather than at client level, unless you want to pass the same value into multiple blocks or stored procedure calls. 但是,如果要循环,则需要编写PL / SQL,因此您最好在块内部而不是在客户端级别声明变量,除非要将相同的值传递给多个块或存储过程调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM