Metsu 3.0

Ad astra per aspera

Oracle PL/SQL – Obteniendo el registro padre, abuelo , etc sin usar sql especifico de oracle

Filed under: Programacion — metsuke at 4:40 pm on Thursday, November 6, 2008

Es un simple ejercicio de programación en este lenguaje que podria venirles bien en un entorno dinámico, no obstante recomiendo el uso de consultas directas siempre que sea posible, será más rápido.

F_GET_PARENT

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
CREATE OR REPLACE FUNCTION "F_GET_PARENT" (tableName IN VARCHAR2, idField IN VARCHAR2, parentField IN VARCHAR2, currentId IN NUMBER) RETURN NUMBER IS
 
 -- Raul Carrillo Garrido aka metsuke - www.metsuke.com
 -- Released Under LGPL License v3 (http://www.gnu.org/copyleft/lesser.html)
 
 sDebug BOOLEAN := TRUE; sDonde VARCHAR2(50) := 'F_GET_PARENT';
 resultado NUMBER := NULL;
 strSQL VARCHAR2(500) := '';
 
 BEGIN
 
   IF (sDebug) THEN P_Debug_Log(sDonde,'Tabla ' || tableName || ' - Id_Fld ' || idField || ' - Parent_Fld ' || parentField || ' - Id ' || currentId ); END IF;
 
   BEGIN
 
	 strSQL := 'SELECT ' || parentField || ' FROM ' || tableName || ' WHERE ' || idField || ' = :currentId';
	 EXECUTE IMMEDIATE strSQL INTO resultado USING currentId;
 
 
	 RETURN resultado;
 
   EXCEPTION
 
        WHEN NO_DATA_FOUND THEN
        IF (sDebug) THEN P_Debug_Log(sDonde,' WHEN NO_DATA_FOUND LVL2 ' || SQLERRM); END IF;
		RETURN NULL;
   END;
 
 EXCEPTION
 
 WHEN OTHERS THEN
   IF (sDebug) THEN P_Debug_Log(sDonde,' WHEN OTHERS GENERAL ' || SQLERRM); END IF;
   RETURN -2;
 
END;
/

F_GET_ANCIENT_PARENT

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
CREATE OR REPLACE FUNCTION "F_GET_ANCIENT_PARENT" (tableName IN VARCHAR2, idField IN VARCHAR2, parentField IN VARCHAR2, currentId IN NUMBER) RETURN NUMBER IS
 
 -- Raul Carrillo Garrido aka metsuke - www.metsuke.com
 -- Released Under LGPL License v3 (http://www.gnu.org/copyleft/lesser.html)
 
 sDebug BOOLEAN := TRUE; sDonde VARCHAR2(50) := 'F_GET_ANCIENT_PARENT';
 resultado NUMBER := NULL;
 
 BEGIN
 
   IF (sDebug) THEN P_Debug_Log(sDonde,'Tabla ' || tableName || ' - Id_Fld ' || idField || ' - Parent_Fld ' || parentField || ' - Id ' || currentId ); END IF;
 
   BEGIN
 
		resultado := F_GET_PARENT(tableName,idField,parentField,currentId);
 
		IF (resultado IS NULL) THEN
		   RETURN currentId;
		ELSIF (resultado < 0) THEN
		   RETURN resultado;
		ELSE
		   RETURN F_GET_ANCIENT_PARENT(tableName,idField,parentField,resultado);		
		END IF;
 
   EXCEPTION
        WHEN NO_DATA_FOUND THEN
        RETURN NULL;
   END;
 
 EXCEPTION
 
 WHEN OTHERS THEN
 
   RETURN -1;
 
END;
/

Dependencias


  • Print
  • email
  • Google Bookmarks
  • Digg
  • BarraPunto
  • Technorati
  • Slashdot
  • del.icio.us
  • Facebook
  • Reddit
  • Sphinn
  • Mixx
  • Live
  • MySpace
  • Ping.fm
  • Twitter
  • Bitacoras.com
  • Faves
  • Furl
  • Identi.ca
  • N4G
  • LinkedIn
  • Netvibes
  • StumbleUpon
  • Haohao
  • MSN Reporter
  • PDF
  • RSS
  • SphereIt
  • Wikio
  • Socialogs

No Comments »

No comments yet.

RSS feed for comments on this post.

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">