关于sql替换
当我运行以下查询时,第二个查询失败,因为 prev_test_ref1 变量未定义。如果我删除第一个查询中的插入语句,再次运行,那么它就可以工作并在第二个查询中使用第一个 sql 查询中的 prev_test_ref1 值。是因为变量作用域吗?我怎样才能用插入语句解决这个问题。
查询1
column prev_test_ref1 new_value prev_test_ref1 ;
insert into testing.test_ref_details(TEST_TYPE,TEST_REF_NO)
select '1',max(test_ref_no) as prev_test_ref1
from testing.test_runs_status
where test_type = 1
and run_status = 1
and test_end_dt = (select last_day(add_months(trunc(sysdate),-6))+2 from dual)
group by test_end_dt
;
查询2
column last_test_end_dt new_value last_test_end_dt;
select to_char(test_completion_dt,'DD-MON-YYYY HH24:MI:SS') as last_test_end_dt
from testing.test_runs_status
where test_ref_no = '&prev_test_ref1';
When i ran the below queries it's failing in the second query becuase prev_test_ref1 variable is not defined. If i remove the insert statement in the first query ,run again then it's working and using the prev_test_ref1 value from the first sql query in second query. Is it because of variable scope? How can i resolve this with the insert statement.
QUERY1
column prev_test_ref1 new_value prev_test_ref1 ;
insert into testing.test_ref_details(TEST_TYPE,TEST_REF_NO)
select '1',max(test_ref_no) as prev_test_ref1
from testing.test_runs_status
where test_type = 1
and run_status = 1
and test_end_dt = (select last_day(add_months(trunc(sysdate),-6))+2 from dual)
group by test_end_dt
;
QUERY2
column last_test_end_dt new_value last_test_end_dt;
select to_char(test_completion_dt,'DD-MON-YYYY HH24:MI:SS') as last_test_end_dt
from testing.test_runs_status
where test_ref_no = '&prev_test_ref1';
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 SQLPlus 中,替换变量只能用 SELECT 语句定义。您的第一次插入不会返回行,因此它不起作用(想一想:它只返回
插入的 1 行。
,SQLPlus 无法知道插入的值。)我建议您添加一个步骤将值保存到变量中(或使用 PL/SQL 块):
In SQLPlus substitution variables will only be defined with SELECT statements. Your first insert doesn't return rows so it won't work (think about it: it only returns
1 row inserted.
, SQLPlus has no way to know the value inserted.)I suggest you add a step to save the value into the variable (or use a PL/SQL block):
INSERT 语句有一个 RETURNING 子句。我们可以使用它来访问表中的“未知”值。以下示例使用 RETURNING 从序列中获取分配的 nextval,但我们可以返回行中的任何列:
不幸的是,RETURNING 子句仅适用于单行 SQL。
The INSERT statement has a RETURNING clause. We can use this to get access to "unknown" values from the table. The following examples uses RETURNING to get the assigned nextval from a sequence, but we could return any column from the row:
Unfortunately the RETURNING clause only works with single-row SQL.
目前还不清楚整个脚本的目的是什么,特别是考虑到评论“我有类似的 sql 查询,它返回多行。在这种情况下,我不能有单独的插入语句。”
如果您想使用选择的结果,请查看多表插入< /a> 符合要求。您的 select 语句可以插入主表和第二个表(例如全局临时表)。然后,您可以查询全局临时表以查看插入了哪些行。
It isn't really clear what the purpose of the whole script is, especially in light of the comment "i have similar sql query which returns multiple rows. In that case i cant have separate insert statement."
If you want to use the results of a select, see if Multi-Table Inserts fit the bill. Your select statement can insert into both the primary table and also a second table (eg a global temporary table). You can then query the global temporary table to see what rows were inserted.