2014-01-07 65 views
4

我有2個表,custlogincustinfoSQL Server存儲過程在多個表中插入

custlogin

custid int primary key auto notnull 
custusename varchar(25) 
custpassword varchar(50) 

custinfo

custid foriegnkey custlogin.custid ondelete set NULL 
custfirstname varchar(25) 
custlastname varchar(25) 
custaddress varchar(100) 

我想寫一個存儲過程它將插入到兩個表中

更確切地說,插入custlogincustusername custpassword,這將返回custid用作custinfo外鍵。

我已經搜索很多,但我沒有找到任何解決方案。

+3

你不可能搜索到硬 - http://stackoverflow.com/questions/13318924/how -do -i-insert-into-two-tables-all-at-a-stored-procedure – Goose

+0

檢查此鏈接 - http://stackoverflow.com/questions/5762942/stored-procedure-to-插入兩桌,有關係 – SAT

回答

13

它將如下所示。您可以使用SCOPE_IDENTITY()得到最後自動生成的ID withing這是在這種情況下,這個存儲過程範圍:

create procedure NameOfYourProcedureHere 
as 
begin 

    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?) 

end 
相關問題