0

How can I read a stored procedure from a .sql file? Example of this file's content:

create or replace procedure insert(p_t varchar(20), p_e varchar(10), out p_id number)
as begin
insert into instrument(type, status) values (p_t, p_e);
commit;
select max(id) from instrument into p_id;
end /

...other procedures...

I want this procedure to be created when I use the command @"filepath", but instead of doing so, Oracle executes the content of the procedure.

I would like to create all my procedures reading the file I made, any ideas?.

1
  • Are you using SQL*Plus or some other tool? Commented Aug 21, 2013 at 18:05

1 Answer 1

2

Firstly, please don't call your procedure INSERT. I'd be horrified if this actually compiled.

Secondly, you seem to be missing a semi-colon after end; and you need to put the / on a new line:

create or replace procedure insert_instrument (
   p_t varchar(20), p_e varchar(10), out p_id number ) as 

begin
  insert into instrument(type, status) values (p_t, p_e);
  commit;
  select max(id) from instrument into p_id;
end;
 /

You could simplify this using the RETURNING INTO clause; it saves an extra SELECT and so will operate quicker:

create or replace procedure insert_instrument (
   p_t varchar(20), p_e varchar(10), out p_id number ) as 

begin
  insert into instrument(type, status) 
  values (p_t, p_e)
  returning id into p_id;
  commit;
end;
 /
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, the procedure name was just a typo:D I had already fixed it but your "returning into" suggestion is very much appreciated. Would like to clarify if you meant to use 'returning id into p_id'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.