PLS-00201 Error when running stored procedure in Oracle - database

I Recently moved on oracle database for one of my project. I have created a stored procedure for selecting multiple rows from database. Following is my procedure
create Or replace
PROCEDURE TEST(p_cursor OUT SYS_REFCURSOR) AS
BEGIN
open p_cursor FOR select * from branch_info;
END TEST;
when I execute this procedure I got following error:
*
ERROR at line 1:
ORA-06550: line 1, column 7:
PLS-00201: identifier 'SAURAV.TEST' must be declared
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
I have searched for it and found similar question here but error line and reason are different.
Anyone please help me in solving this.
EDIT: Misprint TEST with TEXT

The problem is with keyword test
CREATE OR REPLACE PROCEDURE test (p_cursor OUT sys_refcursor)
AS
BEGIN
OPEN p_cursor FOR
SELECT *
FROM branch_info;
END test;
and execute by
variable rc refcursor;
exec test( :rc );
print rc;
ORA-06550: line 1, column 7:
PLS-00201: identifier 'TEST' must be declared
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
Change to some other name
CREATE OR REPLACE PROCEDURE test2 (p_cursor OUT sys_refcursor)
AS
BEGIN
OPEN p_cursor FOR
SELECT *
FROM branch_info;
END test2 ;
execute as
variable rc refcursor;
exec test2 ( :rc );
print rc;
PL/SQL procedure successfully completed.
From sql plus
SQL> variable usercur refcursor;
SQL> DECLARE
2 BEGIN
3 test2(:usercur);
4 end;
5 /
PL/SQL procedure successfully completed.
SQL> print usercur;

Related

SQL Server function error with if condition

I'm still trying to learn an SQL, i made a mistakes but i already search on the internet about the if statements...
I tried to create a simple function to check if the parameter is match the condition..
I have 2 parameters, that will be inputted manually by the users, but i got an error saying
Msg 156, Level 15, State 1, Procedure ufn_calculatebonus, Line 4 [Batch Start Line 1] Incorrect syntax near the keyword 'IF'. Msg 178, Level 15, State 1, Procedure ufn_calculatebonus, Line 10 [Batch Start Line 1] A RETURN statement with a return value cannot be used in this context.
This is the code i've tried to create, i thought i make the IF condition right? After the IF , should have a BEGIN and statements, closing it with END. ??
CREATE FUNCTION Sales.ufn_calculatebonus (#CompanyRevenue int, #OptionalParameter varchar(60))
RETURNS TABLE
AS
IF #CompanyRevenue > 10
BEGIN
SELECT 'INPUT CANNOT BE BIGGER THAN 1000'
END
ELSE
BEGIN
RETURN
(
SELECT * FROM SALES.CUSTBALANCE2 where region=#OptionalParameter or name=#OptionalParameter
);
END
GO
There are 2 distinct types of T-SQL table-valued functions, inline TVF and multi-statement TVF. The body of an inline TVF consists of only of a RETURNS statement with a query specification (not to be confused with a RETURN statement).
Your attempt at a multi-statement table-valued function is invalid for the reasons below.
The body of a multi-statement TVF must be surrounded with BEGIN...END.
A table variable with the returned schema must be declared in the function header.
One cannot return query results with a RETURN statement. Instead, insert results into the table variable declared in the function header end specify RETURN at the end of the function to return the results.
Below is an example of the remediated code. Importantly, note the use of an explicit column list throughout. Don't SELECT * in production code.
CREATE OR ALTER FUNCTION dbo.ufn_calculatebonus (#CompanyRevenue int, #OptionalParameter varchar(60))
RETURNS #BalanceInfo TABLE (CustomerID int, Balance int, ErrorMessage varchar(100))
AS
BEGIN
IF #CompanyRevenue > 10
BEGIN
INSERT INTO #BalanceInfo (ErrorMessage) VALUES('INPUT CANNOT BE BIGGER THAN 1000');
END
ELSE
BEGIN
INSERT INTO #BalanceInfo (CustomerID, Balance) --note ErrorMessage is omitted and will be NULL
SELECT CustomerID, Balance FROM SALES.CUSTBALANCE2 where region=#OptionalParameter or name=#OptionalParameter;
END;
RETURN;
END
GO
A stored procedure is a better fit than a than user-defined function if you need to raise custom errors for parameter validation. Here's a stored procedure example:
CREATE OR ALTER PROCEDURE dbo.usp_calculatebonus (#CompanyRevenue int, #OptionalParameter varchar(60))
AS
IF #CompanyRevenue > 10
BEGIN
THROW 50000, 'INPUT CANNOT BE BIGGER THAN 1000', 16;
END;
SELECT CustomerID, Balance FROM SALES.CUSTBALANCE2 where region=#OptionalParameter or name=#OptionalParameter;
GO

how to call variable result into another select statment in sql stored procedure

Hi I have one doubt in snowflake
how to use variable result in the next sselect session.
I want stored one statement result in variable and that variable result need to call another statment in snowflake server .
use schema test.public;
create or replace procedure empresult()
returns table ()
language sql
as
$$
set empresult=(select deptid from TEST.PUBLIC.DEPT where deptid=10)
declare res resultset(
select * from emp where deptno in ($empresult)
)
begin
return table(res);
end;
$$;
above statment getting error.
Syntax error: unexpected 'declare'. (line 4)
please tell me how to write stored procedure query to call the one statment result into another session select statment task.
The sample code is missing colons and the correct syntax for variables.
One possible fix that works:
create or replace procedure empresult()
returns table ()
language sql
as
$$
declare
empresult integer;
begin
select deptid into :empresult from DEPT where deptid=10;
let res resultset := (select * from emp where deptno = :empresult);
return table(res);
end;
$$;
call empresult();
Staging data:
create temp table dept as select 10 deptid;
create temp table emp as select 'a' a, 'b' b, 10 deptno;

PLS-00201: identifier 'IMMEDIATE' must be declared when using EXECUTE IMMEDIATE?

I'm trying to use execute immediate to create a table as follows :
EXECUTE IMMEDIATE (q'{CREATE TABLE ... ;}');
However oracle gives me the following error:
ORA-06550: line 1, column 7:
PLS-00201: identifier 'IMMEDIATE' must be declared
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
how do i correctly format my string to create my table?
You cannot run ddl operation using dynamic sql with sqlplus.
Test case to replicate the error
SQL> EXECUTE IMMEDIATE (q'{CREATE TABLE test1000(id int,name varchar2(10)}');
BEGIN IMMEDIATE (q'{CREATE TABLE test1000(id int,name varchar2(10)}';); END;
*
ERROR at line 1:
ORA-06550: line 1, column 7:
PLS-00201: identifier 'IMMEDIATE' must be declared
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
Since it doesn't work in sqlplus let's switch to anonymous pl/sql block
SQL> BEGIN
2 EXECUTE IMMEDIATE (q'{CREATE TABLE test1000(id int,name varchar2(10));}');
3* END;
SQL> /
BEGIN
*
ERROR at line 1:
ORA-00922: missing or invalid option
ORA-06512: at line 2
Remove colon in curly brackets to avoid above error
SQL> BEGIN
2 EXECUTE IMMEDIATE (q'{CREATE TABLE test1000(id int,name varchar2(10))}');
3* END;
PL/SQL procedure successfully completed.
SQL> #ddl test1000
PL/SQL procedure successfully completed.
DBMS_METADATA.GET_DDL(OBJECT_TYPE,OBJECT_NAME,OWNER)
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CREATE TABLE "HR"."TEST1000"
( "ID" NUMBER(*,0),
"NAME" VARCHAR2(10) COLLATE "USING_NLS_COMP"
) DEFAULT COLLATION "USING_NLS_COMP" SEGMENT CREATION DEFERRED
PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255
NOCOMPRESS LOGGING
TABLESPACE "EXAMPLE" ;

Copy column from one table to another table with condition

ALTER PROCEDURE [dbo].[Timesheet_update]
AS
BEGIN
DECLARE #sql2 nvarchar(max), #status nvarchar(1)
SET #sql2 = 'insert into s21022020 (s21_stfno) select m_stfno from master where m_status<>'D''
EXECUTE (#sql2)
END
EXECUTE Timesheet_update
Results in an error:
Msg 207, Level 16, State 1, Line 23
Invalid column name 'D'.
m_status column contain data =D
I don't understand why you feel the need to make this a dynamic SQL - just write the statement directly inside the stored procedure - like this:
ALTER PROCEDURE [dbo].[Timesheet_update]
AS
BEGIN
INSERT INTO s21022020 (s21_stfno)
SELECT m_stfno
FROM master
WHERE m_status <> 'D'
END

Implementing IF statement on Oracle Database

Im trying to implement an IF statement to run a query on my Oracle Database.
The expectation:
If the employee is called John, display the limited items(item1 and item2), if it's not John, display the complete list.
DECLARE
employeeName STRING := 'John';
BEGIN
IF (employeeName = 'John') THEN
       (SELECT TableA.Table from Table where TableA.Table = 'Item1' OR TableA.Table = 'Item2');
ELSE 
       (SELECT TableA.Table from Table);
END IF;
END;
Here's the error output:
Error report: ORA-06550: line 6, column 2: PLS-00103: Encountered the symbol " " when expecting one of the following:
( begin case declare exit for goto if loop mod null pragma raise
return select update while with 'an identifier' 'a double-quoted
delimited-identifier' 'a bind variable' ""continue close current
delete fetch lock insert open rollback savepoint set sql execute
commit forall merge pipe purge
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
What am I doing wrong? Forgive my ignorance on IF statements implemented on databases.
In your code,
table is a reserved keyword. Use double quotes or a different identifier
select is missing into.
String is missing length
You can apply your condition in a single SQL and then, use a for loop to loop over the result set.
DECLARE
employeeName varchar2(100) := 'John';
BEGIN
for t in (select "table" from tablea
where "table" in ('item1', 'item2')
or employeename = 'john') loop
dbms_output.put_line(t."table");
end loop;
END;
/

Resources