|
|
Question : How do I lookup and insert the foreign key value into a table when passing a variable value from different column in the related table?
|
|
|
|
Table A contains fields: [Name] as varchar, [AlphaNumber] as numeric, and [SiteCode] as varchar. Data in this table is static. AlphaNumber is the Primary key in Table A. I am using a stored procedure to populate the combo box on a form and display the [Name] field to the operator from Table A. All the data entered on the form will be inserted into table B.
Table B contains the field [ValidOPId] as numeric and is the foreign key related to the [AlphaNumber] field in Table A. So far, I have written this stored procedure and successfully inserted data to Table B (without the form).
ALTER Procedure [dbo].[spLocal_InsertValidationData] @TagName Varchar(50), @ValidTime datetime, @Value real, @Reason1 varchar(50), @Reason2 varchar(50), @Reason3 varchar(50), @Reason4 varchar(50), @Comments varchar(50), @ValidOPId numeric, @ValidAPMId numeric, @ValidPMId numeric, @InterfaceFlag bit, @OOSpecFlag bit
AS INSERT INTO ValidationDetail
VALUES (@TagName,@ValidTime,@Value,@Reason1,@Reason2,@Reason3,@Reason4,@Comments,@ValidOPId,@ValidAPMId,@ValidPMId,@InterfaceFlag,@OOSpecFlag)
Return
Need to modify this procedure to write the [Name] from the combo box on the form back out to table B [ValidOPId]. Need to convert it back to the related numeric [AlphaNumber] that corresponds to the name selected in the combo box on the form first.
Can you help?
|
|
|
|
Answer : How do I lookup and insert the foreign key value into a table when passing a variable value from different column in the related table?
|
|
There are two ways: 1) [preferable] - in html form combo box - set the value of option tag as the AlphaNumber from TableA. So, when ever form submits - it will send the value of selected option from combo box - so, it will pass the AlphaNumber instead of displayed text. This way, your stored procedure will remain as it is.
2) Change the SP as:
ALTER Procedure [dbo].[spLocal_InsertValidationData] @TagName Varchar(50), @ValidTime datetime, @Value real, @Reason1 varchar(50), @Reason2 varchar(50), @Reason3 varchar(50), @Reason4 varchar(50), @Comments varchar(50), @Name varchar(50), @ValidAPMId numeric, @ValidPMId numeric, @InterfaceFlag bit, @OOSpecFlag bit
AS Declare @ValidOPId int Select @ValidOPId = AlphaNumber From [TableA] Where [Name] = @Name
INSERT INTO ValidationDetail VALUES (@TagName,@ValidTime,@Value,@Reason1,@Reason2,@Reason3,@Reason4,@Comments,@ValidOPId,@ValidAPMId,@ValidPMId,@InterfaceFlag,@OOSpecFlag)
|
|
|
|