I'm trying to do this query (in oracle) but I have some problems:
SELECT CASE
WHEN deptno = '10' THEN scott.seq.nextval
|| 'next10'
WHEN deptno = '20' THEN scott.seqnextval
|| 'next20'
WHEN deptno = '30' THEN scott.seq.currval
|| 'curr'
END col_1
FROM scott.emp;
I'm getting this results:
COL_1
----------------------------------------------
191next20
192curr
193curr
194next20
195curr
196curr
197next10
198next20
199next10
200curr
201next20
202curr
203next20
204next10
205next20
206next10
207next10
And this is what I think they should be:
COL_1
----------------------------------------------
191next20
192curr
193curr
194next20
194curr
194curr
197next10
198next20
199next10
199curr
201next20
201curr
203next20
204next10
205next20
206next10
207next10
So, why i get the next value of the sequence also when I should have the current value and not only when the case selects the next value?
Yeah, this could be done with a plsql script but I can't.
Thanks you!
Nextval and currval are not functions, but are "Sequence Pseudocolumns".
"Within a single SQL statement containing a reference to NEXTVAL, Oracle increments the sequence once: For each row returned by the outer query block of a SELECT statement. Such a query block can appear in the following places. ..." (emphasis added) [Oracle Database SQL Language Reference, "How to Use Sequence Values"]
In other words, seq.nextval is not a function with a side affect, but a pseudocolumn that has a particular value per row. Once there is a single reference to seq.nextval, the value increments for every row, whether or not the value is used. The outcome OP is seeing is a peculiar to sequences, not case expressions. For example, same thing with decode:
SQL> select decode(deptno
2 , 10, seq.nextval || 'next10'
3 , 20, seq.nextval || 'next20'
4 , 30, seq.currval || 'curr30')
5 from emp;
DECODE(DEPTNO,10,SEQ.NEXTVAL||'NEXT10',20,SEQ.
----------------------------------------------
35next20
36curr30
37curr30
38next20
39curr30
40curr30
41next10
42next20
43next10
44curr30
45next20
46curr30
47next20
48next10
Interesting. Per the Oracle docs:
The statements in a WHEN clause can modify the database and call
non-deterministic functions. There is no fall-through mechanism as in
the C switch statement
Notice it doesn't say the statements in the "true" WHEN clause. So even if the when statement is false, the nextval will fire:
select
case when 1=0 then 'next ' || seq_id.nextval
when 1=1 then 'curr ' || seq_id.currval
end col1
from dual;
I must admit this is different than I expected.
EDIT:
See answer from ShannonSeverance
Related
I have a procedure that throws the error "ORA-01427: single-row subquery returns more than one row". When I use the statemt as a simple SELECT I get 1 row per ID.
I researched the ORA-01427, however I couldn't really apply the answers to my error, since from what I understand I don't have a subquery.
CREATE OR REPLACE PROCEDURE CALC_SLOPE (TBL_NAME IN VARCHAR2) AS
BEGIN
EXECUTE IMMEDIATE 'ALTER TABLE '||TBL_NAME||' ADD (SLOPE_MEDIAN NUMBER(2,2),
SLOPE_75 NUMBER(2,2),
SLOPE_90 NUMBER(2,2))';
EXECUTE IMMEDIATE 'UPDATE '||TBL_NAME||' a1 SET(SLOPE_MEDIAN, SLOPE_75, SLOPE_90)
=(
SELECT ROUND(MEDIAN(b.SLOPE),2) AS SLOPE_MEDIAN,
ROUND(PERCENTILE_CONT(0.75) WITHIN GROUP (ORDER BY b.slope DESC),2) AS SLOPE_75,
ROUND(PERCENTILE_CONT(0.9) WITHIN GROUP (ORDER BY b.slope DESC),2) AS SLOPE_90
FROM '||TBL_NAME||' a2,
bbx_slope b
WHERE SDO_CONTAINS(a2.GEOMETRY, b.POINT) = ''TRUE''
GROUP BY a2.ID
)';
END CALC_SLOPE;
Where lies the reason for the error and how can it be fixed?
I am working on WINDOWS 10 on ORACLE 12c Enterprise Edition.
I think you wanted a correlated update
..
FROM '
|| tbl_name || ' a2 JOIN bbx_slope b
ON SDO_CONTAINS(a2.GEOMETRY, b.POINT) = ''TRUE''
WHERE a2.ID = a1.id --This one
GROUP BY a2.ID
..
Right now You trying to update single row:
SET(SLOPE_MEDIAN, SLOPE_75, SLOPE_90)
with your select result. If your SELECT returns 50 rows, this 50 rows ORACLE tries to put into this single row.
Your missing in WHERE clause some conditions on ID column
I have a checkbox on my page P3_Checkbox1 that is populated from the database. How can I loop through all the checked values of my checkbox in PL/SQL code?
I assume that APEX_APPLICATION.G_F01 is used only by sql generated checkboxes and I cannot use it for regular checbox as it would not populate G_Fxx array.
I think I understand now what you're saying.
For example, suppose that the P3_CHECKBOX1 checkbox item allows 3 values (display/return):
Absent: -1
Unknown: 0
Here: 1
I created a button (which will just SUBMIT the page) and two text items which will display checked values: P3_CHECKED_VALUES and P3_CHECKED_VALUES_2.
Then I created a process which fires when the button is pressed. The process looks like this (read the comments as well, please):
begin
-- This is trivial; checked (selected) values are separated by a colon sign,
-- just like in a Shuttle item
:P3_CHECKED_VALUES := :P3_CHECKBOX1;
-- This is what you might be looking for; it uses the APEX_STRING.SPLIT function
-- which splits selected values (the result is ROWS, not a column), and these
-- values can then be used in a join or anywhere else, as if it was result of a
-- subquery. The TABLE function is used as well.
-- LISTAGG is used just to return a single value so that I wouldn't have to worry
-- about TOO-MANY-ROWS error.
with
description (code, descr) as
(select -1, 'Absent' from dual union all
select 0 , 'Unknown' from dual union all
select 1 , 'Here' from dual),
desc_join as
(select d.descr
from description d join (select * from table(apex_string.split(:P3_CHECKED_VALUES, ':'))) s
on d.code = s.column_value
)
select listagg(j.descr, ' / ') within group (order by null)
into :P3_CHECKED_VALUES_2
from desc_join j;
end;
Suppose that the Absent and Unknown values have been checked. The result of that PL/SQL process is:
P3_CHECKED_VALUES = -1:0
P3_CHECKED_VALUES_2 = Absent / Unknown
You can rewrite it as you want; this is just one example.
Keywords:
APEX_STRING.SPLIT
TABLE function
I hope it'll help.
[EDIT: loop through selected values]
Looping through those values isn't difficult; you'd do it as follows (see the cursor FOR loop):
declare
l_dummy number;
begin
for cur_r in (select * From table(apex_string.split(:P3_CHECKED_VALUES, ':')))
loop
select max(1)
into l_dummy
from some_table where some_column = cur_r.column_value;
if l_dummy is null then
-- checked value does not exist
insert into some_table (some_column, ...) valued (cur_r.column_value, ...);
else
-- checked value exists
delete from some_table where ...
end if;
end loop;
end;
However, I'm not sure what you meant by saying that a "particular combination is in the database". Does it mean that you are storing colon-separated values into a column in that table? If so, the above code won't work either because you'd compare, for example
-1 to 0:-1 and then
0 to 0:-1
which won't be true (except in the simplest cases, when checked and stored values have only one value).
Although Apex' "multiple choices" look nice, they can become a nightmare when you have to actually do something with them (like in your case).
Maybe you should first sort checkbox values, sort database values, and then compare those two strings.
It means that LISTAGG might once again become handy, such as
listagg(j.descr, ' / ') within group (order by j.descr)
Database values could be sorted this way:
SQL> with test (col) as
2 (select '1:0' from dual union all
3 select '1:-1:0' from dual
4 ),
5 inter as
6 (select col,
7 regexp_substr(col, '[^:]+', 1, column_value) token
8 from test,
9 table(cast(multiset(select level from dual
10 connect by level <= regexp_count(col, ':') + 1
11 ) as sys.odcinumberlist))
12 )
13 select
14 col source_value,
15 listagg(token, ':') within group (order by token) sorted_value
16 from inter
17 group by col;
SOURCE SORTED_VALUE
------ --------------------
1:-1:0 -1:0:1
1:0 0:1
SQL>
Once you have them both sorted, you can compare them and either INSERT or DELETE a row.
I'm trying to output a list of the courses a professor teaches, by receiving the prof's id by parameter to my function, and showing all courses, each separated by a comma. For example, if a Professor teaches Humanities, Science and Math, I want the output to be: 'Humanities, Science, Math'. However, I'm getting just 'Math,'. It only shows the last field that it found that matched with the prof's id.
CREATE OR REPLACE FUNCTION listar_cursos(prof NUMBER) RETURN VARCHAR
IS
CURSOR C1 IS
SELECT subject.name AS name FROM subject
INNER JOIN course_semester
ON subject.id = course_semester.id_subject
WHERE course_semester.id_profesor = prof
ORDER BY subject.name;
test VARCHAR(500);
BEGIN
FOR item IN C1
LOOP
test:= item.name ||',';
END LOOP;
RETURN test;
END;
/
I am aware that listagg exists, however I do not wish to use it.
In your loop, you re-assign to the test variable, instead of appending to it. This is why, at the end of the loop, it will just hold the last value of item.name.
The assignment should instead be something like
test := test || ',' || item.name
Note also that this will leave a comma at the beginning of the string. Instead of returning test, you may want to return ltrim(test, ',').
Note that you don't need to declare a cursor explicitly. The code is easier to read (in my opinion) with an implicit cursor, as shown below. I create sample tables and data to test the function, then I show the function code and how it's used.
create table subject as
select 1 id, 'Humanities' name from dual union all
select 2 , 'Science' from dual union all
select 3 , 'Math' from dual
;
create table course_semester as
select 1 id_subject, 201801 semester, 1002 as id_profesor from dual union all
select 2 , 201702 , 1002 as id_profesor from dual union all
select 3 , 201801 , 1002 as id_profesor from dual
;
CREATE OR REPLACE FUNCTION listar_cursos(prof NUMBER) RETURN VARCHAR IS
test VARCHAR(500);
BEGIN
FOR item IN
(
SELECT subject.name AS name FROM subject
INNER JOIN course_semester
ON subject.id = course_semester.id_subject
WHERE course_semester.id_profesor = prof
ORDER BY subject.name
)
LOOP
test:= test || ',' || item.name;
END LOOP;
RETURN ltrim(test, ',');
END;
/
select listar_cursos(1002) from dual;
LISTAR_CURSOS(1002)
-----------------------
Humanities,Math,Science
Can we use more than one column in a single WHEN clause while using CASE in oracle?
A CASE expression can use more than one column in its logic, e.g.
CASE WHEN col1 > val1 AND col2 > val2 THEN 1 ELSE 0 END
But a CASE expression must return a single scalar value.
no you cant, but you can use multi when clause statement like this:
select
case when .... end col_a,
case when .... end col_b
from
xxxx
Of course you can. For example:
select ...
from emp
where case when bonus is null then salary else salary + bonus end > 4000
Here emp is a table, and bonus and salary are two of the columns in that table. The same WHERE clause can be expressed more simply, but regardless of reformulation, it will refer to both columns. (And there will be a CASE of some sort somewhere - NVL and COALESCE are CASE expressions with a different syntax.)
I have a scenario in oracle where i need to be able to reuse the value of a pseudo column which was calculated previously within the same select statement something like:
select 'output1' process, process || '-Output2' from Table1
I don't want to the repeat the first columns logic again in the second column for maintenance purposes, currently it is done as
select 'output1' process, 'output1' || '-Output2' name from Table1
since i have 4 such columns which depend on the previous column output, repeating would be a maintenance nightmare
Edit: i included table name and removed dual, so that no assumptions are made about that this is not a complex process, my actual statement does have 2 to 3 joins on different tables
You could calculate the value in a sub-query:
select calculated_output process, calculated_output || '-Output2' name from
(
select 'output1' calculated_output from dual
)
You can also use the subquery factoring syntax - i think it is more readable myself:
with tmp as
(
select 'output' process from dual
)
select
process, process || '-Output2' from tmp;
very similar to the previous poster, but I prefer the method I show below, it is more readable, thusly more supportable, in my opinion
select val1 || val2 || val3 as val4, val1 from (
select 'output1' as val1, '-output2' as val2, '-output3' as val3 from dual
)