Error to insert data into a table

Asked

Viewed 48 times

1

Please, I am trying to make an Insert in a temporary table but this giving the error reported below:

code used for insertion

insert into #basehot(email)
select distinct a.email from #email a right join #basehot b on a.cod_pes = b.cod_pes
where a.ordem = 1

code error:

Mensagem 515, Nível 16, Estado 2, Linha 52
Cannot insert the value NULL into column 'COD_PES', table 'tempdb.dbo.#BASEHOT____________________________________________________________________________________________________________00000000A8FD'; column does not allow nulls. INSERT fails.
The statement has been terminated.
  • the column COD_PES is numerical ?

  • 1

    "Cannot Insert the value NULL into column 'COD_PES'", you are trying to insert a null value in the column COD_PES that does not accept null.

  • You are giving select only in email, so you are only entering the email field, and you cannot leave COD_PES null.

  • The Cod_pes column is numeric, and I am only selecting the email because the information already exists in the cod_pes field I am just wanting to fill in the email.

  • If the record already exists in the table #basehot then you should use UPDATE and not INSERT to update the email field only.

  • Good morning, I tried to do with the update but I am not able to do as it is giving the following error if you can help me please: Code update #BASEHOT set email = &#xA;(select distinct a.email from #email a inner join #BASEHOT b on a.COD_PES=b.COD_PES &#xA;) Error: Mensagem 512, Nível 16, Estado 1, Linha 52&#xA;Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. Thank you.

Show 1 more comment

1 answer

0

You are giving select only in the a.email field, so you are only entering the email field in the #basehot table, and the error indicates that you cannot leave the COD_PES field null.

Try to put cod_pes on Insert and a.cod_pes on select:

insert into #basehot(cod_pes, email)
select distinct a.cod_pes, a.email from #email a right join #basehot b on a.cod_pes = b.cod_pes
where a.ordem = 1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.