Procedure needed to input table name
i want a procedure where i would enter the table name and an attribute as input parameters.
The procedure will find whether the entered attribute is primary key or not
Please help me
Hi,
Try this :
SQL> create table test (col1 number primary key, col2 number);
Table created.
SQL> create or replace function is_pk (tb_name varchar2, col_name varchar2)
return varchar2 is
var_is_pk varchar2(5);
begin
begin
select 'PK'
into var_is_pk
from user_cons_columns a, user_constraints b
where a.OWNER=b.OWNER
and a.CONSTRAINT_NAME=b.CONSTRAINT_NAME
and a.TABLE_NAME=b.TABLE_NAME
and a.TABLE_NAME=upper(tb_name)
and a.COLUMN_NAME=upper(col_name)
and b.constraint_type='P';
exception when others then var_is_pk:='NO_PK';
end;
return var_is_pk;
end;
Function created.
SQL> select is_pk('test','col1'), is_pk('test','col2') from dual;
IS_PK('TEST','COL1') IS_PK('TEST','COL2')
PK NO_PKHTH,
Nicolas.
I'm sorry Maarten, my network is catastrophic this morning... so I didn't see your post before post mine.
Message was edited by:
N. Gasparotto
Similar Messages
-
Create a procedure which will pass table name as an argument
Please let me know, I want to create a procedure which will take
table name as an argument and will display total no. of rows of
that table.Hopefully this code will help u.
CREATE OR REPLACE PROCEDURE P1(TAB_NAME IN VARCHAR2, ROW_NO
OUT NUMBER)
AS
SQL_STMT VARCHAR2(1000);
BEGIN
SQL_STMT := 'SELECT COUNT(*) FROM ' || TAB_NAME;
EXECUTE IMMEDIATE SQL_STMT INTO ROW_NO;
END;
SQL > VAR A NUMBER
SQL > EXEC P1('EMP',:A);
SQL > PRINT A
A
12
SQL > SELECT COUNT(*) FROM EMP;
COUNT(*)
12 -
Help needed in making table name and column name dynamic
please check the below query? in the below message
Message was edited by:
460425
Message was edited by:
460425thanks Dmytro,
below is the script i was looking for it got it..any way thanks.
just need to replace dbms_output.put_line with utl_file.put_line
to put the code on server directory.
and execute as and when required.
Reg.
AAK
CREATE OR REPLACE PROCEDURE p_ad_log
IS
CURSOR tbl_cursor IS
SELECT table_name FROM user_tables WHERE table_name IN('EMP','EMP1') ;
CURSOR col_cursor( cp_table_name varchar2) IS
SELECT column_name FROM user_tab_columns WHERE table_name=cp_table_name;
--v_file_handle utl_file.file_type;
--v_file_dir varchar2(30) := 'DIRECTORY PATH' ';
--v_file_name varchar2(30) := 'AD_TRIGGER_TEXT.TXT';
tbl_cursor_value tbl_cursor%ROWTYPE;
col_cursor_value col_cursor%ROWTYPE;
v_string varchar2(4000);
v_string_val varchar2(4000);
BEGIN
DELETE audit_triggers_status;
COMMIT;
--v_file_handle := utl_file.fopen(v_file_dir,v_file_name,'W',32000);
OPEN tbl_cursor;
LOOP
FETCH tbl_cursor into tbl_cursor_value;
EXIT WHEN tbl_cursor%NOTFOUND;
OPEN col_cursor(tbl_cursor_value.table_name);
DBMS_OUTPUT.PUT_LINE( 'CREATE OR REPLACE TRIGGER' ||' ad_'||tbl_cursor_value.table_name); -- short name for audit trigger coz table name will be appended to it and result should not exceed 30 char
DBMS_OUTPUT.PUT_LINE( 'BEFORE INSERT OR UPDATE OR DELETE ON '||tbl_cursor_value.table_name);
DBMS_OUTPUT.PUT_LINE( 'FOR EACH ROW');
DBMS_OUTPUT.PUT_LINE( 'BEGIN');
v_string:='INSERT INTO'||' ad_'||tbl_cursor_value.table_name||'(';
v_string_val:='values(';
INSERT INTO audit_triggers_status( table_name,trigger_name,audit_flag) VALUES (tbl_cursor_value.table_name,' ad_'||tbl_cursor_value.table_name,'Y');
LOOP
FETCH col_cursor into col_cursor_value;
EXIT WHEN col_cursor%NOTFOUND;
v_string:=v_string||col_cursor_value.column_name||',';
v_string_val:=v_string_val||':new.'||col_cursor_value.column_name||',';
END LOOP;
CLOSE COL_CURSOR;
v_string:=substr(v_string,1,length(v_string)-1);
v_string_val:=substr(v_string_val,1,length(v_string_val)-1);
v_string:=v_string||') ';
v_string_val:=v_string_val||');';
--DBMS_OUTPUT.PUT_LINE(v_string||v_string_val);
DBMS_OUTPUT.PUT_LINE('IF INSERTING THEN');
DBMS_OUTPUT.PUT_LINE(' '||v_string||v_string_val);
DBMS_OUTPUT.PUT_LINE('END IF;');
DBMS_OUTPUT.PUT_LINE('IF UPDATING THEN');
DBMS_OUTPUT.PUT_LINE(' '||v_string||v_string_val);
DBMS_OUTPUT.PUT_LINE('END IF;');
DBMS_OUTPUT.PUT_LINE('IF DELETING THEN');
--DBMS_OUTPUT.PUT_LINE(' '||v_string||REPLACE(v_string_val,':new.',':old.');
V_STRING_VAL:=REPLACE(v_string_val,':new.',':old.');
DBMS_OUTPUT.PUT_LINE(' '||v_string||v_string_val);
DBMS_OUTPUT.PUT_LINE('END IF;');
DBMS_OUTPUT.PUT_LINE('END '||' ad_'||tbl_cursor_value.table_name||';');
DBMS_OUTPUT.PUT_LINE(' ');
END LOOP;
CLOSE TBL_CURSOR;
COMMIT;
END; -
Oracle function in stored procedure to retreive particular table name
Hi,
I need to delete a table 'xxsys_dram_flatproperties' from database given model name as 'dram-model'.
the model name can be any thing.corresponding table name will be[i] 'xxsys_<modelname>_flatproperties'.
I wrote stored procedure .I am able to retrieve all the tables in the database using user_tables.Is there any funtion in oracle like
if table name contains letters that are there in model name.
Pleasehelp.Suppose all your tables created in the schema 'temp'
and you are absolutly sure you want to drop only tables which have for example '_model3_' in its name (nothing else should have same part in your schema)
then try a simple solution:
CREATE TABLE temp.xx_model3_01 (col1 NUMBER)
CREATE TABLE temp.xx_model3_02 (col1 NUMBER)
SELECT table_name from all_tables where OWNER = 'TEMP' and table_name LIKE '%_MODEL3_%'
TABLE_NAME
XX_MODEL3_01
XX_MODEL3_02
DECLARE
CURSOR c1
IS
(SELECT table_name
FROM all_tables
WHERE owner = 'TEMP' AND table_name LIKE '%_MODEL3_%');
BEGIN
FOR rec IN c1
LOOP
EXECUTE IMMEDIATE 'DROP TABLE ' || rec.table_name;
END LOOP;
END; -
Need to include table name,schema name in select output
I need to output the table name and schema name of the current user into a table.. I have tried including
within a select statement..
table_name,
from user_tables
where table_name='mnme';
no rows returnedHi,
Its in upper case 'MNME'
Best.
EA -
Need a that Table Name in Select command which being used in from statement
My Question Is:
If my SQL command is like this "
Select Code, Name from Customer (Table Name)
Can I get this output in this Way .. ?
Select Code, Name,Customer(Table Name) from Customer (Table Name)
in dynamic way ..NO. You missing many fundamental concepts of RDBMS. A table is a set of entities, so it's name is plural or collective(unless you really do have only one customer as you said). There is no such thing as a generic "name", or "code" in RDBMS. Those affixes
are called attribute properties, and they have to be part of an attribute data element name -- customer_name, postal_code, etc.
Since a table is a set, its columns are fixed and known in the schema. They do not change by magic! This kind of non-RDBMS thinking is called "Automobiles, Squids and Lady GaGa" coding. Please read any intro book on RDBMS. Your whole mindset is wrong.
--CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking
in Sets / Trees and Hierarchies in SQL -
Help passing table name as parameter to a procedure
Hello,
i'm trying to write a procedure that takes a table name as input and uses a cursor to select a column,count(1) from the passed table to the cursor. The procedure i've come up with is as follows,
CREATE OR REPLACE
PROCEDURE excur(
p_tbl user_tables.table_name%type )
AS
type rc is ref cursor;
c rc;
res BOOLEAN;
BEGIN
open c for 'SELECT columnA,COUNT(1) FROM'|| p_tbl||';';
close c;
END excur;When i try to execute it, an error pops up informing that a table cannot be used in this context. As in i cannot pass a table name as an argument to a procedure. Kindly guide as to how to solve this situation.vishm8 wrote:
Hello,
i'm trying to write a procedure that takes a table name as input and uses a cursor to select a column,count(1) from the passed table to the cursor. The procedure i've come up with is as follows,
CREATE OR REPLACE
PROCEDURE excur(
p_tbl user_tables.table_name%type )
AS
type rc is ref cursor;
c rc;
res BOOLEAN;
BEGIN
open c for 'SELECT columnA,COUNT(1) FROM'|| p_tbl||';';
close c;
END excur;When i try to execute it, an error pops up informing that a table cannot be used in this context. As in i cannot pass a table name as an argument to a procedure. Kindly guide as to how to solve this situation.Generally speaking, Dynamic code is a bad idea for a staggering number of reasons.
That aside, what do you want to return? You're selecting a column and applying an aggregate function (count) but you're not grouping, that doesn't usually work out too well.
TUBBY_TUBBZ?select owner, count(*) from all_objects;
select owner, count(*) from all_objects
ERROR at line 1:
ORA-00937: not a single-group group functionWhy do you perceive the need to be able to take in ANY table name, can't you design an application where table names are known at compile time and not run time? -
How to write table name in SQL according to the user input
hi
i want to select data from a table , and the table name, field name is input by the user, for example, user input table name T1, field name F1 and i want to construct a SQL like select F1 into a_var from T1 . how does the SQL statement written? thanks for your advice.hi
for ur requirement u should go for field sysmbols...
dynamic internal tables..
u can chk this....
TYPE-POOLS : slis.
PARAMETERS : p_struc LIKE dd02l-tabname.
DATA : it_fieldcat TYPE lvc_t_fcat,
t_output TYPE REF TO data .
FIELD-SYMBOLS: <t_output> TYPE table ,
<wa_output> TYPE ANY,
<fval> TYPE ANY.
CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
EXPORTING
I_BUFFER_ACTIVE = I_BUFFER_ACTIVE
i_structure_name = p_struc
I_CLIENT_NEVER_DISPLAY = 'X'
I_BYPASSING_BUFFER = I_BYPASSING_BUFFER
I_INTERNAL_TABNAME = I_INTERNAL_TABNAME
CHANGING
ct_fieldcat = it_fieldcat
EXCEPTIONS
INCONSISTENT_INTERFACE = 1
PROGRAM_ERROR = 2
OTHERS = 3
IF sy-subrc <> 0.
MESSAGE i000(bctrain) WITH 'MERGE FAILED'.
*ELSE.
MESSAGE i000(bctrain) WITH 'MERGE SUCCESS'.
ENDIF.
CREATE DATA t_newline LIKE LINE OF <t_dyntable>.
ASSIGN t_newline->* TO <fs_dyntable>.
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
I_STYLE_TABLE =
it_fieldcatalog = it_fieldcat
I_LENGTH_IN_BYTE =
IMPORTING
ep_table = t_output
E_STYLE_FNAME =
EXCEPTIONS
generate_subpool_dir_full = 1
OTHERS = 2
IF sy-subrc <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
ASSIGN t_output->* TO <t_output>.
SELECT * up to 10 rows FROM
(p_struc) INTO
CORRESPONDING FIELDS OF
TABLE <t_output>.
*write : / 'dynamic table created'.
LOOP AT <t_output> ASSIGNING <wa_output>.
DO.
ASSIGN component sy-index of structure <wa_output> to <fval>.
IF sy-subrc <> 0.
WRITE : /.
EXIT.
ELSE.
WRITE : <fval>.
ENDIF.
ENDDO.
ENDLOOP.
Message was edited by:
Premalatha G -
How to write a procedure with the table name as an argument
I'm very new to Oracle, usually developing in SQL server.
I would like to write a procedure that accepts a table name and then does a
SELECT * FROM [table name] order by CollectionDate.
In SQL I would build up a varchar variable and then execute it.
ThanksI often wonder why so many people new to Oracle want to do this as a first step, when most people who know what they are doing try and avoid it.
SQL> create or replace function f (p_tab varchar2)
2 return sys_refcursor as
3 c sys_refcursor;
4 begin
5 open c for
6 'select * from ' || p_tab || ' order by hiredate';
7 return c;
8 end;
9 /
Function created.
SQL> var c refcursor
SQL> exec :c := f('emp')
PL/SQL procedure successfully completed.
SQL> print c
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7369 SMITH CLERK 7902 12/17/1980 800 20
7499 ALLEN SALESMAN 7698 02/20/1981 1600 300 30
7521 WARD SALESMAN 7698 02/22/1981 1250 500 30
7566 JONES MANAGER 7839 04/02/1981 2975 20
7698 BLAKE MANAGER 7839 05/01/1981 2850 30
7782 CLARK MANAGER 7839 06/09/1981 2450 10
7844 TURNER SALESMAN 7698 09/08/1981 1500 0 30
7654 MARTIN SALESMAN 7698 09/28/1981 1250 1400 30
7839 KING PRESIDENT 11/17/1981 5000 10
7900 JAMES CLERK 7698 12/03/1981 950 30
7902 FORD ANALYST 7566 12/03/1981 3000 20
7934 MILLER CLERK 7782 01/23/1982 1300 10
7788 SCOTT ANALYST 7566 12/09/1982 3000 20
7876 ADAMS CLERK 7788 01/12/1983 1100 20
14 rows selected.
SQL>Note if you pass in a value to be used in the where clause like deptno, you should not use this technique, but use bind variables intead like this -
Wrote file afiedt.sql
1 create or replace function f (p_tab varchar2, p_deptno number)
2 return sys_refcursor as
3 c sys_refcursor;
4 begin
5 open c for
6 'select * from ' || p_tab
7 || ' where deptno = :p_deptno order by hiredate'
8 using p_deptno;
9 return c;
10* end;
SQL> /
Function created.
SQL> exec :c := f('emp', 10)
PL/SQL procedure successfully completed.
SQL> print c
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7782 CLARK MANAGER 7839 06/09/1981 2450 10
7839 KING PRESIDENT 11/17/1981 5000 10
7934 MILLER CLERK 7782 01/23/1982 1300 10
SQL> -
How to find out the table name
hi,
how to find out the table name in which the data from a particular structure in a particular screen is saved,
please tell me the procedure to find out the table name for saving the structure data that is inputted at runtime.
Thanks,
chinnuHi Chinnu,
Below are the tables that are referred to find out the table names
DD02L Table contains the SAP Tables.
DD02T Table contains the SAP Table Texts.
DD01L Table contains the Domains
DD01T Table contains the Domain Texts.
DD03L Table contains the Table Fields.
DD03T Table contains the Table Field Texts. (Language Dependent)
DD04L Table contains the Data Elements.
DD04T Table contains the Data Element Texts.
DD05s Table contains the Foreign Key Fields
last words with L and T only. L->Database Fetch T-> Text
And the procedure to retrive the table name is as follows
1. Go to se11
2. Enter table name DD03T and execute
3. In the next screen you can find Tables, fields, test etc. there you can enter the field name in the fields
and execute.
4. you can get all tables which contains the field.
I hope this will solve your problem
Regards,
Chandru -
Passing table names through variables
Hi All,
Is it possible to pass the table name in a query using a variable.
For example - can we perform a select on a table where the table name is passed using a variable.
Pl assist. Thanks.Hello,
Are you wanting to do this in a procedure? Say the table name being passed in as Input Parameter?
We can do that using Dynamic SQL:
CREATE PROCEDURE "PROC_01"
(IN v_IN_Var1 NVARCHAR)
LANGUAGE SQLSCRIPT AS
BEGIN
DECLARE ABC NVARCHAR(300) := NULL;
ABC := 'SELECT * FROM ' || :v_IN_Var1;
EXEC (:ABC);
END;
CALL PROC_01 ('DUMMY');
But the only thing is you'll not be able to see the result set directly.
You might wan2 fire the select to insert the data into a different table. -
Get Target table name and its count
Hi Experts,
I have created a Procedure where I want to get the Target table name and its count of the previous Interface in a Package and insert these values into the Audit table.
I am able to get the number of Inserts in previous step using getPrevStepLog() API, but I also want the Target table name and its total count of the previous Interface. Is there any generic way or code to bring these Information without hardcoding the table name. Kindly help to sort this issue.Thanks!
Warm Regards,
VBVHi VBV,
Please follow the below steps to audit ur execution.
Please note i coded in such a way that u can use ur existing procedure to capture the table name no need to query ur repository as its not advisable to query the repository as Oracle may change the structure in future which i personally feel right.
I created a Audit table with the below structure.
CREATE TABLE AUDIT_TABLE
INSERT_COUNT VARCHAR2(100 BYTE),
TABLE_NAME VARCHAR2(4000 BYTE)
Step 1 :
This step needs to be added in ur existing IKM after Commit step where i am using API to capture the target table name.
Step Name: Get Table Name
Command On Source
Technology: Oracle
Schema: Any Oracle related Schema
Command:
SELECT '<%=odiRef.getTargetTable("RES_NAME")%>' AS TGT_NAME FROM DUAL
Command On Target
Technology: Jython
Command:
TargetTable='#TGT_NAME'
Step 2:
In ur existing procedure add the below or add a new procedure.
Step Name: Audit Log
Command on Source
Technology : Oracle
Schema: Whichever schema holds the audit table
Command on Target
Technology : Jython
Command:
import java.sql as sql
import java.lang as lang
myCon =odiRef.getJDBCConnection("SRC")
MyStmt=myCon.createStatement()
myRs = MyStmt.executeQuery("INSERT INTO ODITGT.AUDIT_TABLE (INSERT_COUNT,TABLE_NAME) VALUES (<%=odiRef.getPrevStepLog("INSERT_COUNT")%>,'"+TargetTable+"')")
Thats it.
OTN doesnt allow me to put + sign in front of any word ( its assume its a Italic) so please add + (plus) sign before and after TargetTable in the above script.
This way u can capture the table name, insert count in to ur audit table.
Please note u need to call this Procedure after the successful completion of the interface in a package and make sure u selected the right IKM which capture the table name as in Step 1.
I tested in my local and its working fine. Let me know if u find any difficulties in implementation.
Thanks,
Guru -
Looping delete statement where table name is coming from another table
Hi All,
We have to write code to delete records from active tables of WDSO.We have 5 DSO so inspite of writing delete statement for each table we want to put all table names into one table and then loop through it .but we are getting error when we are refering that table field which has active table name.error is :
"dictionary structure or table is either not active or does not exist "
As per my understanding in a delete /select /insert /update statement we need to put table name (whose field we are refering ) it can't be replaced by a variable .
ex: v_table = 'EMPLOYEE' .
DELETE FROMv_table WHERE EMP_NAME = 'ABDC' .
is wrong and it must be like
ex : DELETE FROM EMPLOYEE WHERE EMP_NAME = 'ABDC' .
but we want to make our code dynamic .
Can you please suggest a way so that we can read the table names from another table and delete data based on some selection fom those tables .
I tried variants ,perform etc and even searched FM for the same but not found a solution .Your help will be greatly appreciated .
Thanks in advance .
Regards,
JayaHi,
You can change your statement as follows:
DELETE FROM (v_table) WHERE EMP_NAME = 'ABDC' .
However, I would not recommend this. There is a standard function module RSAN_ODS_DATA_DELETE which allows selective deletion - that should be a safer way to do this. You can -
Hi Experts,
I just want to ask is there any procedure to know the table name and the corrosponding column name when you have only data with you.
Suppose you have a string say "India". Now i want to know the name of the column and corrosponding tablename
which holds the value "India".
Thanks
RajatSolutions previously provided by michaels...
michaels> var val varchar2(5)
michaels> exec :val := 'as'
PL/SQL procedure successfully completed.
michaels> select distinct substr (:val, 1, 11) "Searchword",
substr (table_name, 1, 14) "Table",
substr (t.column_value.getstringval (), 1, 50) "Column/Value"
from cols,
table
(xmlsequence
(dbms_xmlgen.getxmltype ('select ' || column_name
|| ' from ' || table_name
|| ' where upper('
|| column_name
|| ') like upper(''%' || :val
|| '%'')'
).extract ('ROWSET/ROW/*')
) t
-- where table_name in ('EMPLOYEES', 'JOB_HISTORY', 'DEPARTMENTS')
order by "Table"or
SQL> select table_name,
column_name,
:search_string search_string,
result
from cols,
xmltable(('ora:view("'||table_name||'")/ROW/'||column_name||'[ora:contains(text(),"%'|| :search_string || '%") > 0]')
columns result varchar2(10) path '.'
where table_name in ('EMP', 'DEPT')
TABLE_NAME COLUMN_NAME SEARCH_STRING RESULT
DEPT DNAME ES RESEARCH
DEPT DNAME ES SALES
EMP ENAME ES JONES
EMP ENAME ES JAMES
EMP JOB ES SALESMAN
EMP JOB ES SALESMAN
EMP JOB ES SALESMAN
EMP JOB ES PRESIDENT
EMP JOB ES SALESMAN
9 rows selected. -
Table name for release strategy
Hi
In which place i need to take table name for purchase requisition and purchase order release strategy configurartion,plz suggest
Regards
SamHI
While defining characteristics you required table name in po & pr
Maybe you are looking for
-
Ipod touch not being recognised by finder / itunes
I have just bought a new ipod touch and upgraded to 10.6 and the latest itunes, the ipod touch was recognised initially, but now when i plug the ipod in only aperture launches and recognises the ipod - itunes and finder do not see the touch -- any id
-
What do I need to charge my iPhone, iPad and MacBook Pro in Europe (Spain). A power converter of just plug adapters?
-
How to include shell script in java
how to include a ".sh" file in a java code and then use it
-
Help on javafx coding please...
hi, I have developed a small eclipse application. I want to integrate javafx with it. I used the eclipse view's "composite" as the frame for the javafx application i developed, say a frame has a canvas which inturn has a rectangle. Integration using
-
Hello Gurus, I have one table which i partioned by yearly. select partition_name, num_rows from dba_tab_partitions where table_name='TEST_OMAN'; PARTITION_NAME NUM_ROWS CHANGE_DATE_DEC2005 1278654 CHANGE_DATE_DEC2006 2867545 CHANGE_DATE_DEC2007 36574