This question already has answers here:
Using variables in PLSQL SELECT statement
(2 answers)
Closed 5 years ago.
What's the basic syntax for a pl/sql variable? I'm trying to add a variable in my select statement.
My query:
DECLARE
V_id number := 251 ;
BEGIN
SELECT *
FROM client
where 1=1
and clientid = V_id;
END;
Error:
PLS-00428 an INTO clause is expected in this Select statement
This example may work for you. Please check it out
DECLARE
V_Num NUMBER(15,0) := 150;
V_Col VARCHAR2(50);
BEGIN
SELECT col1 into V_col FROM tableA where id = V_num;
END ;
Related
This question already has answers here:
if (select count(column) from table) > 0 then
(3 answers)
Closed 7 months ago.
I have a table that has a column of years as values.
How would I construct an If statement within a stored procedure to check if a record exists that starts with the current YYYY? If there is no record, I will be dropping a sequence.
if (record exists LIKE SYSDATE(YYYY))
Continue
else
DROP MY_SEQ
Here's one option:
declare
l_cnt number;
begin
select count(*)
into l_cnt
from your_table
where extract (year from date_column) = extract (year from sysdate)
and rownum = 1;
if l_cnt > 0 then
-- do something
else
execute immediate 'drop sequence my_seq';
end if;
end;
This question already has answers here:
How to reverse a string in Oracle (11g) SQL without using REVERSE() function
(5 answers)
A procedure to Reverse a String in PL/SQL
(7 answers)
Closed 10 months ago.
i need help with this question, Using PL/SQL how can I read a string backwards using the while loop?
I know that with the for loop there is a method called reverse but I don't know how I can do it with the while loop. If you can help me I appreciate it
Here's one option:
SQL> set serveroutput on
SQL>
SQL> declare
2 l_str varchar2(20) := 'Littlefoot';
3 i number;
4 retval varchar2(20);
5 begin
6 i := length(l_str);
7 while i <> 0 loop
8 retval := retval || substr(l_str, i, 1);
9 i := i - 1;
10 end loop;
11 dbms_output.put_line(retval);
12 end;
13 /
toofelttiL
PL/SQL procedure successfully completed.
SQL>
If it doesn't have to be PL/SQL, hierarchical query along with listagg might be another way to do it:
SQL> var l_str varchar2(20)
SQL> exec :l_str := 'Littlefoot';
PL/SQL procedure successfully completed.
SQL> select listagg(substr(:l_str, length(:l_str) - level + 1, 1), '') within group (order by level) result
2 from dual
3 connect by level <= length(:l_str);
RESULT
--------------------------------------------------------------------------------
toofelttiL
SQL>
This question already has an answer here:
oracle sql if condition then select statement1 else select statement2
(1 answer)
Closed 3 years ago.
I tried to execute the plsql IF statement based on the condition. But it does not work.
declare
admin varchar2(4000);
user1 varchar2(4000);
begin
Admin := q'~
SELECT * INTO ADMIN FROM CI_PREFERENCE ;
~';
User1 :='~
SELECT * FROM CI_PREFERENCE WHERE EMAIL_ID = lower(:APP_USER);
~';
if :G_ADMIN = 'A' then
return admin;
else
return user1;
end if;
end;
It Shows:
ORA-20001: Query must begin with SELECT or WITH error
It looks like your code is missing the Q for the alternative quoting syntax. Replace
User1 :='~
SELECT * FROM CI_PREFERENCE WHERE EMAIL_ID = lower(:APP_USER);
~';
with
User1 := q'~
SELECT * FROM CI_PREFERENCE WHERE EMAIL_ID = lower(:APP_USER);
~';
This question already has answers here:
SELECT from table with Varying IN list in WHERE clause
(2 answers)
Closed 5 years ago.
I am passing an input value as 'MI,NOKIA,APPLE' to a procedure. This input value has to be passed to the query in where condition.
For example:
create or replace procedure abc123(p_name varchar2)
is
v_val number;
begin
dbms_output.put_line ('p_name: '||p_name );
Select 1
Into v_val
from MYTABLE
where Model in p_name;
dbms_output.put_line ('v_val: '||v_val );
end;
This is not working. How do I pass the multiple input values as single parameter?
Use paranthesis after IN operator :
Select 1 Into v_val from MYTABLE where Model in (p_name);
try below code,
create or replace procedure proc123 (p_param VARCHAR2)
IS
TYPE cur_typ IS REF CURSOR;
c cur_typ;
v_query VARCHAR2(200) := 'SELECT * '||
'FROM (SELECT ''Model1'' model FROM dual '||
'union all '||
'SELECT ''Model2'' FROM dual '||
'union all '||
'SELECT ''Model3'' FROM dual)';
v_model VARCHAR2(20);
BEGIN
v_query := v_query||' WHERE INSTR('''||p_param||''',model) > 0';
dbms_output.put_line(v_query);
OPEN c FOR v_query;
LOOP
FETCH c INTO v_model;
EXIT WHEN c%NOTFOUND;
dbms_output.put_line(v_model);
END LOOP;
CLOSE c;
END;
/
--set your dbms_output on
begin
proc123('Model1, Model2');
end;
/
if you want to pass the parameter in a query, then you can use the INSTR function
Select 1
from MYTABLE
where INSTR(p_name, model) > 0;
This question already has answers here:
Create user from string variables in a PL/SQL block
(1 answer)
Why can't I write DDL directly after a PLSQL anonymous block?
(1 answer)
Closed 7 years ago.
What's going on here?
DECLARE
V_COUNT NUMBER(10) := 0;
BEGIN
SELECT COUNT(*) INTO V_COUNT FROM USER_VIEWS WHERE VIEW_NAME = 'DBO$EVT_APP';
IF V_COUNT > 0 THEN
DROP VIEW DBO$EVT_APP;
END IF;
END;
I'm getting the following error:
Error report:
ORA-06550: line 9, column 5:
PLS-00103: Encountered the symbol "DROP" when expecting one of the following: ...
This looks like valid SQL to me. This is my exact code, pasted.
DECLARE
V_COUNT NUMBER(10) := 0;
BEGIN
SELECT COUNT(*) INTO V_COUNT FROM USER_VIEWS WHERE VIEW_NAME = 'DBO$EVT_APP';
IF V_COUNT > 0 THEN
execute immediate 'DROP VIEW DBO$EVT_APP';
END IF;
END;