



CREATE PROCEDURE DBO.InsertZipCode @ZipCode VarChar(10), @CityName VarChar(50), @CountryCode VarChar(4), @OverWriteZipCode Bit, @ZipCodeExists Bit OUTPUT  AS
SET NOCOUNT ON

-- Sidst rettet af Klaus Stenå d. 12/2-2001

Declare @CountryNumber int

SET @CountryNumber = 0
Select @CountryNumber=CountryNumber From COUNTRIES WITH (Nolock) where Upper(CountryCode)=Upper(@CountryCode)

if (@CountryNumber = 0) 
begin
  Insert Into COUNTRIES (CountryCode, CountryName, MarkAsDeleted) Values (@CountryCode, 'Unknown',0)
  Select @CountryNumber=CountryNumber From COUNTRIES WITH (Nolock) where Upper(CountryCode)=Upper(@CountryCode)
end

Select  @ZipCodeExists=Count(*) From ZipCodes WITH (NoLock) Where ZipCode=@ZipCode AND CountryNumber=@CountryNumber

if  @ZipCodeExists = 0
begin
  Insert Into ZipCodes (ZipCode, CityName, CountryNumber) Values (@ZipCode, @CityName, @CountryNumber)
end 
else
begin
  if (@OverWriteZipCode = 1)
  begin
     UPDATE ZipCodes
       SET CityName = @CityName
     Where ZipCode=@ZipCode AND CountryNumber=@CountryNumber
  end
end

