0
Good morning everyone I am trying to change a precedent I created to insert a new book in three tables the table has autoencrement in the id_book column, and tables B and C are not null, I am using @@IDENTITY to get the last id_book value from table A but when trying to insert a book it says that the id_book column of table C cannot accept null value, I believe that for table C @@IDENTITY does not get the last value from the previous table someone could help me? And please excuse me if you’re a little confused I’m new here and I don’t know how to separate the code from the text.
ALTER PROCEDURE adicionarNovoLivro(
@nome_livro varchar(255),
@nome_autor varchar(255),
@ano_livro int,
@nome_editora varchar(100),
@preco_livro float
)
AS
BEGIN
INSERT INTO tbl_LivrosA VALUES(@nome_livro, @nome_autor, @ano_livro, @nome_editora, @preco_livro)
INSERT INTO tbl_LivrosB VALUES(@@IDENTITY, @nome_livro, @nome_autor, @ano_livro, @nome_editora, @preco_livro)
INSERT INTO tbl_LivrosC VALUES(@@IDENTITY, @nome_livro, @nome_autor, @ano_livro, @nome_editora, @preco_livro)
END
EXEC adicionarNovoLivro 'Nome na Taverna', 'Álvarez de Azevedo', 1855, 'Editora Abril', 16.96
You could create a variable, assign the @@IDENTITY value and after adding the variable to the Inserts.
– Reginaldo Rigo
Good morning Reginaldo I do not know if I did exactly as you are proposing but I had tried to do it and also could not give the same error, it works @@IDENTITY only if I do with two tables then the second IDENTITY takes the id of the previous one with three the third table is without id.
– wilder
Try to do it the way below.
– Reginaldo Rigo
Possible duplicate of How to get the Primary key Identity of a entered record?
– Sorack
@Wilder The correct name of the writer is Álvares de Azevedo.
– José Diz
Was any of the answer helpful? Don’t forget to choose one and mark it so it can be used if someone has a similar question!
– Sorack