首页 > 解决方案 > 使用变量时 SQL 查询不给出输出

问题描述

我在 sql 表中插入了下面的查询。

select account_num,bill_seq,bill_version,
to_char(start_of_bill_dtm,'YYYYMM-DD') st_bill_dtm, 
to_char(bill_dtm - 1,'YYYYMM-DD') en_bill_dtm,
to_char(actual_bill_dtm,'YYYYMM-DD') act_bill_dtm, 
round((invoice_net_mny + invoice_tax_mny)/1000,0) mon_bill,
bill_type_id,bill_status
from billsummary
where to_char(bill_dtm - 1,'YYYYMM') between'||chr(32)||
startMonth ||chr(32)||'and'|| chr(32)||endMonth ||chr(32)||
'and cancellation_dtm is null

我试图在下面的循环中执行该查询。

FOR a in 1 .. 17 LOOP 
 dbms_output.put_line(startmonth);
 dbms_output.put_line(endmonth);
 dbms_output.put_line('in the loop');
 sql_query:='select run_sql from table_1';
 Execute IMMEDIATE sql_query;
 Execute IMMEDIATE 'commit';
END LOOP;

但它没有给出任何输出。我尝试通过硬编码“startMonth”和“endMonth”来执行相同的查询,如下所示。

select account_num,bill_seq,bill_version,
to_char(start_of_bill_dtm,'YYYYMM-DD') st_bill_dtm, 
to_char(bill_dtm - 1,'YYYYMM-DD') en_bill_dtm,
to_char(actual_bill_dtm,'YYYYMM-DD') act_bill_dtm, 
round((invoice_net_mny + invoice_tax_mny)/1000,0) mon_bill,
bill_type_id,bill_status
from billsummary
where to_char(bill_dtm - 1,'YYYYMM') between'202011 'and'202104'and cancellation_dtm is null

然后它起作用并给了我一个输出。变量(startMonth 和 endMonth)的值在循环中也是可见的。有谁可以找出原因?

更新

我更新了查询以使用解决方案中提到的绑定变量。

这是我的程序。

CREATE OR REPLACE procedure schema.sixMonthAverage (startMonth varchar,endMonth varchar ,thirdMonth varchar )
IS
start_date varchar :=:startMonth;
end_date varchar :=:endMonth;

begin

for c_rec in(select run_sql from table_1)
 loop
      execute immediate c_rec.run_sql using start_date, end_date;
END LOOP;

EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line('Exception');
END;
/

这给了我“startMonth”和“endMonth”的错误“错误绑定变量”。

这就是我所说的过程。

Execute Schema.sixMonthAverage ('202011', '202104', '202001');
commit;

查询位于 table_1 中。如下所示。

create table tbl_six_mon_1
as
select account_num,bill_seq,bill_version,
to_char(start_of_bill_dtm,'YYYYMM-DD') st_bill_dtm, 
to_char(bill_dtm - 1,'YYYYMM-DD') en_bill_dtm,
to_char(actual_bill_dtm,'YYYYMM-DD') act_bill_dtm, 
round((invoice_net_mny + invoice_tax_mny)/1000,0) mon_bill,
bill_type_id,bill_status
from billsummary
where to_char(bill_dtm - 1,'YYYYMM') between :start_date and :end_date
and cancellation_dtm is null

任何人都可以识别我的方法中的错误吗?

标签: sqloracle

解决方案


已经有一段时间了,但是,

问题1。

我假设您在 table_1 中插入第一个查询,并在从 table_1 中提取该查询后尝试执行该查询。引用看起来完全错误。

where to_char(bill_dtm - 1,'YYYYMM') between'||chr(32)||
startMonth ||chr(32)||'and'|| chr(32)||endMonth ||chr(32)||
'and cancellation_dtm is null

我希望它看起来像

where to_char(bill_dtm - 1,'YYYYMM') between startMonth and endMonth
and cancellation_dtm is null

问题2。

我怀疑 startMonth 和 endMonth 不在插入到 table_1 中的 SQL 的上下文中,可能类似于(我已经多年没有完成这个 sql,所以我的语法可能是错误的)

您可以使用绑定变量并假设 table_1 中的查询有 2 个绑定变量。

**your SQL query here.**
where to_char(bill_dtm - 1,'YYYYMM') between :1 and :2
and cancellation_dtm is null

当你执行语句时,建立那些绑定变量。

execute immediate sql_query using startMonth, endMonth,

问题3。

您的最终查询,您在硬编码的“202011”中有一个尾随空格

正如我所说,我已经有好几年没有做这种事情了,所以我的语法可能有点不对劲,但它应该为你指明正确的方向。:)


推荐阅读