我正在创建一个存储过程,用于填充两个表tblAirport
和tblCountry
。 tblCountry
从tblAirport
获取其国家/地区名称,但我只想在`tblCountry中显示一个国家/地区名称的实例。到目前为止,对于我的存储过程,我有这个
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)
我尝试使用IF EXISTS (SELECT * FROM tblCountry WHERE strCountry = @strCountry)
SET @strCountry = @strCountry + 'x'
只显示任何重复的国家/地区,但我不知道如何消除表格中的重复项。我是SQL的新手,我只学习了IF EXISTS
函数。任何建议都会很棒。谢谢!
答案 0 :(得分:1)
这是处理多行IF ELSE(https://technet.microsoft.com/en-us/library/ms182717(v=sql.110).aspx)
的方法IF NOT EXISTS (SELECT * FROM tblCountry WHERE strCountry = @strCountry)
BEGIN
INSERT INTO tblCountry (strCountry) VALUES (@strCountry)
END;
总的来说,我会关注一个程序,它使用数据来驱动查找列表中的可能值,特别是那些可能预先预先定义的国家。你讨厌他们输入自由形式的副本,这些副本实际上是同一个国家,拼写略有不同。