Ayuda Urgente con Store Procedures en MySQL

Foro sobre el popular gestor de base de datos MySQL.

Ayuda Urgente con Store Procedures en MySQL

Notapor neptuno75 » Lun Ene 21, 2008 2:08 pm

Hola a todos, soy nuevo trabajando con los store procedures en mysql y tengo un problema que no se como solucionar, el detalle que tengo es el siguiente, yo estoy creando el siguiente store:
Código: Seleccionar todo
  1. delimiter //

  2.  

  3. CREATE PROCEDURE `administra_nova`.`sp_Actualiza_Interes` (registro int)

  4. BEGIN

  5.  DECLARE interes1, interes2 int;

  6.  DECLARE interes3, resultado int;

  7.  

  8.  select ninteresjdh,ninteresfdh

  9.    into interes1,interes2

  10.    from ttcontrollicita

  11.   where nlicitacion_id=registro;

  12.  

  13.  select ninteresptp into interes3

  14.    from ttcontrollicita

  15.   where nlicitacion_id=registro;

  16.  

  17.  if interes1=0 and intres2=0 and interes3=0 then

  18.    update ttcontrollicita set NInteresGen=0 where NLicitacion_id=registro;

  19.  else

  20.    if interes1>0 and interes2=0 and intres3=0 then

  21.       update ttcontrollicita set NInteresGen=interes1 where NLicitacion_id=registro;

  22.    else

  23.      if interes1=0 and interes2>0 and intres3=0 then

  24.         resultado=(interes1 + interes2 + interes2)/3;

  25.         update ttcontrollicita set NInteresGen=interes2 where NLicitacion_id=registro; 

  26.      else

  27.         if interes1=0 and interes2=0 and intres3>0 then

  28.            resultado=(interes1 + interes2 + interes2)/3;

  29.            update ttcontrollicita set NInteresGen=interes3 where NLicitacion_id=registro;

  30.         else

  31.           if interes1>0 and interes2>0 and intres3=0 then

  32.              resultado=(interes1 + interes2)/2;

  33.              update ttcontrollicita set NInteresGen=resultado where NLicitacion_id=registro;

  34.           else

  35.              if interes1>0 and interes2=0 and intres3>0 then

  36.                 resultado=(interes1 + interes3)/2;

  37.                 update ttcontrollicita set NInteresGen=resultado where NLicitacion_id=registro;

  38.              else

  39.                 if interes1=0 and interes2>0 and intres3>0 then

  40.                    resultado=(interes2 + interes3)/2;

  41.                    update ttcontrollicita set NInteresGen=resultado where NLicitacion_id=registro;

  42.                 else

  43.                    if interes1>0 and interes2>0 and intres3>0 then

  44.                       resultado=(interes1 + interes2 + interes3)/3;

  45.                       update ttcontrollicita set NInteresGen=resultado where NLicitacion_id=registro;

  46.                    end if;

  47.                 end if;

  48.              end if;

  49.           end if;

  50.         end if;

  51.       end if;

  52.     end if;

  53.   end if;

  54. END;

  55.  

  56. delimiter;



Pero al momento de querer correrlo, para que sea guardado en la b.d. me manda el siguiente mensaje:

Error Code : 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DECLARE interes3, resultado int;
select ninteresjdh,ninteresfdh
into interes' at line 1 :(
(16 ms taken)

Ya estuve revisando varios manuales de MYSQL pero sinceramente ya me desespere y no encuentro lo que necesito, quisiera ver si me pueden ayudar.

Gracias.
neptuno75
Novato
Novato
 
Mensajes: 5
Registrado: Lun Ene 21, 2008 1:59 pm


Re: Ayuda Urgente con Store Procedures en MySQL

Notapor ivancp » Lun Ene 21, 2008 4:58 pm

Que version de mysql estas utilizando?
Imagen @latindev | Mi Blog
Por favor lee las reglas del foro
Avatar de Usuario
ivancp
Colaborador
Colaborador
 
Mensajes: 680
Registrado: Jue Sep 06, 2007 12:58 pm


Re: Ayuda Urgente con Store Procedures en MySQL

Notapor neptuno75 » Mar Ene 22, 2008 9:17 am

Estoy trabajando con la ver. 5.0.19NT.
neptuno75
Novato
Novato
 
Mensajes: 5
Registrado: Lun Ene 21, 2008 1:59 pm

Re: Ayuda Urgente con Store Procedures en MySQL

Notapor ivancp » Mié Ene 23, 2008 9:23 am

El error esta en que el ultimo END debería terminar con //, no con punto y coma (;)

Código: Seleccionar todo
  1. delimiter //

  2.  

  3. CREATE PROCEDURE ... (registro int)

  4. BEGIN

  5. DECLARE interes1, interes2 int;

  6. DECLARE interes3, resultado int;

  7.  

  8. ...

  9.  

  10. END//

  11.  

  12. delimiter ;

Imagen @latindev | Mi Blog
Por favor lee las reglas del foro
Avatar de Usuario
ivancp
Colaborador
Colaborador
 
Mensajes: 680
Registrado: Jue Sep 06, 2007 12:58 pm

Re: Ayuda Urgente con Store Procedures en MySQL

Notapor neptuno75 » Mié Ene 23, 2008 12:40 pm

Ya corregi el store como me lo mencionaste, quedo de la siguiente manera:

Código: Seleccionar todo
  1. DELIMITER //

  2.  

  3. CREATE PROCEDURE `administra_nova`.`sp_Actualiza_Interes` (in registro int)

  4. BEGIN

  5. DECLARE interes1,interes2 int;

  6. DECLARE interes3,resultado int;

  7.  

  8. select ninteresjdh,ninteresfdh,ninteresptp

  9.    into interes1,interes2,interes3

  10.    from ttcontrollicita

  11.   where nlicitacion_id=registro;

  12.  

  13. if interes1=0 and intres2=0 and interes3=0 then

  14.    update ttcontrollicita set NInteresGen=0 where NLicitacion_id=registro;

  15. else

  16.    if interes1>0 and interes2=0 and intres3=0 then

  17.       update ttcontrollicita set NInteresGen=interes1 where NLicitacion_id=registro;

  18.    else

  19.      if interes1=0 and interes2>0 and intres3=0 then

  20.    update ttcontrollicita set NInteresGen=interes2 where NLicitacion_id=registro;  

  21.      else

  22.         if interes1=0 and interes2=0 and intres3>0 then

  23.       update ttcontrollicita set NInteresGen=interes3 where NLicitacion_id=registro;

  24.    else

  25.      if interes1>0 and interes2>0 and intres3=0 then

  26.         resultado=(interes1 + interes2)/2;

  27.         update ttcontrollicita set NInteresGen=resultado where NLicitacion_id=registro;

  28.      else

  29.         if interes1>0 and interes2=0 and intres3>0 then

  30.            resultado=(interes1 + interes3)/2;

  31.       update ttcontrollicita set NInteresGen=resultado where NLicitacion_id=registro;

  32.         else

  33.            if interes1=0 and interes2>0 and intres3>0 then

  34.               resultado=(interes2 + interes3)/2;

  35.          update ttcontrollicita set NInteresGen=resultado where NLicitacion_id=registro;

  36.            else

  37.               if interes1>0 and interes2>0 and intres3>0 then

  38.                  resultado=(interes1 + interes2 + interes3)/3;

  39.             update ttcontrollicita set NInteresGen=resultado where NLicitacion_id=registro;

  40.                    end if;

  41.                 end if;

  42.            end if;

  43.           end if;

  44.         end if;

  45.       end if;

  46.     end if;

  47.   end if;

  48. END //

  49.  

  50. DELIMITER ;



Pero me esta generando ahora el siguiente error:

Error Code : 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DECLARE interes3,resultado int;
select ninteresjdh,ninteresfdh,ninteresptp
in' at line 1
(0 ms taken)

Como vez?
Gracias por Ayudarme...
neptuno75
Novato
Novato
 
Mensajes: 5
Registrado: Lun Ene 21, 2008 1:59 pm

Re: Ayuda Urgente con Store Procedures en MySQL

Notapor latindev » Mié Ene 23, 2008 3:40 pm

El codigo que enviaste genera un error diferente:

Código: Seleccionar todo
  1. ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that

  2. corresponds to your MySQL server version for the right syntax to use near '=(int

  3. eres1 + interes2)/2;

  4.        update ttcontrollicita set NInteresGen=resulta' at line 22

  5.  



Es por que en MySQL la asignacion de variables se hace mediante SET:

Código: Seleccionar todo
  1.      if interes1>0 and interes2>0 and intres3=0 then

  2.         SET resultado = (interes1 + interes2)/2;

  3.         update ttcontrollicita set NInteresGen=resultado where NLicitacion_id=registro;

  4.      else



Yo estoy utilizando 5.0.45-community-nt
Imagen
Avatar de Usuario
latindev
Administrador
Administrador
 
Mensajes: 1062
Registrado: Lun Jun 02, 2003 8:30 pm
Ubicación: Peru

Re: Ayuda Urgente con Store Procedures en MySQL

Notapor neptuno75 » Mié Ene 23, 2008 4:21 pm

Ya cambie nuevamente el store, ahora quedo de la siguiente manera:

Código: Seleccionar todo
  1. delimiter //

  2.  

  3. CREATE PROCEDURE `administra_nova`.`sp_Actualiza_Interes` (registro int)

  4. BEGIN

  5. DECLARE interes1,interes2,interes3,resultado int;

  6.  

  7. select ninteresjdh,ninteresfdh,ninteresptp

  8.    into interes1,interes2,interes3

  9.    from ttcontrollicita

  10.   where nlicitacion_id=registro;

  11.  

  12. if interes1=0 and intres2=0 and interes3=0 then

  13.    update ttcontrollicita set NInteresGen=0 where NLicitacion_id=registro;

  14. else

  15.    if interes1>0 and interes2=0 and intres3=0 then

  16.       update ttcontrollicita set NInteresGen=interes1 where NLicitacion_id=registro;

  17.    else

  18.      if interes1=0 and interes2>0 and intres3=0 then

  19.    update ttcontrollicita set NInteresGen=interes2 where NLicitacion_id=registro;  

  20.      else

  21.         if interes1=0 and interes2=0 and intres3>0 then

  22.           update ttcontrollicita set NInteresGen=interes3 where NLicitacion_id=registro;

  23.    else

  24.      if interes1>0 and interes2>0 and intres3=0 then

  25.         set resultado=(interes1 + interes2)/2;

  26.         update ttcontrollicita set NInteresGen=resultado where NLicitacion_id=registro;

  27.      else

  28.         if interes1>0 and interes2=0 and intres3>0 then

  29.            set resultado=(interes1 + interes3)/2;

  30.       update ttcontrollicita set NInteresGen=resultado where NLicitacion_id=registro;

  31.         else

  32.            if interes1=0 and interes2>0 and intres3>0 then

  33.               set resultado=(interes2 + interes3)/2;

  34.          update ttcontrollicita set NInteresGen=resultado where NLicitacion_id=registro;

  35.            else

  36.               if interes1>0 and interes2>0 and intres3>0 then

  37.                  set resultado=(interes1 + interes2 + interes3)/3;

  38.             update ttcontrollicita set NInteresGen=resultado where NLicitacion_id=registro;

  39.                    end if;

  40.                 end if;

  41.            end if;

  42.           end if;

  43.         end if;

  44.       end if;

  45.     end if;

  46.   end if;

  47. END//

  48.  

  49. delimiter;


Pero me esta generando el siguiente error:

Error Code : 1327
Undeclared variable: interes1
(0 ms taken)

se supone que yo ya declare esa variable que me pide no?'
Gracias por su ayuda
neptuno75
Novato
Novato
 
Mensajes: 5
Registrado: Lun Ene 21, 2008 1:59 pm

Re: Ayuda Urgente con Store Procedures en MySQL

Notapor ivancp » Mié Ene 23, 2008 4:36 pm

El codigo que enviaste ya no genera ningun error en mi MySQL. Yo estoy utilizando 5.0.51-community-nt, te recomiendo que descargues la ultima version disponible de mysql.com.

Antes de actualizar tu servidor, debes sacarle una copia a la base de datos.

Nota importante: El codigo que envíes encierralo entre las etiquetas (bbcodes) que corresponden al codigo (code):

Código: Seleccionar todo
  1.  [code] Codigo fuente [/code]


Puedes seleccionar el texto y presionar el boton code de la barra de herramientas.
Imagen @latindev | Mi Blog
Por favor lee las reglas del foro
Avatar de Usuario
ivancp
Colaborador
Colaborador
 
Mensajes: 680
Registrado: Jue Sep 06, 2007 12:58 pm

Re: Ayuda Urgente con Store Procedures en MySQL

Notapor waltico » Jue Ene 24, 2008 10:05 am

Ya la pregunta fue constesta... pero sin mas puedo sugerir

Algunos links donde podras informarte más acerca de Store Procedures
http://dev.mysql.com/tech-resources/...procedures.pdf

http://dev.mysql.com/tech-resources/art ... dures.html

Código: Seleccionar todo
  1. CREATE PROCEDURE procedure1                /* mombre del procedimiento */

  2. (IN parameter1 INTEGER)                    /* parametros */

  3. BEGIN                                      /* Comienzo del procedimiento */

  4.   DECLARE variable1 CHAR(10);                /* variables */

  5.   IF parameter1 = 17 THEN                    /* comienzo de la condicion IF */

  6.     SET variable1 = 'demo1';                   /* asignación */

  7.   ELSE

  8.     SET variable1 = 'demo2';                  /* asignación alterna */

  9.   END IF;                                   /* fin de la condición IF */

  10.   INSERT INTO table1 VALUES (variable1);    /* ingreso  */

  11. END                                       /* fin del procedimiento */

Avatar de Usuario
waltico
Colaborador
Colaborador
 
Mensajes: 183
Registrado: Sab Jun 21, 2003 4:04 pm
Ubicación: Puno

Re: Ayuda Urgente con Store Procedures en MySQL

Notapor neptuno75 » Lun Ene 28, 2008 5:43 pm

Sigo con el problema de mi store procedure, ahora me esta marcando el siguiente error:

Error Code : 1327
Undeclared variable: interes1
(0 ms taken)

Y el store procedure como quedo es:

delimiter //

CREATE PROCEDURE sp_Actualiza_Interes (registro int)
BEGIN
DECLARE interes1, interes2, interes3, resultado int;

select ninteresjdh,ninteresfdh,ninteresptp
into interes1,interes2,interes3
from ttcontrollicita
where nlicitacion_id=registro;

if interes1=0 and intres2=0 and interes3=0 then
update ttcontrollicita set NInteresGen=0 where NLicitacion_id=registro;
else
if interes1>0 and interes2=0 and intres3=0 then
update ttcontrollicita set NInteresGen=interes1 where NLicitacion_id=registro;
else
if interes1=0 and interes2>0 and intres3=0 then
update ttcontrollicita set NInteresGen=interes2 where NLicitacion_id=registro;
else
if interes1=0 and interes2=0 and intres3>0 then
update ttcontrollicita set NInteresGen=interes3 where NLicitacion_id=registro;
else
if interes1>0 and interes2>0 and intres3=0 then
set resultado=(interes1 + interes2)/2;
update ttcontrollicita set NInteresGen=resultado where NLicitacion_id=registro;
else
if interes1>0 and interes2=0 and intres3>0 then
set resultado=(interes1 + interes3)/2;
update ttcontrollicita set NInteresGen=resultado where NLicitacion_id=registro;
else
if interes1=0 and interes2>0 and intres3>0 then
set resultado=(interes2 + interes3)/2;
update ttcontrollicita set NInteresGen=resultado where NLicitacion_id=registro;
else
if interes1>0 and interes2>0 and intres3>0 then
set resultado=(interes1 + interes2 + interes3)/3;
update ttcontrollicita set NInteresGen=resultado where NLicitacion_id=registro;
end if;
end if;
end if;
end if;
end if;
end if;
end if;
end if;
END//

delimiter;

Ya hice la actualización de mi servidor de datos, como se muestra a continuación:

select version()
Resultado:
5.0.45-community-nt

Gracias por su ayuda
neptuno75
Novato
Novato
 
Mensajes: 5
Registrado: Lun Ene 21, 2008 1:59 pm


    

Volver a MySQL

¿Quién está conectado?

Usuarios navegando por este Foro: No hay usuarios registrados visitando el Foro y 0 invitados