我在数据库中创建了3个表 tbProducts,tbLineOfAuthority,tbProductLineOfAuthority
tbProducts包含以下领域
tbLineOfAuthority包含以下领域
tbProductLineOfAuthority包含以下领域
现在tbLineOfAuthority包含以下数据
在前端,我有以下场地的表格
如果用户使用以下数据填写上述表格
现在我的问题如何根据上述要求在ms sql server中执行插入操作,如果有两个以上的表与外键相关。
我已将存储过程编写为
Insert into tbProduct(ProductName, ProductDescription)
Values(@ProductName, @ProductDescription)
我很困惑,我将如何编写一个自动更新tbProductLineOfAuthority(ProductId和LineOfAuthorityId)的存储过程
当用户从复选框中选择多个权限时,会发生什么 在表格中,我的意思是如果用户将选择临时性,信用卡和借记卡。
请帮帮我!!!
答案 0 :(得分:0)
为此,您必须为tbProducts
和tbLineOfAuthority
表传递3个参数,如下所示。插入记录后,使用SCOPE_IDENTITY()
获取新生成的IDENTITY
值[考虑到这两个表中的PK列均为IDENTITY
列]。
create procedure sp_insert_ops (@ProductName,
@ProductDescription,
@tbLineOfAuthority)
as
begin
declare @tbProduct_ID INT;
Insert into tbProduct(ProductName, ProductDescription)
Values(@ProductName, @ProductDescription);
SELECT @tbProduct_ID = SCOPE_IDENTITY();
INSERT INTO tbProductLineOfAuthority(ProductId, LineOfAuthority)
VALUES(@tbProduct_ID, NULL);
end