Problem when capturing a data by SQL Server stored procedure

0

I am working with this table in SQL Server 2014:

create table suceso 
(
  id int identity (1,1) not null,
  codMod varchar (10) not null,
  fecha date not null
  constraint pk_suceso primary key (id)
);
go

And this is the stored procedure:

create procedure sp_insNuevoModulo
@codMod varchar (10)
as
begin 
  declare @fecha date;
  set @fecha = (select fecha from suceso where codMod = @codMod and fecha = 
  (select convert (varchar(10), GETDATE(),103)));
  if (@fecha = null)
     insert into suceso values (@codMod, (select convert (varchar(10), GETDATE(),103)));
end
go

In short, if the return becomes null, I would have to add a new datum, otherwise, nothing.

when I run:

 exec sp_insNuevoModulo 'COD01';

I get the message: "Commands completed correctly." but when consulting the table is empty.

    
asked by Alvaro Mauricio Angulo Riquelm 21.03.2018 в 13:04
source

1 answer

0

To solve the problem you should only change in the Stored Procedure this: if (@fecha = null) for this: if @fecha is null , since this @fecha = null is practically an assignment and not a question.

  create procedure sp_insNuevoModulo
    @codMod varchar (10)
    as
    begin 
      declare @fecha date;
      set @fecha = (select fecha from suceso where codMod = @codMod and fecha = 
      (select convert (varchar(10), GETDATE(),103)));
      if @fecha is null 
      begin 
         insert into suceso values (@codMod, (select convert (varchar(10), GETDATE(),103)));
      end
    end
    go

Greetings!

    
answered by 21.03.2018 / 13:27
source