mardi 26 décembre 2017

I want my stored procedure to only populate one instance of a name

I'm creating a stored procedure that populates two tables tblAirport and tblCountry. tblCountry gets its country names from tblAirport but I only want one instance of the country name to show up in `tblCountry. So far for my stored procedure I have this

`DECLARE @PK INT = (SELECT PK FROM tblAirport WHERE strName = @strName)

IF @PK IS NULL INSERT INTO tblAirport (ICAOCode,IATACode,strName,strCity,strCountry,degLat,minLat,secLat,Equator,degLong,minLong,secLong,Meridian,strElevation) VALUES (@ICAOCode,@IATACode,@strName,@strCity,@strCountry,@degLat,@minLat,@secLat,@Equator,@degLong,@minLong,@secLong,@Meridian,@strElevation) SET @PK = (SELECT PK FROM tblAirport WHERE strName = @strName);

IF EXISTS (SELECT * FROM tblCountry WHERE strCountry = @strCountry) SET @strCountry = @strCountry + 'x'

INSERT INTO tblCountry (strCountry) VALUES (@strCountry)`

I tried using IF EXISTS (SELECT * FROM tblCountry WHERE strCountry = @strCountry) SET @strCountry = @strCountry + 'x' just to show any duplicate countries but I don't know how to eliminate the duplicates from my table. I'm new to SQL and I've only learned the IF EXISTS function. Any suggestions would be great. Thank you!

Aucun commentaire:

Enregistrer un commentaire