Is there any way to accomplish this? For example, let's say the object_id for stored procedure X is 1234. I'm trying to do something like
EXEC ('EXEC Object_Name(1234)')
and it would run stored procedure X. Is there a way to accomplish this?
Thanks.
I guess you work with Oracle db.
To run procedure with known object_id you can use dictionary view all_objects and construct a call.
Here is an example:
declare
iObjectId number := 3927531;
sProcedureName varchar2(128);
sProcedureOwner varchar2(128);
begin
-- get the procedure name
for c in (
select object_name procedure_name,
owner procedure_owner
from all_objects
where object_type = 'PROCEDURE'
and object_id = iObjectId
) loop
sProcedureName := c.procedure_name;
sProcedureOwner := c.procedure_owner;
end loop;
if sProcedureName is not null then
execute immediate 'begin '||sProcedureOwner||'.'||sProcedureName||'; end;';
dbms_output.put_line('Procedure '||sProcedureName||' was executed successfully.');
else
dbms_output.put_line('Procedure was not found for object_id = '||iObjectId||'.');
end if;
end;
Keep in mind that procedure can have required parameters. In this case you can use the dictionary view all_arguments.
Hope that helps.
Related
Hi so i have the following plan :
i want to Write a script, which calls another script. A parameter V_USERNAME should be passed from the 1st script to the 2nd script.( Using the keyword DEFINE)
My code look like :
##C:\Users\pe.k\Documents\script2.sql &p_v_username
set serveroutput on
define p_v_username = "user";
In the 2nd script all tables of the user should be output. (Using the key word EXECUTE IMMEDIATE and a cursor).
The output control is to be done via a parameter in the script or when calling the script.
Example call:
SQL> #start_script1 MML
declare
&p_v_username varchar2(100);
v_result varchar2(100);
cursor cp_username (&p_v_username varchar2)
is
select owner, table_name
from all_tables
where owner = &p_v_username
order by owner, table_name;
begin
dbms_output.put_line('Alle Tabellen der User'); --l_username);
open cp_username(&p_v_username);
--loop
--fetch cp_username into v_result;
-- dbms_output.put_line(v_result);
--end loop;
close cp_username;
end;
/
And i have the errors and i am lost. I dont know how to do it
Your second procedure should be something like this:
BEGIN
DBMS_OUTPUT.PUT_LINE('Alle Tabellen der User'); --l_username);
FOR TABS IN (
SELECT OWNER, TABLE_NAME
FROM ALL_TABLES
WHERE OWNER = '&P_V_USERNAME'
ORDER BY OWNER, TABLE_NAME
) LOOP
DBMS_OUTPUT.PUT_LINE(TABS.TABLE_NAME);
END LOOP;
END;
/
In order to print the output generated by the DBMS_OUTPUT package from PL/SQL, make sure to set serveroutput on
I am new to Oracle database, got a task to write several selects, but I do not get somethow principle, how it works (or better to say does not work). Could you please help me to find a mistake and probably you have a link to some info or converter that helps such writings.
v_sql := 'SELECT ''DROP TABLE ''||object_name||'' as
select *
FROM all_objects
WHERE object_name LIKE '''%''|| v_date ||''%'''
and object_type = ''TABLE''
and owner =''||v_owner||''';
Are you trying to generate DROP statements for a specific user? Start with the below anonymous block and add to it.
--Generate DROP statements for all tables for a user.
declare
v_owner varchar2(128) := user;
begin
for objects in
(
select distinct 'DROP TABLE "'||owner||'"."'||object_name||'"' v_sql
from all_objects
where object_type = 'TABLE'
and owner = v_owner
order by v_sql
) loop
dbms_output.put_line(objects.v_sql);
end loop;
end;
/
I'm trying to create a procedure that given a table name, it will create a sequence and auto incrementing trigger, all using variables based on the table name.
Code :
CREATE OR REPLACE procedure CREATE_SEQUENTIAL_TR(table_name VARCHAR)
is -- Tried using declare but it wouldn't accept
coluna_cod varchar(100 char);
begin
--Finding the cod column name for this table first
--They start with "PK_CD"
select
COLUMN_NAME
into
coluna_cod
from
ALL_TAB_COLUMNS
where
TABLE_NAME=table_name
and COLUMN_NAME like "PK_CD%";
--Creating the sequence obj
drop sequence "cod" || table_name;
create sequence "cod" || table_name;
--Now creating the trigger
create or replace trigger "cod" || table_name || "tr"
before
UPDATE or INSERT on table_name
for each row
declare
cod number := coluna_cod;
tr_name varchar(100 char) := "cod" || table_name
begin
if UPDATING then
if :new.cod != :old.cod then
:new.cod := :old.cod;
end if;
else -- inserting
:new.cod := tr_name.nextval();
end if;
end;
end;
The complexity of this ended up quite out of the scope of my knowledge.
At the moment it is giving an error on drop sequence "cod" || table_name (Unexpected DROP symbol found) but I'm sure I have made other errors.
Can someone help me figure this logic out?
You can't put DDL statements (like drop or create or alter) directly inside a PL/SQL block. If you want to do DDL inside PL/SQL, you can do an execute immediate:
declare
begin
drop sequence X; -- error
execute immediate 'drop sequence X'; -- works fine
end;
/
I am trying to list procedures and triggers in the database, but I have to use procedure or function to do so.
This query returns exactly what I need, but I need to get the same results using PL/SQL.
select *
from all_source
where type = 'PROCEDURE'
this query returns exactly what I need, but I have to get the same results using PL/SQL.
Thank you
Try this
declare record all_source%ROWTYPE
begin
select * into record from all_source where type = ?
end;
Try with the below procedure,
CREATE OR REPLACE
PROCEDURE get_proc_list(
i_type IN VARCHAR2,
o_result OUT sys_refcursor)
IS
l_type VARCHAR2(10) := UPPER(i_type);
BEGIN
OPEN o_result FOR
SELECT * FROM all_source
WHERE TYPE = l_type;
END;
To Execute the procedure,
var o_result refcursor;
EXECUTE get_proc_list('procedure',:o_result);
print o_result;
I'm trying to use the following stored procedure.
DELIMITER $$
CREATE DEFINER=`root`#`localhost`
PROCEDURE `DeleteField`( IN _TABLENAME Text, IN _FIELDNAME text)
BEGIN
if exists (select * from information_schema.Columns
where table_name = _TABLENAME and column_name = _FIELDNAME)
then
alter table _TABLENAME drop column _FIELDNAME;
end if;
END
So I do Call('anytable','Anyfield') and I get the Error
Error Code:1146Table'Database._tablename'doesn't exist
This _tablename should be my parameter, not a string.
Plz some help before I hang myself, I love my life far too much.
I expect you will need to create a dynamic SQL query to do this.
An example of how to do this is at:
http://www.java2s.com/Code/SQL/Procedure-Function/Createadynamicstatementinaprocedure.htm
This would be the alter table replacement, though I have tested this.
DECLARE l_sql VARCHAR(4000);
SET l_sql=CONCAT_ws(' ',
'ALTER table ',_TABLENAME,' drop column ',_FIELDNAME);
SET #sql=l_sql;
PREPARE s1 FROM #sql;
EXECUTE s1;
DEALLOCATE PREPARE s1;