Error PLS-00428: an INTO clause is expected in this SELECT statement - oracle

I just want to show USU_USERS table between a declare procedure but it's shows this error when I try to compile
DECLARE
ID_UNICO VARCHAR(200) := 'H3LP';
BEGIN
DECLARE
VID VARCHAR2(200);
VID_USER VARCHAR2(200);
BEGIN
VID := ID_UNICO;
VID_USER := 'SOPORTE';
USR_SP_USERS.CREATE(
VID => VID,
VID_USER => VID_USER,
);
END;
SELECT * from USU_USERS;
END;
NOTE : THIS TABLE HAS MORE THAN ONE ROWS.

PL/SQL is a server-side language only, and as such there's no way to "show" the results of a SELECT statement anywhere. Your SELECT statement needs to put the values it retrieves from the database into a variable or variables. Let's try to give you something which will work:
DECLARE
strID_UNICO VARCHAR(200) := 'H3LP';
strVID VARCHAR2(200);
strVID_USER VARCHAR2(200);
rowUSU_USERS USU_USERS%ROWTYPE;
BEGIN
strVID := strID_UNICO;
strVID_USER := 'SOPORTE';
USR_SP_USERS.CREATE(VID => strVID,
VID_USER => strVID_USER);
SELECT *
INTO rowUSU_USERS
FROM USU_USERS;
END;
Note that if the table USU_USERS has more than one row in it this code will fail. Assuming, though, that USU_USERS has only a single row in it, you could then use DBMS_OUTPUT.PUT_LINE to output the various fields in rowUSU_USERS.

Related

Oracle read and return data from cursor

I'm trying to create a stored procedure in Oracle that has one input parameter and one output variable and, in case of results, return a dataset to my .Net Application. The main issue is that I can't change the signature of the procedure and need to do if condition to validate if exist records or not.
The main issue that i've being struggled is with cursors (to execute and return the information), and to count the results of the select.
Here is an example of what i'm doing to try to retrieve the data.
CREATE PROCEDURE SP_Testing (v_input IN VARCHAR2(50), v_OutID NUMBER(1))
AS
TYPE v_record_botoes IS RECORD (
v_dummy_col1 VARCHAR2(50),
v_dummy_col2 VARCHAR2(250)
);
TYPE table_botoes IS TABLE OF v_record_botoes;
tt_botoes table_botoes;
v_ref_cursor SYS_REFCURSOR;
CURSOR v_cursor IS
(SELECT dt.v_dummy_col1,
dt.v_dummy_col2
FROM dummy_table dt
WHERE v_dummy_col3 = v_input);
v_check NUMBER;
BEGIN
tt_botoes := table_botoes();
v_check := 0;
FOR v_row IN v_cursor
LOOP
tt_botoes.extend;
tt_botoes(tt_botoes.COUNT) := v_row;
END LOOP;
v_check := tt_botoes.COUNT;
-- condition that need to know the nr of records of the select
IF v_check = 0 THEN
v_OutID := 0;
ELSE
v_OutID := 1;
OPEN v_ref_cursor FOR
SELECT *
FROM tt_botoes; -- also tryed "FROM TABLE (tt_botoes)" and "FROM TABLE (cast(tt_botoes AS table_botoes))"
-- return dataset to .net application
DBMS_SQL.RETURN_RESULT(v_ref_cursor)
END IF;
END;
Already tryed to convert the v_cursor into a sys_refcursor to be outputed by the DBMS_SQL package but didn't get anywhere.
Also i've tried to create a temporary table to hold the information, but then have a concurrency issue.
Any idea what i'm doing wrong, or any other possible solution to solve this issue?
Thanks in advance
Completely untested because I do not have the environment to test it but I would structure this quite differently, see below (I am assuming that the missing out in the specification is just a typo)
CREATE PROCEDURE SP_Testing (v_input IN VARCHAR2(50), v_OutID **out** NUMBER(1))
AS
v_ref_cursor SYS_REFCURSOR;
v_check NUMBER;
BEGIN
select count(1)
into v_check
from dummy_table dt
WHERE v_dummy_col3 = v_input
-- condition that need to know the nr of records of the select
IF v_check = 0 THEN
v_OutID := 0;
ELSE
v_OutID := 1;
OPEN v_ref_cursor FOR
SELECT dt.v_dummy_col1,
dt.v_dummy_col2
FROM dummy_table dt
WHERE v_dummy_col3 = v_input
-- return dataset to .net application
DBMS_SQL.RETURN_RESULT(v_ref_cursor)
END IF;
END;

Oracle Where with variable

I have problem with my plsql code. It's just part of my whole job.
declare
id number(2):=1; (here is function which returns any value)
check VARCHAR2(100);
begin
select COUNT(*) into check from T_SDSN_LOG Where ANY_ID=id AND CHECK LIKE
'NAME';
dbms_output.put_line(check);
end;
In this case, my select returns 0 althought it should be 2.
If I change the part
Where ANY_ID=id to
Where ANY_ID=2 it works perfectly. Any advices? I need id to be variable as a return value from function.
This uses a locally defined function so it isn't available in the SQL but can be referenced in the PL/SQL.
DECLARE
lnum_id NUMBER := return_id;
lnum_check VARCHAR2(100);
FUNCTION return_id
RETURN NUMBER
IS
BEGIN
RETURN 123456;
END;
BEGIN
lnum_id := return_id;
SELECT COUNT(*)
INTO lnum_check
FROM my_table
WHERE table_id = lnum_id;
DBMS_OUTPUT.put_line(lnum_check);
END;
You will presumably want this functionality in a package in which case you can declare the function in the package header, write the code for the function in the body and then reference it in the SQL itself. So if I declare a function (FNC_RETURN_ID) in a package called PKG_DATA_CHECKS that returns a NUMBER I can do the following;
DECLARE
lnum_id NUMBER;
lnum_check VARCHAR2(100);
BEGIN
SELECT COUNT(*)
INTO lnum_check
FROM my_table
WHERE table_id = (SELECT pkg_data_checks.fnc_return_id FROM dual);
DBMS_OUTPUT.put_line(lnum_check);
END;

Why am i returning an INVALID identifier when trying to COMPILE this TRIGGER?

So I am trying to compile this trigger that prints some lines as outputs ( have the variables declared because i will be building onto this trigger using these variables later on). There's an existing trigger in the same database that does the same exact thing with a different table passing the same data, but my trigger seems to throw an error saying that 'new.PROCESSED is an invalid identifier'. What am I doing wrong? I may just not know what is fully going on here in my code... (thanks in advance!) value_value_id_se and cALCULATION_VALUE_CALCULATI329 are both functions in the system...
create or replace TRIGGER CAL_VAL
AFTER INSERT
ON XML_HOURS_LOAD
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
WHEN (
NEW.processed = 'N'
)
DECLARE
Value_ID Number;
pValue_ID Number;
pCalculation_ID NUMBER;
Calculation_ID Number;
Calculation_Value_ID Number;
p_Entity_Address_ID Varchar2(50);
New_Value_ID Number;
New_Calculation_ID Number;
New_Calculation_Value_ID Number;
BEGIN
Value_ID := value_value_id_seq.NEXTVAL;
New_Value_ID := Value_id ;
Calculation_Value_ID:=CALCULATION_VALUE_CALCULATI329.NEXTVAL;
calculation_id := Calculation_Calculation_ID_SEQ.NEXTVAL;
p_Entity_Address_ID := :New.EIA_ID_TX;
DBMS_OUTPUT.PUT_LINE(Get_energy_product_id(:NEW.Product_Name_Cd));
DBMS_OUTPUT.PUT_LINE(Get_Data_Source_Id(:NEW.Data_Source_Tx));
DBMS_OUTPUT.PUT_LINE(Get_Supply_Type_Id(:NEW.Supply_Type_Tx));
DBMS_OUTPUT.PUT_LINE(Get_State_CD(Get_entity_Id(p_Entity_Address_ID)));
DBMS_OUTPUT.PUT_LINE(Get_Entity_Address_ID(Get_Entity_ID(p_Entity_Address_ID)));
DBMS_OUTPUT.PUT_LINE('Value_ID' || Value_ID);
END;

PLSQL CLOBS into variables

I'm trying to save a CLOB into a variable to perform operations like extract and such. I have this code:
DECLARE
clob_rec CLOB;
n_rec NUMBER:=100;
BEGIN
SELECT LOB INTO clob_rec FROM table1 WHERE ID = 1234;
n_rec := clob_rec.EXTRACT('//XPTO/text()', 'xmlns:XPTO').getNumVal();
END;
I want to save multiple values from the XML to various variables like n_rec. How can get an "instance of the object (CLOB)" to perform functions or methods like extract()?
You need to convert it to an XMLtype first:
DECLARE
clob_rec CLOB;
n_rec NUMBER:=100;
x XMLType;
BEGIN
SELECT LOB INTO clob_rec FROM table1 WHERE ID = 1234;
x := XMLType(clob_rec);
n_rec := x.EXTRACT('//XPTO/text()', 'xmlns:XPTO').getNumVal();
END;

Error when comparing values in Oracle

I write simple procedure.
DECLARE
connection_id LINE.CONNECTION_ID%TYPE := 11009;
tmp_integer INTEGER;
BEGIN
SELECT COUNT(*) INTO tmp_integer FROM LINE WHERE LINE.CONNECTION_ID = 11009;
DBMS_OUTPUT.PUT_LINE(connection_id);
DBMS_OUTPUT.PUT_LINE(tmp_integer);
END;
Result of the launch:
11009
3
It is good result. I have only 3 rows where CONNECTION_ID is 11009.
After modification:
DECLARE
connection_id LINE.CONNECTION_ID%TYPE := 11009;
tmp_integer INTEGER;
BEGIN
SELECT COUNT(*) INTO tmp_integer FROM LINE WHERE LINE.CONNECTION_ID = connection_id;
DBMS_OUTPUT.PUT_LINE(connection_id);
DBMS_OUTPUT.PUT_LINE(tmp_integer);
END;
But in this case I gain strange result:
11009
30997
Where is the mistake?
Try changing the name of your PL/SQL variable to be different from the column name in the table, e.g. v_connection_id.

Resources