TOAD 脚本中的变量

发布于 2024-08-15 21:51:10 字数 524 浏览 6 评论 0原文

我有一个正在 TOAD 中执行的 SQL 脚本。目前,我只是用一个又一个的语句来排列它,因此:

select such-and-such from somewhere;

delete other-thing from somewhere-else;

等等。一些 where 子句最终会重复,因为我有复杂的内部查询来获取要操作的特定 ID。我想在变量中捕获脚本开头的 ID,然后在后续的 where 子句中使用该变量。像这样的事情:

variable MY_ID = select the-ID from somewhere;

select such-and-such from somewhere where ID = @MY_ID;

显然,我正在编写该语法,但这就是我正在寻找的。但我不确定这在 TOAD 脚本中是否可行。我知道我可以将整个事情转换为 PL/SQL 块,但由于各种原因我试图避免这样做。

有什么方法可以使用 TOAD 执行此操作而不转换为 PL/SQL 块吗?

I have a SQL script that is being executed in TOAD. Currently, I have it laid out with just statement after statement, thusly:

select such-and-such from somewhere;

delete other-thing from somewhere-else;

And so on. Some of the where clauses end up being repetitive because I have complex inner queries to get particular IDs to operate on. I'd like to capture the ID in the beginning of the script in a variable, and then use that variable in subsequent where clauses. So something like this:

variable MY_ID = select the-ID from somewhere;

select such-and-such from somewhere where ID = @MY_ID;

Obviously, I'm making up that syntax, but that is what I'm looking for. But I'm not sure if that is possible in a TOAD script. I know I can convert the whole thing to a PL/SQL block but I'm trying to avoid having to do that for various reasons.

Any way to do this using TOAD without converting to a PL/SQL block?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

弃爱 2024-08-22 21:51:10

我想这会实现你想要的。您可以声明一个绑定变量,向其中插入一个值,然后在以后的语句中使用它。

variable l_var varchar2(1);

begin
  select dummy
    into :l_var
    from dual;
end;

select *
  from dual
 where dummy = :l_var;

I think this will accomplish what you want. You can declare a bind variable, insert a value into it, and then use it in future statements.

variable l_var varchar2(1);

begin
  select dummy
    into :l_var
    from dual;
end;

select *
  from dual
 where dummy = :l_var;
少女的英雄梦 2024-08-22 21:51:10

我使用 SQL*PLUS 替换变量。他们得到了 TOAD 的支持。
您可以按 F5 执行此代码。

COLUMN VAR NEW_VALUE VAR_VALUE

SELECT 'SOMETHING' VAR FROM DUAL;  --this sets 'VAR_VALUE' = 'SOMETHING'

SELECT '&VAR_VALUE' FROM DUAL;  --this uses the value set by the previous stmt.

I use SQL*PLUS substitution variables. They are supported by TOAD.
You can execute this code by pressing F5.

COLUMN VAR NEW_VALUE VAR_VALUE

SELECT 'SOMETHING' VAR FROM DUAL;  --this sets 'VAR_VALUE' = 'SOMETHING'

SELECT '&VAR_VALUE' FROM DUAL;  --this uses the value set by the previous stmt.
随波逐流 2024-08-22 21:51:10

我不再主动使用 TOAD,但应该有某种机制来设置绑定参数的值,即从 ID = :myid; 的某个位置选择这样那样的内容,这样每次发生时 TOAD 都会提供该参数的值相同。

或者,您可以创建会话上下文值或 PL/SQL 包变量(注意:与重写整个代码以使用 PL/SQL 不同)。请参阅此问题

I no longer actively use TOAD, but there should be some mechanism for setting values for bind parameters ie select such-and-such from somewhere where ID = :myid; such that every time it occurs TOAD supplies the same value for that parameter.

Alternatively, you could create a session context value or PL/SQL package variable (note: not the same thing as rewriting your entire code to use PL/SQL). See this question

纸短情长 2024-08-22 21:51:10

如果使用得当,TOAD 中的“计算字段”功能实际上非常强大。它只不过是一个将自身附加到查询本身的“令牌”脚本编辑器。它只能通过查询设计编辑器使用,而不能从本机编辑器使用,本机编辑器允许您直接编写 SQL。

作为提示,下次您在 TOAD 中创建查询设计并需要创建复杂的 WHERE 或子查询时,请尝试“计算字段”功能并使用 FORMS 选项将您的条件基本上附加到给定列或查询。你会惊讶地发现它有多么强大。它有助于使 SQL 查询保持良好的可读格式。

The "Calculated Fields" feature in TOAD is actually quite powerful if used in the right way. It's nothing more than a "token" script editor that attaches itself to the Query itself. It's only available via the Query Design Editor, and not from the native Editor, which allows you to write straight up SQL.

As a hint, next time you create a Query Designed in TOAD and need to create complex WHERE or sub-queries, try the "Calculated Fields" feature and use the FORMS option to basically attach your conditions to a given column or query. You'll be surprised how powerful it is. And it helps keep your SQL query in a nice readable format.

随风而去 2024-08-22 21:51:10

当我使用 CASE 句子名称 AS Ticket 的结果进行“分组依据”时,Toad 显示错误,指出 Ticket 不是有效项目,

SELECT 
CASE WHEN ( amt_1 >= 10000000                             ) THEN  'R100000'
     WHEN ( amt_1 <   9000000 and amt_1 >=  8000000       ) THEN  'R090000'
     WHEN ( amt_1 <   8000000 and amt_1 >=  7000000       ) THEN  'R080000'
     WHEN ( amt_1 <   7000000 and amt_1 >=  6000000       ) THEN  'R070000'
     WHEN ( amt_1 <   6000000 and amt_1 >=  5000000       ) THEN  'R060000'
     WHEN ( amt_1 <   5000000 and amt_1 >=  4000000       ) THEN  'R050000'
     WHEN ( amt_1 <   4000000 and amt_1 >=  3000000       ) THEN  'R040000'
     WHEN ( amt_1 <   3000000 and amt_1 >=  2000000       ) THEN  'R030000'
     WHEN ( amt_1 <   2000000 and amt_1 >=  1000000       ) THEN  'R020000'
     WHEN ( amt_1 <   1000000 and amt_1 >=  500000        ) THEN  'R010000'
     WHEN ( amt_1 <    500000 and amt_1 >=  100000        ) THEN  'R005000'
     WHEN ( amt_1 <    100000                             ) THEN  'R001000'             
END  as Ticket,    
CAST ( SUM(AMT_1/100) AS DECIMAL(10,2) )  
Count(*)     
FROM BASE24.PTLF 
GROUP BY  Ticket
                   

When I tray to do a 'group by' using the result of the CASE sentence name AS Ticket, Toad show error saying that Ticket in not a valid item,

SELECT 
CASE WHEN ( amt_1 >= 10000000                             ) THEN  'R100000'
     WHEN ( amt_1 <   9000000 and amt_1 >=  8000000       ) THEN  'R090000'
     WHEN ( amt_1 <   8000000 and amt_1 >=  7000000       ) THEN  'R080000'
     WHEN ( amt_1 <   7000000 and amt_1 >=  6000000       ) THEN  'R070000'
     WHEN ( amt_1 <   6000000 and amt_1 >=  5000000       ) THEN  'R060000'
     WHEN ( amt_1 <   5000000 and amt_1 >=  4000000       ) THEN  'R050000'
     WHEN ( amt_1 <   4000000 and amt_1 >=  3000000       ) THEN  'R040000'
     WHEN ( amt_1 <   3000000 and amt_1 >=  2000000       ) THEN  'R030000'
     WHEN ( amt_1 <   2000000 and amt_1 >=  1000000       ) THEN  'R020000'
     WHEN ( amt_1 <   1000000 and amt_1 >=  500000        ) THEN  'R010000'
     WHEN ( amt_1 <    500000 and amt_1 >=  100000        ) THEN  'R005000'
     WHEN ( amt_1 <    100000                             ) THEN  'R001000'             
END  as Ticket,    
CAST ( SUM(AMT_1/100) AS DECIMAL(10,2) )  
Count(*)     
FROM BASE24.PTLF 
GROUP BY  Ticket
                   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文