我正在努力学习一些oracle sql语法。 (我习惯了sql server)。
我尝试使用变量存储值以便稍后再次使用该值。
然而在变量块中显然我需要进行select into查询(对我来说很奇怪),我无法获得屏幕输出。
我设法使用临时表编写以下查询:
CREATE GLOBAL TEMPORARY TABLE temp
(leeftijd varchar(30),
aantal number,
aantalProcentueel number)
ON COMMIT DELETE ROWS;
declare
aantalLeden number;
begin
select count(*) into :aantalLeden
FROM dbcmnu_all
WHERE DBCMNU_ALL.ZF_VP='105' AND DBCMNU_ALL.BEGIN_ZF_VP <= sysdate AND
(DBCMNU_ALL.EIND_ZF_VP >= sysdate Or DBCMNU_ALL.EIND_ZF_VP Is Null)
AND DBCMNU_ALL.DATDEC Is Null;
select case
when rijksnummer is null then 'ongeldig'
when length(rijksnummer) < 6 then 'ongeldig'
when substr(rijksnummer, 5, 2) <= 0 or substr(rijksnummer, 5, 2) > 12 then 'ongeldig'
when months_between(TRUNC(sysdate),to_date(substr(rijksnummer, 1, 8), 'YYYYMMDD'))/12 between 0 and 9.99 then '0-9'
when months_between(TRUNC(sysdate),to_date(substr(rijksnummer, 1, 8), 'YYYYMMDD'))/12 between 10 and 19.99 then '10-19'
when months_between(TRUNC(sysdate),to_date(substr(rijksnummer, 1, 8), 'YYYYMMDD'))/12 between 20 and 29.99 then '20-29'
when months_between(TRUNC(sysdate),to_date(substr(rijksnummer, 1, 8), 'YYYYMMDD'))/12 between 30 and 39.99 then '30-39'
when months_between(TRUNC(sysdate),to_date(substr(rijksnummer, 1, 8), 'YYYYMMDD'))/12 between 40 and 49.99 then '40-49'
when months_between(TRUNC(sysdate),to_date(substr(rijksnummer, 1, 8), 'YYYYMMDD'))/12 between 50 and 59.99 then '50-59'
when months_between(TRUNC(sysdate),to_date(substr(rijksnummer, 1, 8), 'YYYYMMDD'))/12 between 60 and 69.99 then '60-69'
when months_between(TRUNC(sysdate),to_date(substr(rijksnummer, 1, 8), 'YYYYMMDD'))/12 between 70 and 79.99 then '70-79'
when months_between(TRUNC(sysdate),to_date(substr(rijksnummer, 1, 8), 'YYYYMMDD'))/12 between 80 and 89.99 then '80-89'
when months_between(TRUNC(sysdate),to_date(substr(rijksnummer, 1, 8), 'YYYYMMDD'))/12 between 90 and 99.99 then '90-99'
when months_between(TRUNC(sysdate),to_date(substr(rijksnummer, 1, 8), 'YYYYMMDD'))/12 >= 100 then '100+'
end as leeftijd
, count(*) as aantal
into temp
FROM dbcmnu_all
WHERE DBCMNU_ALL.ZF_VP='105' AND DBCMNU_ALL.BEGIN_ZF_VP <= sysdate AND
(DBCMNU_ALL.EIND_ZF_VP >= sysdate Or DBCMNU_ALL.EIND_ZF_VP Is Null)
AND DBCMNU_ALL.DATDEC Is Null
group by
case
when rijksnummer is null then 'ongeldig'
when length(rijksnummer) < 6 then 'ongeldig'
when substr(rijksnummer, 5, 2) <= 0 or substr(rijksnummer, 5, 2) > 12 then 'ongeldig'
when months_between(TRUNC(sysdate),to_date(substr(rijksnummer, 1, 8), 'YYYYMMDD'))/12 between 0 and 9.99 then '0-9'
when months_between(TRUNC(sysdate),to_date(substr(rijksnummer, 1, 8), 'YYYYMMDD'))/12 between 10 and 19.99 then '10-19'
when months_between(TRUNC(sysdate),to_date(substr(rijksnummer, 1, 8), 'YYYYMMDD'))/12 between 20 and 29.99 then '20-29'
when months_between(TRUNC(sysdate),to_date(substr(rijksnummer, 1, 8), 'YYYYMMDD'))/12 between 30 and 39.99 then '30-39'
when months_between(TRUNC(sysdate),to_date(substr(rijksnummer, 1, 8), 'YYYYMMDD'))/12 between 40 and 49.99 then '40-49'
when months_between(TRUNC(sysdate),to_date(substr(rijksnummer, 1, 8), 'YYYYMMDD'))/12 between 50 and 59.99 then '50-59'
when months_between(TRUNC(sysdate),to_date(substr(rijksnummer, 1, 8), 'YYYYMMDD'))/12 between 60 and 69.99 then '60-69'
when months_between(TRUNC(sysdate),to_date(substr(rijksnummer, 1, 8), 'YYYYMMDD'))/12 between 70 and 79.99 then '70-79'
when months_between(TRUNC(sysdate),to_date(substr(rijksnummer, 1, 8), 'YYYYMMDD'))/12 between 80 and 89.99 then '80-89'
when months_between(TRUNC(sysdate),to_date(substr(rijksnummer, 1, 8), 'YYYYMMDD'))/12 between 90 and 99.99 then '90-99'
when months_between(TRUNC(sysdate),to_date(substr(rijksnummer, 1, 8), 'YYYYMMDD'))/12 >= 100 then '100+'
end
order by leeftijd;
end;
select leetijd, aantal, aantalProcentueel
from temp;
此查询失败,说:PLS-00103:遇到符号“SELECT”
现在如何在oracle sql中声明变量并在sql语句中稍后使用该变量? (我想添加一个列blabla / variable作为columnname
如果可能的话,我也想在屏幕上输出一个......
答案 0 :(得分:1)
PLS-00103是因为PL / SQL解析器试图将select
解释为块流的一部分,并且在那里无效。您需要使用/
:
declare
aantalLeden number;
begin
...
end;
/
select leetijd, aantal, aantalProcentueel
from temp;
@a_horse_with_no_name是对的,你应该做insert into
,而不是select into
; @YaroslavShabalin对你的额外冒号是正确的,因为aantalLeden
是一个本地PL / SQL变量,而不是绑定变量:
select count(*) into aantalLeden
FROM dbcmnu_all
WHERE DBCMNU_ALL.ZF_VP='105' AND DBCMNU_ALL.BEGIN_ZF_VP <= sysdate AND
(DBCMNU_ALL.EIND_ZF_VP >= sysdate Or DBCMNU_ALL.EIND_ZF_VP Is Null)
AND DBCMNU_ALL.DATDEC Is Null;
insert into temp (leeftijd, aantal number)
select case
...
end as leeftijd
, count(*) as aantal
from ...
group by ...;
在用于插入的选择中有一个order by
子句没什么意义。我不确定你打算用你的aantalLeden
值做什么;目前尚不清楚是否要将aantalProcentueel
值设置为所有行的值。
你可能根本不想要一个临时表,这取决于你接下来要做什么。