Home   Artículos   Recursos   Foros   
Artíclos recientes publicados en Latindevelopers:

Visual C++: NSDoubleEdit: Un control para el manejo de números decimales en Visual C++.
Visual C++: Implementando una Calculadora en Visual C++
Visual C++: CCommandLine: Una clase para el uso de la linea de comando
Visual C++: Una clase para el manejo del Registro


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 el 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
delimiter //

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

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

select ninteresptp into 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
        resultado=(interes1 + interes2 + interes2)/3;
   update ttcontrollicita set NInteresGen=interes2 where NLicitacion_id=registro;   
     else
        if interes1=0 and interes2=0 and intres3>0 then
      resultado=(interes1 + interes2 + interes2)/3;
      update ttcontrollicita set NInteresGen=interes3 where NLicitacion_id=registro;
   else
     if interes1>0 and interes2>0 and intres3=0 then
        resultado=(interes1 + interes2)/2;
        update ttcontrollicita set NInteresGen=resultado where NLicitacion_id=registro;
     else
        if interes1>0 and interes2=0 and intres3>0 then
           resultado=(interes1 + interes3)/2;
      update ttcontrollicita set NInteresGen=resultado where NLicitacion_id=registro;
        else
           if interes1=0 and interes2>0 and intres3>0 then
              resultado=(interes2 + interes3)/2;
         update ttcontrollicita set NInteresGen=resultado where NLicitacion_id=registro;
           else
              if interes1>0 and interes2>0 and intres3>0 then
                 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;


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:58 pm

Re: Ayuda Urgente con Store Procedures en MySQL

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

Que version de mysql estas utilizando?
ivancp
Programador Experimentado
Programador Experimentado
 
Mensajes: 369
Registrado: Jue Sep 06, 2007 12:57 pm

Re: Ayuda Urgente con Store Procedures en MySQL

Notapor neptuno75 el 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:58 pm

Re: Ayuda Urgente con Store Procedures en MySQL

Notapor ivancp el Mié Ene 23, 2008 9:22 am

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

Código: Seleccionar todo
delimiter //

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

...

END//

delimiter ;
ivancp
Programador Experimentado
Programador Experimentado
 
Mensajes: 369
Registrado: Jue Sep 06, 2007 12:57 pm

Re: Ayuda Urgente con Store Procedures en MySQL

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

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

Código: Seleccionar todo
DELIMITER //

CREATE PROCEDURE `administra_nova`.`sp_Actualiza_Interes` (in registro int)
BEGIN
DECLARE interes1,interes2 int;
DECLARE 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
        resultado=(interes1 + interes2)/2;
        update ttcontrollicita set NInteresGen=resultado where NLicitacion_id=registro;
     else
        if interes1>0 and interes2=0 and intres3>0 then
           resultado=(interes1 + interes3)/2;
      update ttcontrollicita set NInteresGen=resultado where NLicitacion_id=registro;
        else
           if interes1=0 and interes2>0 and intres3>0 then
              resultado=(interes2 + interes3)/2;
         update ttcontrollicita set NInteresGen=resultado where NLicitacion_id=registro;
           else
              if interes1>0 and interes2>0 and intres3>0 then
                 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 ;


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:58 pm

Re: Ayuda Urgente con Store Procedures en MySQL

Notapor latindeveloper el Mié Ene 23, 2008 3:40 pm

El codigo que enviaste genera un error diferente:

Código: Seleccionar todo
ERROR 1064 (42000): 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 '=(int
eres1 + interes2)/2;
        update ttcontrollicita set NInteresGen=resulta' at line 22


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

Código: Seleccionar todo
     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


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

Re: Ayuda Urgente con Store Procedures en MySQL

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

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

Código: Seleccionar todo
delimiter //

CREATE PROCEDURE `administra_nova`.`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;

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:58 pm

Re: Ayuda Urgente con Store Procedures en MySQL

Notapor ivancp el Mié Ene 23, 2008 4:35 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
[code] Codigo fuente [/code]

Puedes seleccionar el texto y presionar el boton code de la barra de herramientas.
ivancp
Programador Experimentado
Programador Experimentado
 
Mensajes: 369
Registrado: Jue Sep 06, 2007 12:57 pm

Re: Ayuda Urgente con Store Procedures en MySQL

Notapor waltico el Jue Ene 24, 2008 10:04 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
CREATE PROCEDURE procedure1                /* mombre del procedimiento */
(IN parameter1 INTEGER)                    /* parametros */
BEGIN                                      /* Comienzo del procedimiento */
  DECLARE variable1 CHAR(10);                /* variables */
  IF parameter1 = 17 THEN                    /* comienzo de la condicion IF */
    SET variable1 = 'demo1';                   /* asignación */
  ELSE
    SET variable1 = 'demo2';                  /* asignación alterna */
  END IF;                                   /* fin de la condición IF */
  INSERT INTO table1 VALUES (variable1);    /* ingreso  */
END                                       /* fin del procedimiento */
by: Oscar Walther Huanca Torres
Web: http://waltico.wordpress.com
E-Mail: walticogt + yahoo.com
Avatar de Usuario
waltico
Programador
Programador
 
Mensajes: 153
Registrado: Sab Jun 21, 2003 4:04 pm
Ubicación: Puno - Perú

Re: Ayuda Urgente con Store Procedures en MySQL

Notapor neptuno75 el 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:58 pm


Volver a MySQL

¿Quién está conectado?

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