Update:
I want to insert data conditionally
Why this MariaDB query has errors:
if 2 > 1 then
select 'hi'
else
select 'bye'
end if
This is the error:
Error in query (1064): Syntax error near 'else select 'bye' end if' at line 3
if (exists (select * from teachers where name = 'Jennifer'))
then
-- do nothing
else
insert into teachers (name, age)
values ('Jennifer', 30)
end if;
I'm not sure that IF can be used like this outside of a stored procedure or function. You may use the IF() function or a CASE expression instead:
SELECT IF(2 > 1, 'hi', 'bye')
OR
SELECT CASE WHEN 2 > 1 THEN 'hi' ELSE 'bye' END
Related
My code:
create table info(str varchar2(30));
declare
cursor c(job emp_ast.job_id%type, dep emp_ast.department_id%type) is select employee_id
from emp_ast
where job_id=job and department_id=dep;
type t_job is table of emp_ast.job_id%type;
t t_job:=t_job();
emp emp_ast.employee_id%type;
i number(3);
begin
select job_id
bulk collect into t
from emp_ast;
for i in 10..270 loop
for j in 1..t.count loop
open c(i, t(j));
loop
fetch c into emp;
insert into info
values (i||' '||t(j)||' '||emp);
exit when c%notfound;
end loop;
i:=i+10;
end loop;
end loop;
end;
/
I get "expression 'I' cannot be used as an assignment target", reffering to the line where I increment i by 10. I am trying to save the department_id, employee_id and job_id as a string in a table for each department and each job.
At the point where you get that message, i refers to the loop control variable i defined in the line for i in 10..270 loop, not the int(3) variable defined earlier. In PL/SQL a loop definition defines a variable which is only accessible inside the loop, and which you cannot alter. I suggest you change the name of one or the other to make them unique.
EDIT
PL/SQL doesn't provide a way to step by more than 1 in a computed FOR loop. Instead, you will need to compute the desired department number value within the loop:
DECLARE
CURSOR c(job EMP_AST.JOB_ID%TYPE,
dep EMP_AST.DEPARTMENT_ID%TYPE)
IS SELECT EMPLOYEE_ID
FROM EMP_AST
WHERE JOB_ID = job AND
DEPARTMENT_ID = dep;
TYPE t_job IS TABLE OF EMP_AST.JOB_ID%TYPE;
t t_job := t_job();
emp EMP_AST.EMPLOYEE_ID%TYPE;
nDepartment NUMBER;
BEGIN
SELECT job_id
BULK COLLECT INTO t
FROM EMP_AST;
FOR i IN 1..27 LOOP
nDepartment := i * 10;
FOR j IN 1..t.COUNT LOOP
OPEN c(t(j), nDepartment);
LOOP
FETCH c INTO emp;
INSERT INTO info
VALUES (nDepartment || ' ' || t(j) || ' ' || emp);
EXIT WHEN c%notfound;
END LOOP; -- cursor c
CLOSE c;
END LOOP; -- j
END LOOP; -- i
END;
/
Note that in the code above the nDepartment value is computed within the i loop, which now increments from 1 to 27 instead of going from 10 to 270.
Im trying to learn PL/SQL. I am struck with this code. Please notify me where im going wrong. I use Oracle 10g in a command line .
declare
grade char(1):='&v_grade';
app varchar(15);
begin
app:=case v_grade
when 'a' then
dbms_output.put_line('Excellent');
when 'b' then
dbms_output.put_line('Good');
else
dbms_output.put_line('Bad');
end case;
dbms_output.put_line('Grade'||grade||' Appraisal:'||app);
end;
/
It shows
Enter value for v_grade: a
old 2: grade char(1):='&v_grade';
new 2: grade char(1):='a';
dbms_output.put_line('Excellent');
*
ERROR at line 7:
ORA-06550: line 7, column 34:
PLS-00103: Encountered the symbol ";" when expecting one of the following:
. ( * % & = - + < / > at else end in is mod remainder not rem
when <an exponent (**)> <> or != or ~= >= <= <> and or like
LIKE2_ LIKE4_ LIKEC_ between || multiset member SUBMULTISET_
The symbol ";" was ignored.
ORA-06550: line 9, column 29:
PLS-00103: Encountered the symbol ";" when expecting one of the following:
. ( * % & = - + < / > at else end in is mod remainder not rem
when <an exponent (**)> <> or != or ~= >= <= <> and or like
LIKE2_ LIKE4_ LIKEC_ between ||
ORA-06550: line 11, column 28:
PLS-00103: Encountered the symbol ";" when expecting one of the following:
. ( * % & = - + < / > at end in is mod remainder not rem
<an exponent (**)> <> or != or ~= >= <= <> and or like LIKE2_
LIKE4_ LIKEC_ between || multiset
please notify me where i am going wrong.
T.J. Crowder is sort of right, you shouldn't have a semicolon within a case statement in SQL, but this is the PL/SQL version so it's slightly different.
You are currently trying to assign the (non-existent) return from the dbms_output.put_line procedure (not function) call to your app variable.
For the assignment to work you need the case to evaluate to a string, so you can just use text literals; but the assignment needs to be in each branch:
declare
grade char(1):='&v_grade';
app varchar(15);
begin
case grade
when 'a' then app:= 'Excellent';
when 'b' then app:= 'Good';
else app := 'Bad';
end case;
dbms_output.put_line('Grade '||grade||' Appraisal: '||app);
end;
/
Which when run gets:
Enter value for v_grade: a
Grade b Appraisal: Excellent
PL/SQL procedure successfully completed.
Or use a query to do the assignment, though this is not quite as efficient:
declare
grade char(1):='&v_grade';
app varchar(15);
begin
select case grade
when 'a' then 'Excellent'
when 'b' then 'Good'
else 'Bad'
end case
into app
from dual;
dbms_output.put_line('Grade '||grade||' Appraisal: '||app);
end;
/
Enter value for v_grade: b
Grade b Appraisal: Good
PL/SQL procedure successfully completed.
Or you can do the output directly in each branch:
declare
grade char(1):='&v_grade';
begin
dbms_output.put('Grade '||grade||' Appraisal: ');
case grade
when 'a' then
dbms_output.put_line('Excellent');
when 'b' then
dbms_output.put_line('Good');
else
dbms_output.put_line('Bad');
end case;
end;
/
Enter value for v_grade: c
Grade c Appraisal: Bad
PL/SQL procedure successfully completed.
The first part of your output is before the case now.
You're basically mixing up the different approaches.
You might want to change the first line to:
grade char(1):= upper('&v_grade');
... and then make the case look for A,B,C instead of a,b,c - then it won't matter what case the input is in.
You can read more about the PL/SQL case statemnt here and here.
I have a table "flags" containing flags with "name" and "value" where "name" is the name of the flag and "value" is either 'T' or 'F'
I would like to update a table based on a flag with name 'enable_feature' in the following manner:
BEGIN;
IF ((SELECT flags.value FROM flags WHERE flags.name = 'enable_feature') = 'T')
UPDATE... SET...;
ELSE
UPDATE... SET...;
END IF;
END;
My trouble seems to be in the IF statement. Specifically, i get the following error:
PLS-00103: Encountered the symbol "SELECT" when expecting one of the
following:
( - + case mod new not null continue avg count current
exists max min prior sql stddev sum variance execute forall merge
time timestamp interval date pipe
How do I modify this statement such that I can perform an UPDATE/SET statement based on the value of 'enable_feature' (The UPDATE/SET statement is on a different table)
DECLARE
v flags.value%type;
BEGIN
SELECT flags.value into v FROM flags WHERE flags.name = 'enable_feature';
IF v = 'T' THEN
UPDATE... SET...;
ELSE
UPDATE... SET...;
END IF;
END;
Is it possible to do something like this in PL/SQL in 10g?
if user_is_goat = 1 then
for item_rec in (select * from pricing_for_goats)
else
for item_rec in (select * from pricing_for_non_goats)
end if;
loop
.
.
end loop;
It seems that when oracle sees "for rec in select * from dual" it expects "loop" to immediate follow. My code in the loop is many lines and I don't want to have to maintain 2 copies of it.
Try query below, this will check if variable user_is_goat = 1 and returns data from for_goats else it will return from for_non_goats
for item_rec in
(
select * from pricing_for_goats where user_is_goat = 1
union
select * from pricing_for_non_goats where user_is_goat <> 1
)
loop
.....
.....
end loop;
Oracle SQL Developer complains about next SQL though I can't seem to find the reason:
IF to_number(to_char(sysdate, 'HH24')) > 6 THEN
IF to_number(to_char(sysdate, 'HH24')) < 9 THEN
SELECT 1 FROM dual;
ELSE
SELECT (CASE WHEN result = 'SUCCESS' THEN 1 ELSE 0 END) FROM t_job WHERE to_char(start_time, 'yyyy/mm/dd') = to_char(sysdate, 'yyyy/mm/dd');
END IF;
ELSE
SELECT (CASE WHEN result = 'SUCCESS' THEN 1 ELSE 0 END) FROM t_job WHERE to_char(start_time, 'yyyy/mm/dd') = to_char(sysdate, 'yyyy/mm/dd');
END IF;
What's the mistake in provided query?
Error report:
Error starting at line 7 in command:
ELSE
Error report:
Unknown Command
(CASEWHENRESULT='SUCCESS'THEN1ELSE0END)
---------------------------------------
1
Error starting at line 9 in command:
END IF
Error report:
Unknown Command
There are a couple of problems with your code (which is PL/SQL, not just SQL):
1) You are missing the begin and end around the block.
2) Your selects need an into clause
try:
DECLARE
l_result number;
BEGIN
IF to_number(to_char(sysdate, 'HH24')) > 6 THEN
IF to_number(to_char(sysdate, 'HH24')) < 9 THEN
SELECT 1 INTO l_result FROM dual;
ELSE
SELECT (CASE WHEN result = 'SUCCESS' THEN 1 ELSE 0 END)
INTO l_result
FROM t_job WHERE to_char(start_time, 'yyyy/mm/dd') = to_char(sysdate, 'yyyy/mm/dd');
END IF;
ELSE
SELECT (CASE WHEN result = 'SUCCESS' THEN 1 ELSE 0 END)
INTO l_result
FROM t_job WHERE to_char(start_time, 'yyyy/mm/dd') = to_char(sysdate, 'yyyy/mm/dd');
END IF;
dbms_output.put_line('result is '||l_result);
END;
Since this is a PL/SQL block, your SELECT statements would need to select the data into some local variable or they would need to be used in a cursor. Which approach you want would depend on how many rows in T_JOB could potentially match the criteria you're specifying. Assuming all three statements would return exactly 1 row, you could do something like this (code simplified to avoid repeating the same query twice)
DECLARE
l_some_local_variable PLS_INTEGER;
BEGIN
IF( to_number( to_char( sysdate, 'HH24' ) ) > 6 and
to_number( to_char( sysdate, 'HH24' ) ) < 9 )
THEN
SELECT 1
INTO l_some_local_variable
FROM dual;
ELSE
SELECT (CASE WHEN result = 'SUCCESS'
THEN 1
ELSE 0
END)
INTO l_some_local_variable
FROM t_job
WHERE trunc( start_time ) = trunc( sysdate );
END IF;
END;
Of course, once you populate the data in your local variable, you would need to actually do something with the value. Potentially, you may want to create a function that returns the local variable rather than using an anonymous PL/SQL block as I have done here.