admin管理员组文章数量:1122832
I am new to using SQL Developer and I have tried Googling but nothing seems to be working for me. I am trying to use a variable twice in a statement and only be prompted once for the variable. However when I try using && even if I add undefine. The code will run twice for me asking for the variable but after the second time it keeps reusing the last given variable This is the code that has gotten me close:
UNDEFINE PayeeNumber
select *
from policytran
where comm1payee = &&PayeeNumber or comm2payee = &&PayeeNumber
I am new to using SQL Developer and I have tried Googling but nothing seems to be working for me. I am trying to use a variable twice in a statement and only be prompted once for the variable. However when I try using && even if I add undefine. The code will run twice for me asking for the variable but after the second time it keeps reusing the last given variable This is the code that has gotten me close:
UNDEFINE PayeeNumber
select *
from policytran
where comm1payee = &&PayeeNumber or comm2payee = &&PayeeNumber
Share
Improve this question
asked Nov 21, 2024 at 21:38
Robert ZRobert Z
1
2
|
1 Answer
Reset to default 0Generally speaking, undefine
should work. This is a SQL*Plus demo.
Sample table:
SQL> select * From policytran;
COMM1PAYEE COMM2PAYEE NAME
---------- ---------- -----
1 100 Mike
2 100 Scott
3 300 Jason
Query which uses substitution variable (the same as yours):
SQL> select *
2 from policytran
3 where comm1payee = &&payeenumber or comm2payee = &&payeenumber;
Enter value for payeenumber: 1
COMM1PAYEE COMM2PAYEE NAME
---------- ---------- -----
1 100 Mike
Re-running the same statement returns the same data because of the &&
usage:
SQL> /
COMM1PAYEE COMM2PAYEE NAME
---------- ---------- -----
1 100 Mike
Undefining it; query works as expected (it asks for the value):
SQL> undefine payeenumber
SQL> /
Enter value for payeenumber: 100
COMM1PAYEE COMM2PAYEE NAME
---------- ---------- -----
1 100 Mike
2 100 Scott
SQL>
On the other hand, that query could be rewritten so that you don't use the variable twice.
Undefining it first:
SQL> undefine payeenumber
A new query:
SQL> select *
2 from policytran
3 where &payeenumber in (comm1payee, comm2payee);
Enter value for payeenumber: 100
COMM1PAYEE COMM2PAYEE NAME
---------- ---------- -----
1 100 Mike
2 100 Scott
Re-running it asks for the value:
SQL> /
Enter value for payeenumber: 3
COMM1PAYEE COMM2PAYEE NAME
---------- ---------- -----
3 300 Jason
SQL>
本文标签: Reset Variable Value with prompt SQLStack Overflow
版权声明:本文标题:Reset Variable Value with prompt SQL - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736307055a1933177.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
UNDEFINE PayeeNumber
or replace it withACCEPT PayeeNumber PROMPT 'PayeeNumber: '
– keithwalsh Commented Nov 21, 2024 at 22:18select
as a statement on its own? – Alex Poole Commented Nov 21, 2024 at 23:08