1

这是我的存储过程:

create procedure insertBook2 @author varchar(32), @title varchar(32), @pages int
as
begin
    declare @identity int
    insert into inventar(author,class) values (@author,'book')
    select @IDENTITY=SCOPE_IDENTITY()
    insert into book(id,title,pages) values (@identity,@title,@pages)
end  

和表关系

inventar(id,class,author)
book(id,title,pages);

我执行了程序,但它影响了 3 行。在表格簿值上添加了另一行: id=id title=NULL 和 pages=NULL在具有真实信息的行之前:/ 谢谢!

4

1 回答 1

0

Your stored procedure looks fine in that it should create the correct number of rows, that is one in each table.

However, I see you are doing

select @IDENTITY=SCOPE_IDENTITY()

whereas you should really use

set @IDENTITY=SCOPE_IDENTITY()

as you're setting a variable, not querying a table.

That would cause you to get extra output saying "1 rows affected", which is perhaps what caused your confusion.

于 2013-04-04T14:25:41.457 回答