Foros del Web » Programación para mayores de 30 ;) » Bases de Datos General » Mysql »

Pasar valor de tabla en procedimiento almacenado

Estas en el tema de Pasar valor de tabla en procedimiento almacenado en el foro de Mysql en Foros del Web. Buenos días a todos! Haciendo un pequeño foro, escribí unos procedimientos para administrar los registros de un sistema de arbol anidado (nested set tree). Los ...
  #1 (permalink)  
Antiguo 22/09/2010, 15:58
 
Fecha de Ingreso: enero-2009
Mensajes: 15
Antigüedad: 15 años, 3 meses
Puntos: 0
Sonrisa Pasar valor de tabla en procedimiento almacenado

Buenos días a todos!

Haciendo un pequeño foro, escribí unos procedimientos para administrar los registros de un sistema de arbol anidado (nested set tree). Los dejo abajo como aporte.

Ahora bien, mi idea es modificar esos procedimientos para que sirvan en distintas tablas, pasándoles como variable el nombre de las mismas. El único modo que encontré es el que describen en http://www.forosdelweb.com/f86/ayuda-con-procedimiento-almacenado-parametrizado-753431/
¿Les parece que es una buena idea utilizar un mismo procedimiento para varias tablas?
¿Es posible hacerlo? y si es posible, ¿Cómo es la forma más adecuada de hacerlo?

Desde ya muchas gracias!!!!!

Código SQL:
Ver original
  1. DROP PROCEDURE IF EXISTS insertForum;
  2. DELIMITER GO
  3. CREATE PROCEDURE insertForum(
  4.   IN parent SMALLINT,
  5.   IN fmType TINYINT(1),
  6.   IN fmName VARCHAR(128),
  7.   IN fmComments VARCHAR(255),
  8.   IN fmStatus TINYINT(1))
  9. BEGIN
  10.   DECLARE parentleft, parentright, s SMALLINT DEFAULT 0;
  11.   SELECT left_id, right_id INTO parentleft, parentright FROM forums WHERE id = parent;
  12.   IF FOUND_ROWS() = 1 THEN
  13.     BEGIN
  14.       UPDATE forums SET right_id = right_id + 2 WHERE right_id > parentleft;
  15.       UPDATE forums SET left_id = left_id + 2 WHERE left_id > parentleft;
  16.       INSERT INTO forums (pf_id, left_id, right_id, forum_type, forum_name, comments, STATUS)
  17.         VALUES (parent, parentleft + 1, parentleft + 2, fmType, fmName, fmComments, fmStatus);
  18.     END;
  19.   ELSE
  20.    BEGIN
  21.     SELECT IF(MAX(right_id),MAX(right_id),0) INTO s FROM forums;
  22.     INSERT INTO forums (pf_id, left_id, right_id, forum_type, forum_name, comments, STATUS)
  23.         VALUES (parent, s+1, s+2, fmType, fmName, fmComments, fmStatus);
  24.     END;
  25.   END IF;
  26.   BEGIN
  27.     SELECT LAST_INSERT_ID() FROM forums;
  28.   END;
  29. END;
  30. GO
  31. DELIMITER ;

Código SQL:
Ver original
  1. DROP PROCEDURE IF EXISTS deleteForums;
  2. DELIMITER GO
  3. CREATE PROCEDURE deleteForums( IN node SMALLINT )
  4. BEGIN
  5.   DECLARE thisleft, thisright, thiswidth SMALLINT DEFAULT 0;
  6.   SELECT left_id, right_id, ( right_id - left_id + 1 ) INTO thisleft, thisright, thiswidth FROM forums WHERE id = node;
  7.   DELETE FROM forums WHERE left_id BETWEEN thisleft AND thisright;  
  8.   UPDATE forums SET left_id = left_id - thiswidth WHERE left_id > thisleft;
  9.   UPDATE forums SET right_id = right_id - thiswidth WHERE right_id > thisright;
  10. END;
  11. GO
  12. DELIMITER ;

Código SQL:
Ver original
  1. DROP PROCEDURE IF EXISTS upDownForum;
  2. DELIMITER GO
  3. CREATE PROCEDURE upDownForum( IN node SMALLINT, IN op VARCHAR(4) )
  4. BEGIN
  5.   DECLARE leftnode, thisleft, thisright, thiswidth, thisjump, behind_left, behind_right SMALLINT DEFAULT 0;
  6.   SELECT left_id, right_id, ( right_id - left_id + 1 ) INTO thisleft, thisright, thiswidth FROM forums WHERE id = node;
  7.   SELECT left_id, right_id, IF( op = 'UP', (left_id - thisleft), ( right_id + 1 - thisleft ) ) INTO behind_left, behind_right, thisjump FROM forums WHERE IF( op = 'UP', (right_id = thisleft - 1), (left_id = thisright + 1));
  8.   IF FOUND_ROWS() = 1 THEN
  9.   IF thisjump > 0 THEN
  10.    BEGIN
  11.       UPDATE forums SET left_id = left_id + thiswidth WHERE left_id > behind_right;
  12.       UPDATE forums SET right_id = right_id + thiswidth WHERE right_id > behind_right;
  13.       UPDATE forums SET left_id = left_id + thisjump, right_id = right_id + thisjump WHERE left_id BETWEEN thisleft AND thisright;
  14.       UPDATE forums SET left_id = left_id - thiswidth WHERE left_id > thisright;
  15.       UPDATE forums SET right_id = right_id - thiswidth WHERE right_id > thisright;
  16.    END;
  17.    ELSE
  18.    BEGIN
  19.       UPDATE forums SET left_id = left_id + thiswidth WHERE left_id >= behind_left;
  20.       UPDATE forums SET right_id = right_id + thiswidth WHERE right_id >= behind_left;
  21.       UPDATE forums SET left_id = left_id + thisjump - thiswidth, right_id = right_id + thisjump - thiswidth WHERE left_id BETWEEN thisleft + thiswidth AND thisright + thiswidth;
  22.       UPDATE forums SET left_id = left_id - thiswidth WHERE left_id > thisright;
  23.       UPDATE forums SET right_id = right_id - thiswidth WHERE right_id > thisright;
  24.    END;
  25.   END IF;
  26.   END IF;
  27. END;
  28. GO
  29. DELIMITER ;

Nota: el de insertar registro habría que mejorarlo, ya que inserta sólo al comienzo

Etiquetas: pasar, procedimiento, tablas, almacenar
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 23:22.