Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   SQL (https://www.clubdelphi.com/foros/forumdisplay.php?f=6)
-   -   Incorrect syntax near (https://www.clubdelphi.com/foros/showthread.php?t=67703)

Ledian_Fdez 30-04-2010 22:44:58

Incorrect syntax near
 
Hola amigos de este foro:
Tengo 2 tablas (Existencia y Salida)

Intento hacer un procedimiento almacenado donde se le de salida a un producto existente y cantidad.


Este es el código:
Código SQL [-]
 create procedure Nueva_BajaIns

@CodSAF varchar(4),
@CodIns varchar(5),
@Cant int,
@Motivo char(2),
@Fecha datetime,
@Observ varchar(50)

AS

declare @errorvar int
set @errorvar = 0

declare @Total int
set @Total = 0

begin transaction
 /* si el producto existe */
 if exists(select * from Existencia where SAF=@CodSAF and INS=@CodIns)  
   begin
     /* guardo en @Total la disponibilidad */
    @Total = select Cant from Existencia where SAF=@CodSAF and INS=@CodIns
    /* si la disponibilidad del producto satisface */
    if(@Total >= @Cant)
    begin
     /* inserto en salida */
     insert into Salidas (CodSAF, CodIns, Cant, Motivo, Fecha, Observ)
     values (@CodSAF, @CodIns, @Cant, @Motivo, @Fecha, @Observ)
     set @errorvar = @errorvar + @@ERROR   
     
     /* Si aun queda disponibilidad    */
     if(@Total > @Cant)
       begin 
  update Existencia
        set Cant = Cant - @Cant  -- realizo la resta para saber disponibilidad una vez hecha la salida
        where SAF=@CodSAF and INS=@CodIns
        set @errorvar = @errorvar + @@ERROR
       end
     else /* si la disponibilidad se queda en cero elimino el producto de existencia */
       begin
  delete from Existencia
  where SAF=@CodSAF and INS=@CodIns
  set @errorvar = @errorvar + @@ERROR
       end
    end
  
 if (@errorvar = 0)
   commit transaction
 else
   rollback transaction

Me da este error: :confused:
Cita:

Server: Msg 170, Level 15, State 1, Procedure Nueva_BajaIns, Line 26
Line 26: Incorrect syntax near '@Total'.

En espera de su ayuda y muchas gracias de antemano.

Salu2,
Ledian.

Ñuño Martínez 02-05-2010 21:04:46

Pregunta: ¿Qué gestor de base de datos estás usando?

Otra pregunta, aún más importante: ¿Cuál es la línea 26? Porque en la que yo creo que es la 26 no hay ningún @Total.

Ledian_Fdez 03-05-2010 14:20:51

Incorrect syntax near
 
Cita:

Empezado por Ñuño Martínez (Mensaje 362606)
Pregunta: ¿Qué gestor de base de datos estás usando?

Otra pregunta, aún más importante: ¿Cuál es la línea 26? Porque en la que yo creo que es la 26 no hay ningún @Total.

EL gestor de base de datos que utilizo es MSSQL Server 2000
La linea 26 es la que esta en rojo.


En espera de su ayuda un delphiano

Salu2,
Ledian.

Ñuño Martínez 03-05-2010 14:24:24

No me había dado cuenta, o quizá lo cambiaste. Da igual, ahora sí la he visto. Que conste que yo de SQL sólo sé hacer consultas simples con SELECT, INSERT, UPDATE y DELETE.

Dicho esto, se me ocurre que en todas las asignaciones utilizas SET, pero en la línea del error no. Quizá sea eso, dicho desde la confesa ignorancia.

Ledian_Fdez 03-05-2010 14:41:26

Ok
 
Ya lo resolví, me quedo así.

Código SQL [-]
alter  procedure Nueva_BajaIns

@CodSAF varchar(4),
@CodIns varchar(5),
@Cant int,
@Motivo char(2),
@Fecha datetime,
@Observ varchar(50)

AS

declare @errorvar int
set @errorvar = 0

declare @Total int
set @Total = 0

begin transaction
 if exists(select * from Existencia where SAF=@CodSAF and INS=@CodIns)  
   begin
    select @Total = Cant from Existencia where SAF=@CodSAF and INS=@CodIns
    if(@Total >= @Cant)
      begin
       insert into Salidas (CodSAF, CodIns, Cant, Motivo, Fecha, Observ)
       values (@CodSAF, @CodIns, @Cant, @Motivo, @Fecha, @Observ)
       set @errorvar = @errorvar + @@ERROR      
       if(@Total > @Cant)
         begin 
    update Existencia
          set Cant = Cant - @Cant
          where SAF=@CodSAF and INS=@CodIns
          set @errorvar = @errorvar + @@ERROR
         end
       else 
         begin
    delete from Existencia
    where SAF=@CodSAF and INS=@CodIns
    set @errorvar = @errorvar + @@ERROR
         end
      end
   end
 if (@errorvar = 0)
   commit transaction
 else
  rollback transaction

Muchas gracias de todos modos !!!


Salu2,
Ledian.


La franja horaria es GMT +2. Ahora son las 16:46:37.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi