Cannot insert the value NULL into column 'Cod_Cli', table 'TrabajoFinal.dbo.Historial_Cli'; column does not allow nulls. INSERT fails.

Leonel Duran 1 Reputation point
2022-12-17T14:44:03.67+00:00

M esta presentado este error al momento de excutar el registro

tengo un proceso almacenamiento creado para la inserción de nuevos clientes

un tabla que registra el historico de la tabla cliente

y un trigger para cada ves que se inserte un nuevo cliente se dispare y guarde el evento que sucedio con la tabla afectada osea cliente

SQL Server Other
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ronen Ariely 15,206 Reputation points
    2022-12-17T15:52:00.207+00:00

    Hi @Leonel Duran

    Bienvenido a los foros de QnA.

    Este foro es para discusiones en inglés. La interfaz aquí aún no es compatible con otros idiomas.

    Usé el traductor de Google para entender tu pregunta, pero responderé en inglés. continuemos la discusión en inglés :-)

    ...

    Welcome to the QnA forums.

    This forum is for discussions in English. The interface here is not yet support other languages.

    I used Google translator to understand your question, but I will answer in English. let's continue the discussion in English :-)

    ----------

    Question in English: Cannot insert the value NULL into column 'Cod_Cli', table 'TrabajoFinal.dbo.History_Cli'; column does not allow nulls. INSERT fails.

    M is presented with this error when executing the record

    I have a storage process created for the insertion of new clients

    a table that records the history of the client table

    and a trigger for each time a new client is inserted, it fires and saves the event that happened with the affected table, that is, client

    ----------

    Answer (English)

    It seems like your table TrabajoFinal.dbo.Historial_Cli have a column named Cod_Cli which is configure not be NULL like bellow sample

    CREATE TABLE Cod_Cli (ColumnA int NOT NULL)  
    GO  
    

    In such column which is configured using "NOT NULL" we cannot insert NULL and the following attempt will raise the error which you get

    INSERT Cod_Cli (ColumnA) VALUES (NULL)  
    GO  
      
    -- Cannot insert the value NULL into column 'ColumnA', table 'tempdb.dbo.Cod_Cli'; column does not allow nulls. INSERT fails.  
    

    271598-image.png

    Option 1: ALTER the table to allow NULL

    ALTER TABLE Cod_Cli  
     ALTER COLUMN ColumnA int NULL  
    GO  
    

    Now we can INSERT NULL

    INSERT Cod_Cli (ColumnA) VALUES (NULL)  
    GO  
    

    271617-image.png

    Option 2: change your INSERT query and make sure that you do not INSERT NULL. You can use the function ISNULL to set the value you insert to something which is not NULL if the value is NULL

    DECLARE @Input INT  
    INSERT Cod_Cli (ColumnA) VALUES (ISNULL(@Input, 0))  
    GO  
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.