本文介绍了插入外键值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑这3个表

Consider these 3 tables

table1-tbCompanyData
CompanyID CompanyName
1 ABB
2 DELL 


table2-tbQuestion 
Code QnDescription
1 How you rate our services
2 How is our products
3 How do you think about us 


tbale 3-tbQnRating
CompanyID QnCode Rating
1 1 3
2 1 2
2 2 3
3 1 1


我的存储过程


My Stored Procedure

create proc [dbo].[sp_InsertAllValues]
@CompanyName varchar(100),
@Rating int
as
declare @topId int
set @topId=(select MAX(ID+1) from tbCompanyData)
declare @QnCode int
set @QnCode=(select code from tbQnMst where Code=1)(NOTE:I'm inserting the QnCode values statically here so in my table according to the QnCOde 1 companyName,QnCode and rating are getting inserted so please tell me how do i insert QnCode dynamically)
begin
begin try
begin transaction
insert into tbCompanyData values(@CompanyName)
insert into tbQnRating values(@topId,@QnCode,@Rating)
commit transaction
end try
begin catch
rollback transaction
end CATCH
end 



预先感谢



Thanx in advance

推荐答案


这篇关于插入外键值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 15:42