IF COL_LENGTH('Personel', 'ZipCode') IS NULL
BEGIN
	ALTER TABLE Personel ADD ZipCode varchar(10) NULL	
END
IF COL_LENGTH('Personel', 'City') IS NULL
BEGIN
	ALTER TABLE Personel ADD City varchar(50) NULL	
END
IF COL_LENGTH('Personel', 'CountryNumber') IS NULL
BEGIN
	ALTER TABLE Personel ADD CountryNumber int NULL	
END
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_Personel_Countries]') AND parent_object_id = OBJECT_ID(N'[dbo].[Personel]'))
ALTER TABLE [dbo].[Personel]  WITH CHECK ADD  CONSTRAINT [FK_Personel_Countries] FOREIGN KEY([CountryNumber])
REFERENCES [dbo].[Countries] ([CountryNumber])
ON UPDATE CASCADE
ON DELETE SET NULL
GO
IF COL_LENGTH('Personel', 'ZipCodeNumber') IS NOT NULL
BEGIN
  EXEC('
  UPDATE Personel
  SET ZipCode=Z.ZipCode, City=Z.CityName, CountryNumber=Z.CountryNumber
  FROM Personel P
  INNER JOIN ZIPCODES Z ON Z.ZipCodeNumber=P.ZipCodeNumber

  ALTER TABLE Personel DROP COLUMN ZipCodeNumber
  ')
END
GO
IF COL_LENGTH('PersonelHidden', 'ZipCode') IS NULL
BEGIN
	ALTER TABLE PersonelHidden ADD ZipCode varchar(10) NULL	
END
IF COL_LENGTH('PersonelHidden', 'City') IS NULL
BEGIN
	ALTER TABLE PersonelHidden ADD City varchar(50) NULL	
END
IF COL_LENGTH('PersonelHidden', 'CountryNumber') IS NULL
BEGIN
	ALTER TABLE PersonelHidden ADD CountryNumber int NULL	
END
GO
IF COL_LENGTH('PersonelHidden', 'ZipCodeNumber') IS NOT NULL
BEGIN
  EXEC('
  UPDATE PersonelHidden
  SET ZipCode=Z.ZipCode, City=Z.CityName, CountryNumber=Z.CountryNumber
  FROM PersonelHidden P
  INNER JOIN ZIPCODES Z ON Z.ZipCodeNumber=P.ZipCodeNumber

  ALTER TABLE PersonelHidden DROP COLUMN ZipCodeNumber
  ')
END
GO
