我有2张tablecustlogin和custinfo`:
和
custlogin:
custlogin
custid int primary key auto notnull custusename varchar(25) custpassword varchar(50)
custinfo:
custinfo
custid foriegnkey custlogin.custid ondelete set NULL custfirstname varchar(25) custlastname varchar(25) custaddress varchar(100)
我想编写一个存储过程,该过程将插入两个表中
更准确地说,是插入custloginwith custusername custpassword,它将返回custid用作的外键custinfo。
custusername custpassword
custid
我进行了很多搜索,但未找到任何解决方案。
如下图所示。SCOPE_IDENTITY()在这种情况下,您可以用来获取最后一个自动生成的ID以及此存储过程的作用域:
SCOPE_IDENTITY()
create procedure NameOfYourProcedureHere as begin SET NOCOUNT ON; SET XACT_ABORT ON; insert into custlogin(custusename, custpassword) values ('','') -- put values here (from parameters?) insert into custinfo(custid, custfirstname, custlastname, custaddress) values (SCOPE_IDENTITY(), '', '', '') -- put other values here (from parameters?) SET NOCOUNT OFF; SET XACT_ABORT OFF; end