我有一个表'Person',其列为'Person_id作为主键','DOB'和'place',如下所示:
'Person'
Person_id |Name|DOB | place
另一个表是“employee”,其中emp_id是主键,如下所示:
'employee'
Person_id |emp_id|dateofjoin
还有一个表“详情”:
'Details'
emp_id|competency|rating
现在我想要的是,一旦我添加'Person'表,将两个表的其余部分详细描述为'Employe'和'Details',以便在Person表中添加的新Person更新。那么,我怎么能用sql查询呢?另外我想清楚我对数据库不是很熟悉。
答案 0 :(得分:2)
我认为你的事情是这样的(对于SQL Server):
Create Procedure dbo.CreateMyEmployee ( @empName varchar(50),
@dob datetime,
@doj datetime,
@place as varchar(100),
@competency varchar(100),
@rating int)
As
Begin
Declare @empId int
Begin Transaction
Begin Try
Insert into Person (Name, DOB, Place)
Values ( @empName, @dob, @place)
Insert into employe (Name, dateofJoin) -- Assuming emp_id is identity columen
Values ( @empName, @doj)
Select @empId = SCOPE_IDENTITY()
Insert Into Details(emp_id, competency, rating)
Values (@empId, @competency, @rating)
Commit transaction
End Try
Begin Catch
Rollback Transaction
SELECT ERROR_NUMBER() AS ErrorNumber, ERROR_MESSAGE() AS ErrorMessage
End Catch
End