I know I am an idiot.. But I have been trying to create this simple stored procedure in my Oracle database for some time now and I keep getting the error "procedure created with compilation errors". I can't seem to find anything wrong with it and I am following the syntax I have found online. I am using an Oracle xe 11g server with pl/sql 11. Please help!
CREATE OR REPLACE PROCEDURE hr.countEmployee(passin IN NUMBER)
IS
BEGIN
SELECT COUNT(*) FROM hr.mitch_employee_motors WHERE hr.mitch_employee_motors.deptno = hr.countemployee.passin;
END;
Aleksej hit the nail on the head and I follow up with the code that reflects his comment.
CREATE OR REPLACE PROCEDURE hr.countemployee (passin IN NUMBER)
IS
l_count INTEGER;
BEGIN
SELECT COUNT (*) INTO l_count
FROM hr.mitch_employee_motors
WHERE hr.mitch_employee_motors.deptno = hr.countemployee.passin;
DBMS_OUTPUT.PUT_LINE (l_count);
END;
What are you using the write and execute your SQL and PL/SQL? I encourage you to try out SQL Developer. It would have helped you a whole lot in diagnosing the issue.
You can try:
CREATE OR REPLACE PROCEDURE hr.countEmployee(passin IN NUMBER,OUT_CURSOR OUT sys_refcursor)
IS
BEGIN
OPEN OUT_CURSOR FOR
SELECT COUNT(*) FROM hr.mitch_employee_motors WHERE hr.mitch_employee_motors.deptno = hr.countemployee.passin;
END;
Related
I am new to oracle.So,I am creating a procedure which has all features of insert,update,delete and select method.So when I hit the compilation button then the result is:
Warning: compiled but with compilation errors
But i am unable to see where is the mistake I did.My table is:
Now the procedure I tried is:
CREATE OR REPLACE PROCEDURE OT.ALL_CRUD_PERSON(DATA1 VARCHAR2,
ID PERSON.ID%TYPE,
NAME PERSON.NAME%TYPE,
AGE PERSON.AGE%TYPE,
R_C OUT SYS_REFCURSOR)
IS
CURSOR CUR IS
SELECT MAX(ID) FROM OT.PERSON;
BEGIN
IF DATA1='INSERT' THEN
INSERT INTO OT.PERSON(ID,NAME,AGE) VALUES (CUR,NAME,AGE);
END IF;
IF DATA1='UPDATE' THEN
UPDATE OT.PERSON SET NAME=NAME,AGE=AGE WHERE ID=ID;
END IF;
IF DATA1='DELETE' THEN
DELETE FROM OT.PERSON WHERE ID=ID;
END IF;
IF DATA1='SELECT' THEN
OPEN R_C FOR
SELECT * FROM OT.PERSON;
END IF;
END;
/
Also,I want to ask is it the good process to put all the functionality in same procedure?
Problem 1
INSERT INTO PERSON(ID,NAME,AGE) VALUES (CUR,NAME,AGE);
This will result in a
Error(19,41): PL/SQL: ORA-00904: "CUR": invalid identifier
Perhaps should be
INSERT INTO PERSON(ID,NAME,AGE) VALUES (ID,NAME,AGE);
That at least will compile without errors.
Looks like you're using Toad...I know if you use SQL Developer it will automatically show you the Errors whenever you compile PL/SQL with compiler feedback.
Also, ask yourself this question - do you want due to a bug, for a call to do an UPDATE to accidentally do a DELETE?
I would suggest you break these operations out to individual functions/procedures - and tie them together using a PACKAGE.
I'm coming from a long history with SQL Server and trying to learn PL/PGSQL. I've recently discovered the PG11 feature CREATE PROCEDURE which allows for internal transactions inside their body.
As a learning exercise, I've created this:
DROP PROCEDURE IF EXISTS test_proc();
CREATE PROCEDURE test_proc()
LANGUAGE plpgsql
AS $$
BEGIN
DROP TABLE IF EXISTS a;
CREATE TABLE a (aid int);
COMMIT;
END;
$$;
call test_proc();
It works fine in PSQL, however when I execute it in PGAdmin 4's Query Tool, it errors with
ERROR: invalid transaction termination
CONTEXT: PL/pgSQL function test_proc() line 5 at COMMIT
SQL state: 2D000
Can someone please explain what's going on? I'm guessing that the PROCEDURE is in fact valid and the issue might be in the query tool might be incorrectly processing the contained COMMIT.
Are there any suggestions for working around this?
Thanks!
In Pgadmin CREATE PROCEDURE as FUNCTION
please try this by Replace PROCEDURE with Function and add Return type also and it will work
DROP FUNCTION IF EXISTS test_proc();
CREATE FUNCTION test_proc()
RETURNS VOID
LANGUAGE plpgsql
AS $$
BEGIN
DROP TABLE IF EXISTS a;
CREATE TABLE a (aid int);
COMMIT;
END;
$$;
Can you please try unchecking auto commit & auto rollback options in pgAdmin4?
You can find drop down near Execute button in the query tool.
I have a Sybase procedure returning nothing. Now I want to modify the procedure and wanted an OUT parameter similar to SYS_REFCURSOR in Oracle to get the result of a query. Can you please suggest ?
Code:
create procedure get_rec_test
#buss_date datetime
as
begin
INSERT INTO TEST
SELECT * FROM short_positions_report
end
GO
In the above code how to add the out parameter to get resultset from TEST table. ? Am a novice in sybase and any help will be highly appreciated.
I have procedure dbo.GetData:
Create procedure dbo.GetData
As
Begin
Select * from dbo.tblName
End
And I also created a schema [ABC], table ABC.tblName
So, I would like to change schema [dbo] of table in procedure dbo.GetData into [ABC] by using another stored procedure.
And, the result is:
Create procedure dbo.GetData
As
Begin
Select * from [ABC].tblName
End
How can I do it?
Thank you everyone.
I'm not sure I understand what you're asking, but I think you simply want to change the code being executed in the stored procedure. If so, a simple ALTER PROCEDURE would do the trick to change the code, but not the name:
ALTER PROCEDURE dbo.GetData
AS
BEGIN
SELECT * FROM [ABC].tblName
END
Full syntax of [ALTER PROCEDURE] 1 (for SQL Server)
If this is not what you're after, please clarify the question.
Update:
The only real solution I see is that you script out your procs, and then use a text-editor to replace the dbo. values with [ABC]. values.
I just attempted to try and do this through updating the system tables, but in SQL Server 2012 (which I use), it simply gets far too complex for that.
Try this hope this may help you!
ALTER SCHEMA NewSchemaName TRANSFER OldSchemaName.ObjectName
So, not sure what is happening. But I have stored procedure and it keeps disappearing out of my DB in SQL 2k.
I can add it again and then try to execute it from my web app and i get an exception saying the stored procedure cant be found. So then ill go back to management and refresh and its gone again !?!
here is the config for the stored proc:
set ANSI_NULLS OFF
set QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[USP_Equipment_Delete]
#EquipmentID int
AS
DELETE FROM [dbo].[Equipment]
WHERE
[EquipmentID] = #EquipmentID
None of my other stored procedure disappear. This is the only one. I have easily 100 in there. They all use the same SQLHelper class. This one just keeps disappearing!!!??!!
Any help or suggestions are appreciated!
Thanks so much!
You were creating this or another stored proc and at the end of your code, maybe after a comment, where you did not see it you have a drop of this proc.
Take a look at your db using:
select syo.name
from syscomments syc
join sysobjects syo on
syo.id = syc.id
where syc.[text] like '%DROP PROC%'
I had the same problem and I just fixed it:
In the script file it was missing the "GO" statement between the end of the stored procedure and the beginning of the next "IF EXIST THEN DROP" statement.
So what happened was that the drop statement was getting appended to the end of whatever stored procedure was above it in the script. So when the software ran the stored procedure it would drop whatever stored procedure was below it in the script.
It seems so obvious to us now but didn't make any sense at the time. We found it running the SQL profiler against a customer's database that was having the problem in the field.
Are you using the correct database?
Try
using [database name]
prior to executing your stored procedure, just to make sure.
Do you have a CREATE PROCEDURE anywhere? You can't ALTER a procedure if it doesn't exist.
Perhaps the code to access the stored procedure is using a different context other than dbo. Make sure to add dbo.USP_Equipment_Delete to the code using it.
I was facing the problem that all Stored Procedures with a create statement disappeared from the database after execution.
The Solution was: The database user should have the rights to drop,create and alter on the database in which the "Stored Procedures" are going to be created.
Perhaps there's a job thats restoring an old backup periodically?
Check if the "Initial Catalog" in your connection string is set to the correct database.
Put the database in single user mode (and make sure you're the single user) and check if the procedure still disappears every hour?
If it's there, then this query must return a record:
SELECT * FROM sysobjects
WHERE id = OBJECT_ID('USP_Equipment_Delete')
AND OBJECTPROPERTY(id, N'IsProcedure') = 1