简体   繁体   English

对于在单个语句中多次出现的替换变量,如何让 SQL Developer/SQL+ 只提示一次?

[英]How can I make SQL Developer/SQL+ prompt only once for a substitution variable that occurs multiple times in a single statement?

I have a query roughly like this:我有一个大致如下的查询:

 select * 
  from A_TABLE 
  where A_COLUMN = '&aVariable'
    union
 select * 
  from A_TABLE 
  where B_COLUMN = '&aVariable';

But when I run it, SQL Developer prompts me for the variable twice, even though it's the same variable.但是当我运行它时,SQL Developer 两次提示我输入变量,即使它是同一个变量。

If there's a way to make it prompt only once for a variable that is used twice, how do I do it?如果有办法让它只提示一次使用两次的变量,我该怎么做?

I do not want to use a script, it must be a single executable query.不想用一个脚本,它必须是一个可执行查询。

As I was forming this post, I figured out how to do it:在我撰写这篇文章时,我想出了如何做到这一点:

 :a_var
 select * 
  from A_TABLE 
  where A_COLUMN = :a_var
    union
 select * 
  from A_TABLE 
  where B_COLUMN = :a_var;

SQL Developer will then prompt for a bind variable, you can enter it and hit apply. SQL Developer 然后将提示输入绑定变量,您可以输入它并点击应用。

select * 
  from A_TABLE 
  where A_COLUMN = '&aVariable'
    union
 select * 
  from A_TABLE 
  where B_COLUMN = '&&aVariable';

Notice how the 2nd (and subsequent) variables will use double-ampersand (&&)请注意第二个(及后续)变量将如何使用双与号 (&&)

I know you found another way to do it, but FYI the basic answer is that if you double up the ampersand (eg, use '&&aVariable'), then the value you enter for the substitution variable will be remembered for the length of your session.我知道您找到了另一种方法来做到这一点,但仅供参考,基本答案是,如果您将&符号加倍(例如,使用“&&aVariable”),那么您为替换变量输入的值将在您的会话期间被记住. Note that in this case if you re-execute the query you will not be prompted again, it will keep using the same value.请注意,在这种情况下,如果您重新执行查询,将不会再次提示您,它将继续使用相同的值。

So when you use & substitution you are just using the single & as a faster way instead of having to type a value over again.因此,当您使用 & 替换时,您只是使用单个 & 作为一种更快的方式,而不必再次键入一个值。 By using the && the value becomes locked-in to the variable name.通过使用 && 值被锁定在变量名中。 So &&variable would be different from &&variable1.所以 &&variable 与 &&variable1 不同。 By doing it this way you will be prompted once and that value you inputted will be adhered to that variable name.通过这种方式,您将收到一次提示,并且您输入的值将遵循该变量名称。 Simply put if you write your code like this:简单地说,如果你像这样编写代码:

SELECT *
FROM A_TABLE
WHERE A_COLUMN = '&&aVariable1'
UNION
SELECT *
FROM A_TABLE
WHERE B_COLUMN ='&&aVariable2';

Then if you wanted to use a different set of values you will have to change the variable name to something like this '&&aVariable3' and '&&aVariable4' should be sufficient for another set.然后,如果您想使用一组不同的值,则必须将变量名称更改为类似这样的 '&&aVariable3' 和 '&&aVariable4' 应该足以用于另一组。

ACCEPT variableToPrompt PROMPT "Enter variable" DEFAULT "test";

SELECT
    *
FROM    
    TABLE
WHERE
    COLUMN = '&variableToPrompt'

Note that the ' in '&variableToPrompt' is only required if your variable is a string.请注意,仅当您的变量是字符串时,才需要 '&variableToPrompt' 中的 '。 The default value is optional.默认值是可选的。

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

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