How you will create Toad Function?



How you will create Toad Function?..

Answer / swapna

CREATE OR REPLACE FUNCTION cons_dependants (tname IN
VARCHAR2, cvalue IN NUMBER)
RETURN NUMBER
IS

CURSOR depen_cur
IS
SELECT c1.table_name table_name, c2.column_name
column_name
FROM user_constraints c1, user_cons_columns c2,
user_constraints c
WHERE c1.constraint_name = c2.constraint_name
AND c1.r_constraint_name = c.constraint_name
AND c.table_name = UPPER (tname);

tbl_cnt NUMBER;
sql_cur NUMBER;
qry_stmt VARCHAR2 (1000);
v_tbl_cnt NUMBER;
BEGIN
FOR i IN depen_cur
LOOP
/*PREPARE THE GENERAL QUERY STATEMENT TO FIND OUT THE DATA
EXISTANCE IN DEPENDANT TABLES*/
dbms_output.put_line(i.table_name);
qry_stmt := 'select count(*) from '
|| i.table_name
|| ' where '
|| i.column_name
|| ' = '
|| ':c_bind_value'
|| ' and '
|| 'record_status ='
|| ' '
|| '''N''';
DBMS_OUTPUT.put_line (qry_stmt);
sql_cur := DBMS_SQL.open_cursor;
DBMS_SQL.parse (sql_cur, qry_stmt, DBMS_SQL.native);
DBMS_SQL.bind_variable (sql_cur, ':c_bind_value',
cvalue);
DBMS_SQL.define_column (sql_cur, 1, v_tbl_cnt);
tbl_cnt := DBMS_SQL.EXECUTE (sql_cur);

IF DBMS_SQL.fetch_rows (sql_cur) > 0
THEN
DBMS_SQL.column_value (sql_cur, 1, v_tbl_cnt);
DBMS_OUTPUT.put_line (v_tbl_cnt||' number of
rows');

IF v_tbl_cnt > 0
THEN
DBMS_SQL.close_cursor (sql_cur);
RETURN 1;
ELSIF v_tbl_cnt = 0
THEN
DBMS_SQL.close_cursor (sql_cur);
END IF;
END IF;
END LOOP;
RETURN -1; /*CAN DELETE THE RECORDS*/
END cons_dependants;
/

Is This Answer Correct ?    3 Yes 10 No

Post New Answer

More SQL PLSQL Interview Questions

Does sql backup shrink transaction log?

0 Answers  


What are the query optimization techniques?

0 Answers  


Is a foreign key always unique?

0 Answers  


when is the use of update_statistics command? : Sql dba

0 Answers  


How to use boolean type in select statement?

0 Answers  






what is the difference between pragma exception_init and raise_application_error

1 Answers   Aetins, State Street,


What is left inner join in sql?

0 Answers  


how to convert dates to character strings? : Sql dba

0 Answers  


what is oracle sql,pl/sql with interfaces

2 Answers   TCS,


What do you mean by stored procedures?

0 Answers  


what is log shipping? : Sql dba

0 Answers  


wht is the difference between truncat,drop in sqlserver wht is the difference between function and stored procedure

3 Answers   Apollo,


Categories