I'm trying to create a stored procedure the inserts values into two tables tblAirport
and tblCountry
. tblAirport
has a FK that references tblCountry
and I want to insert the values to tblCountry
using the values from the FK in tblAirport
. I'm new to SQL and I think I have to use the SCOPE_IDENTITY()
function to achieve this but I'm not sure how to implement it. These are my tables;
tblAirport
PK INT IDENTITY(1,1),
geoLocation GEOGRAPHY,
ICAOCode VARCHAR(4),
IATACode VARCHAR(3),
strName VARCHAR(MAX),
strCity VARCHAR(MAX),
strCountry VARCHAR(MAX),
degLat DECIMAL(7,2),
minLat DECIMAL(7,2),
secLat DECIMAL(7,2),
Equator VARCHAR(1),
degLong DECIMAL(7,2),
minLong DECIMAL(7,2),
secLong DECIMAL(7,2),
Meridian VARCHAR(1),
strElevation VARCHAR(5),
FKCountry INT NULL
tblCountry
PK INT IDENTITY(1,1),
strCountry VARCHAR(MAX)
This is what I have for my stored procedure
CREATE PROCEDURE [dbo].spInsertAirport
@ICAOCode VARCHAR(4),
@IATACode VARCHAR(3),
@strName VARCHAR(MAX),
@strCity VARCHAR(MAX),
@strCountry VARCHAR(MAX),
@degLat DECIMAL(7,2),
@minLat DECIMAL(7,2),
@secLat DECIMAL(7,2),
@Equator VARCHAR(1),
@degLong DECIMAL(7,2),
@minLong DECIMAL(7,2),
@secLong DECIMAL(7,2),
@Meridian VARCHAR(1),
@strElevation VARCHAR(5),
@FKCountry INT
AS
BEGIN TRANSACTION
BEGIN TRY
SET NOCOUNT ON
SET ANSI_WARNINGS OFF
INSERT INTO tblAirport (ICAOCode, IATACode, strName, strCity, strCountry,
degLat, minLat, secLat, Equator,
degLong, minLong, secLong, Meridian, strElevation)
VALUES (@IATACode, @strName, @strCity, @strCountry,
@degLat, @minLat, @secLat, @Equator,
@degLong, @minLong, @secLong, @Meridian, @strElevation)
I don't know how to insert the countries into the tblCountry
table; any help is awesome