Validations in SQL

1

I have the following problem, I have a web application, in which I fill a form in which an image is attached (in sql I only save the name of the image and then open it in another form), when updating the record, if the image is not updated the SP, go and record NULL in the database ... How could I validate that if the image parameter goes empty, I leave the same as it is already in the table ???

The code I have is a simple update to the table

UPDATE [dbo].[FormDetalle] SET [archivoadjunto] = @file,[comentarios] = @Comment,[avance]= @iAvance,fec_finreal=@date
        WHERE [id_1] = @id1 AND [id_accion]=@iAccionID;

the image parameter is @file

    
asked by Richard 18.05.2018 в 17:44
source

2 answers

3

The simplest thing is to use ISNULL :

UPDATE [dbo].[FormDetalle] 
SET [archivoadjunto] = ISNULL(NULLIF(@file,''),[archivoadjunto]),
    [comentarios] = @Comment,
    [avance]= @iAvance,
    fec_finreal=@date
WHERE [id_1] = @id1 
AND [id_accion]=@iAccionID
;
    
answered by 18.05.2018 / 17:50
source
1

Try putting something like this:

if(@file is null)
begin
  UPDATE [dbo].[FormDetalle] SET [comentarios] = @Comment,[avance]= @iAvance,fec_finreal=@date
    WHERE [id_1] = @id1 AND [id_accion]=@iAccionID;
end
else
  begin
    UPDATE [dbo].[FormDetalle] SET [archivoadjunto] = @file,[comentarios] = @Comment,[avance]= @iAvance,fec_finreal=@date
    WHERE [id_1] = @id1 AND [id_accion]=@iAccionID;
  end

Verify if the @file parameter is null so you do not update that field, if it has value then if you update it

    
answered by 18.05.2018 в 18:05