DECLARE @GroupName varchar(30)
DECLARE @SortNumber int
DECLARE @GroupCode varchar(3)

DECLARE @CountryName varchar(30)
DECLARE @CountryCode varchar(3)
DECLARE @CurrencyCode varchar(3)
DECLARE @CurrencyName varchar(30)

DECLARE @CountryGroupNumber int

DECLARE @OtherEurope int
DECLARE @OtherAfrica int
DECLARE @OtherAmerica int
DECLARE @OtherAsia int
DECLARE @OtherOceanic int
DECLARE @OtherWorld int
DECLARE @Denmark int
DECLARE @Finland int

CREATE TABLE #TempGroups (CountryName     VarChar(30) NOT NULL,
                          GroupSortNumber Int         NOT NULL,
                          CountryCode     varchar(3)  NOT NULL)

DECLARE @TheCountryCode varchar(3)

SELECT @TheCountryCode = CountryCode FROM Countries WITH (NOLOCK) 
WHERE CountryNumber = (SELECT KeyValue FROM HotelSetup WITH (NOLOCK) WHERE SearchKey = 'COUNTRYCODE')

PRINT 'CountryCode: '+@TheCountryCode

IF @TheCountryCode = 'GL' 
BEGIN
  -- Update ZipCodes ZipCodes Between 3900-3999 skal tilknyttes Grønland i stedet for Danmark
  DECLARE @CountryNumber INT
  SELECT @CountryNumber=CountryNumber FROM Countries WITH (NOLOCK) WHERE CountryCode = 'GL'
	
  UPDATE ZipCodes SET CountryNumber = @CountryNumber
  WHERE ZipCode > '3900' 
  AND ZipCode < '3999'
  AND CountryNumber = 1

  INSERT INTO #TempGroups VALUES ('Bosiddende i Grønland',1,'GL')
  INSERT INTO #TempGroups VALUES ('Danmark',2,'DK')
  INSERT INTO #TempGroups VALUES ('Sverige',3,'S')
  INSERT INTO #TempGroups VALUES ('Norge',4,'N')
  INSERT INTO #TempGroups VALUES ('Island',5,'IS')
  INSERT INTO #TempGroups VALUES ('Tyskland',6,'D')
  INSERT INTO #TempGroups VALUES ('England',7,'GB')
  INSERT INTO #TempGroups VALUES ('Holland',8,'NL')
  INSERT INTO #TempGroups VALUES ('Frankrig',9,'F')
  INSERT INTO #TempGroups VALUES ('Italien',10,'I')
  INSERT INTO #TempGroups VALUES ('Øvrige europa',11,'OE')
  INSERT INTO #TempGroups VALUES ('USA',12,'USA')
  INSERT INTO #TempGroups VALUES ('Canada',13,'CA')
  INSERT INTO #TempGroups VALUES ('Japan',14,'JP')
  INSERT INTO #TempGroups VALUES ('Andre lande', 15, 'øvr')

  DECLARE db_curGL CURSOR FOR
    SELECT * FROM #TempGroups WITH (nolock)
  OPEN db_curGL 
	   
  FETCH NEXT FROM db_curGL INTO @GroupName, @SortNumber, @GroupCode
	    
  WHILE @@fetch_status = 0 
  BEGIN
    IF @SortNumber = 1   -- Bosiddende i Grønland
    BEGIN
      UPDATE CountryGroups SET GroupName = @GroupName, GroupSortNumber = @SortNumber, GroupCode = @GroupCode  WHERE GroupName LIKE 'Gr%nland' 
      PRINT 'Rename: ' + @GroupName
      FETCH NEXT FROM db_curGL INTO @GroupName, @SortNumber, @GroupCode
      CONTINUE
    END
    IF @SortNumber = 7   -- England 
    BEGIN
      UPDATE CountryGroups SET GroupName = @GroupName, GroupSortNumber = @SortNumber, GroupCode = @GroupCode  WHERE GroupName LIKE '%/ Nord irland%' OR GroupName LIKE '%/ Nordirland%'
      PRINT 'Rename: ' + @GroupName
      FETCH NEXT FROM db_curGL INTO @GroupName, @SortNumber, @GroupCode
      CONTINUE
    END
    IF @SortNumber = 11   -- Øvrige europa 
    BEGIN
      UPDATE CountryGroups SET GroupName = @GroupName, GroupSortNumber = @SortNumber, GroupCode = @GroupCode  WHERE GroupName LIKE '%Europa i %vrigt%'
      PRINT 'Rename: ' + @GroupName
      FETCH NEXT FROM db_curGL INTO @GroupName, @SortNumber, @GroupCode
      CONTINUE
    END
    IF @SortNumber = 15   -- Andre lande 
    BEGIN
      UPDATE CountryGroups SET GroupName = @GroupName, GroupSortNumber = @SortNumber, GroupCode = @GroupCode  WHERE GroupName LIKE '%vrige lande%'
      PRINT 'Rename: ' + @GroupName
      FETCH NEXT FROM db_curGL INTO @GroupName, @SortNumber, @GroupCode
      CONTINUE
    END

    IF NOT EXISTS (SELECT GroupName FROM CountryGroups WITH (NOLOCK) WHERE GroupName = @GroupName) 
    BEGIN
      PRINT 'Insert: ' + @GroupName
      INSERT INTO CountryGroups (GroupName, GroupSortNumber, GroupCode) VALUES (@GroupName,@SortNumber,@GroupCode)
    END
    ELSE
    BEGIN
      UPDATE CountryGroups SET GroupSortNumber = @SortNumber, GroupCode = @GroupCode WHERE GroupName = @GroupName
      PRINT 'Update: ' + @GroupName
    END
  
    FETCH NEXT FROM db_curGL INTO @GroupName, @SortNumber, @GroupCode
  END
END -- End of Grønland Groups

IF (@TheCountryCode = 'S' OR @TheCountryCode = 'SE')  
BEGIN
  UPDATE Countries SET CountryGroupNumber = 80 WHERE CountryGroupNumber > 80
  DELETE CountryGroups WHERE GroupNumber > 80

  INSERT INTO #TempGroups VALUES ('Sverige', 1, 'S')
  INSERT INTO #TempGroups VALUES ('Danmark', 2, 'DK')
  INSERT INTO #TempGroups VALUES ('Norge', 3, 'N')
  INSERT INTO #TempGroups VALUES ('Finland',4, 'FIN')
  INSERT INTO #TempGroups VALUES ('Island',5, 'IS')
  INSERT INTO #TempGroups VALUES ('Tyskland',	6, 'D')
  INSERT INTO #TempGroups VALUES ('Storbritannien',	7, 'GB')
  INSERT INTO #TempGroups VALUES ('Irland',	8, 'IRL')
  INSERT INTO #TempGroups VALUES ('Nederländerna', 9, 'NL')
  INSERT INTO #TempGroups VALUES ('Belgien',10, 'B')
  INSERT INTO #TempGroups VALUES ('Luxemburg',11, 'L')
  INSERT INTO #TempGroups VALUES ('Frankrike',12, 'FR')
  INSERT INTO #TempGroups VALUES ('Schweiz',13, 'CH')
  INSERT INTO #TempGroups VALUES ('Österrike',14, 'A')
  INSERT INTO #TempGroups VALUES ('Italien',15, 'I')
  INSERT INTO #TempGroups VALUES ('Spanien',16, 'E')
  INSERT INTO #TempGroups VALUES ('Portugal', 17, 'P')
  INSERT INTO #TempGroups VALUES ('Grekland', 18, 'GR')
  INSERT INTO #TempGroups VALUES ('Turkiet', 19, 'TUR')
  INSERT INTO #TempGroups VALUES ('Ryssland', 20, 'RU')
  INSERT INTO #TempGroups VALUES ('Estland', 21, 'EE')
  INSERT INTO #TempGroups VALUES ('Lettland',	22, 'LV')
  INSERT INTO #TempGroups VALUES ('Litauen', 23, 'LT')
  INSERT INTO #TempGroups VALUES ('Polen', 24, 'PL')
  INSERT INTO #TempGroups VALUES ('Tjeckien',	25, 'CZ')
  INSERT INTO #TempGroups VALUES ('Slovakien', 26, 'SK')
  INSERT INTO #TempGroups VALUES ('Ungern',	27, 'HU')
  INSERT INTO #TempGroups VALUES ('Övriga Europa', 28, 'ÖEu')
  INSERT INTO #TempGroups VALUES ('USA',29, 'USA')
  INSERT INTO #TempGroups VALUES ('Canada',	30, 'CA')
  INSERT INTO #TempGroups VALUES ('Central och Sydamerika',	31, 'ÖAm')
  INSERT INTO #TempGroups VALUES ('Japan', 32, 'JP')
  INSERT INTO #TempGroups VALUES ('Sydkorea',	33, 'KR')
  INSERT INTO #TempGroups VALUES ('Övriga Asien',	34, 'ÖAs')
  INSERT INTO #TempGroups VALUES ('Australien',	35, 'AU')
  INSERT INTO #TempGroups VALUES ('Nya Zeeland', 36, 'NZ')
  INSERT INTO #TempGroups VALUES ('Övriga Världen',	37, 'Övr')
  INSERT INTO #TempGroups VALUES ('Ej angivet',	38, 'Eja')

	DECLARE db_cur CURSOR FOR
	  SELECT * FROM #TempGroups WITH (nolock)
	OPEN db_cur 
	   
	FETCH NEXT FROM db_cur INTO @GroupName, @SortNumber, @GroupCode
	    
	WHILE @@fetch_status = 0 
	BEGIN
	  IF @SortNumber = 7   -- Storbritannien 
	  BEGIN
	    IF EXISTS (SELECT GroupName FROM CountryGroups WITH (NOLOCK) WHERE GroupName = 'Storbritannen')
	    BEGIN
              UPDATE CountryGroups SET GroupName = @GroupName, GroupSortNumber = @SortNumber, GroupCode = @GroupCode  WHERE GroupName = 'Storbritannen'
	    END
	    ELSE
	    BEGIN
	    -- Findes slet ikke i forvejen
	      SELECT @CountryGroupNumber = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupCode = @CountryCode
	      INSERT INTO Countries (CountryName, CountryCode, MarkAsDeleted, CountryGroupNumber, CurrencyCode, CurrencyName, SortNumber) VALUES (@CountryName,@CountryCode,0,@CountryGroupNumber,@CurrencyCode,@CurrencyName,999)
	    END
	    FETCH NEXT FROM db_cur INTO @GroupName, @SortNumber, @GroupCode
	    CONTINUE
	  END

	  IF @SortNumber = 26   -- Slovenien 
	  BEGIN
	    UPDATE CountryGroups SET GroupName = @GroupName, GroupSortNumber = @SortNumber, GroupCode = @GroupCode  WHERE GroupName = 'Slovenien'
	    PRINT 'Rename: ' + @GroupName
	    FETCH NEXT FROM db_cur INTO @GroupName, @SortNumber, @GroupCode
	    CONTINUE
	  END
    
	  IF NOT EXISTS (SELECT GroupName FROM CountryGroups WITH (NOLOCK) WHERE GroupName = @GroupName) 
	  BEGIN
	    PRINT 'Insert: ' + @GroupName
	    INSERT INTO CountryGroups (GroupName, GroupSortNumber, GroupCode) VALUES (@GroupName,@SortNumber,@GroupCode)
	  END
	  ELSE
	  BEGIN
	    UPDATE CountryGroups SET GroupSortNumber = @SortNumber, GroupCode = @GroupCode WHERE GroupName = @GroupName
	    PRINT 'Update: ' + @GroupName
	  END
	  
	  FETCH NEXT FROM db_cur INTO @GroupName, @SortNumber, @GroupCode
	END
END  -- end Swenden Countrygroups

IF @TheCountryCode = 'N'
BEGIN
  INSERT INTO #TempGroups VALUES ('Norge',1,'N')
  INSERT INTO #TempGroups VALUES ('Sverige',2,'S')
  INSERT INTO #TempGroups VALUES ('Danmark',3,'DK')
  INSERT INTO #TempGroups VALUES ('Finland',4,'FIN')
  INSERT INTO #TempGroups VALUES ('Island',5,'IS')
  INSERT INTO #TempGroups VALUES ('Storbritannia',6,'GB')
  INSERT INTO #TempGroups VALUES ('Nederland',7,'NL')
  INSERT INTO #TempGroups VALUES ('Tyskland',8,'D')
  INSERT INTO #TempGroups VALUES ('Frankrike',9,'F')
  INSERT INTO #TempGroups VALUES ('Spania',10,'E')
  INSERT INTO #TempGroups VALUES ('Sveits',11,'CH')
  INSERT INTO #TempGroups VALUES ('Liechtenstein',12,'LI')
  INSERT INTO #TempGroups VALUES ('Italia',13,'I')
  INSERT INTO #TempGroups VALUES ('Østerrike',14,'A')
  INSERT INTO #TempGroups VALUES ('Belgia',15,'B')
  INSERT INTO #TempGroups VALUES ('Portugal',16,'P')
  INSERT INTO #TempGroups VALUES ('Hellas',17,'GR')
  INSERT INTO #TempGroups VALUES ('Irland',18,'IRL')
  INSERT INTO #TempGroups VALUES ('Luxembourg',19,'L')
  INSERT INTO #TempGroups VALUES ('Tyrkia',20,'TR')
  INSERT INTO #TempGroups VALUES ('Polen',21,'PL')
  INSERT INTO #TempGroups VALUES ('Tsjekkia',22,'CZ')
  INSERT INTO #TempGroups VALUES ('Slovakia',23,'SK')
  INSERT INTO #TempGroups VALUES ('Ungarn',24,'H')
  INSERT INTO #TempGroups VALUES ('Estland',25,'EE')
  INSERT INTO #TempGroups VALUES ('Latvia',26,'LV')
  INSERT INTO #TempGroups VALUES ('Litauen',27,'LT')
  INSERT INTO #TempGroups VALUES ('Kypros',28,'CY')
  INSERT INTO #TempGroups VALUES ('Malta',29,'MT')
  INSERT INTO #TempGroups VALUES ('Slovenia',30,'SI')
  INSERT INTO #TempGroups VALUES ('Ukraina',31,'UA')
  INSERT INTO #TempGroups VALUES ('Russland',32,'RU')
  INSERT INTO #TempGroups VALUES ('Andre europeiske land',33,'OE')
  INSERT INTO #TempGroups VALUES ('USA',34,'USA')
  INSERT INTO #TempGroups VALUES ('Canada',35,'CA')
  INSERT INTO #TempGroups VALUES ('Mexico',36,'MX')
  INSERT INTO #TempGroups VALUES ('Brasil',37,'BR')
  INSERT INTO #TempGroups VALUES ('Latin-Amerika ellers',38,'OA')
  INSERT INTO #TempGroups VALUES ('Australia',39,'AU')
  INSERT INTO #TempGroups VALUES ('Oseania ellers',40,'OOL')
  INSERT INTO #TempGroups VALUES ('Japan',41,'JP')
  INSERT INTO #TempGroups VALUES ('Kina',42,'CN')
  INSERT INTO #TempGroups VALUES ('Sør-Korea',43,'KR')
  INSERT INTO #TempGroups VALUES ('Andre asiatiske land',44,'OAL')
  INSERT INTO #TempGroups VALUES ('Sør-Afrika',45,'ZA')
  INSERT INTO #TempGroups VALUES ('Andre afrikanske land',46,'OA1')

	DECLARE db_cur CURSOR FOR
	  SELECT * FROM #TempGroups WITH (nolock)
	OPEN db_cur 
	   
	FETCH NEXT FROM db_cur INTO @GroupName, @SortNumber, @GroupCode
	    
	WHILE @@fetch_status = 0 
	BEGIN
	  -- Der er nogle lande hvor GroupName skal renames
	  IF @SortNumber = 6   -- Storbritanien 
	    UPDATE CountryGroups SET GroupName = @GroupName, GroupSortNumber = @SortNumber, GroupCode = @GroupCode  WHERE GroupName = 'Storbritannia' OR GroupName LIKE '%/ Nord irland%' OR GroupName LIKE '%/ Nordirland%' OR GroupName LIKE '%Storbritanien%'
	  IF @SortNumber = 7   -- Holland 
	    UPDATE CountryGroups SET GroupName = @GroupName, GroupSortNumber = @SortNumber, GroupCode = @GroupCode  WHERE GroupName = 'Holland' OR GroupName = 'Nederland'
	  IF @SortNumber = 9   -- Frankrig 
	    UPDATE CountryGroups SET GroupName = @GroupName, GroupSortNumber = @SortNumber, GroupCode = @GroupCode  WHERE GroupName = 'Frankrig' OR GroupName = 'Frankrike'
	  IF @SortNumber = 10   -- Spanien 
	    UPDATE CountryGroups SET GroupName = @GroupName, GroupSortNumber = @SortNumber, GroupCode = @GroupCode  WHERE GroupName = 'Spanien' OR GroupName = 'Spania'
	  IF @SortNumber = 11   -- Schweiz 
	    UPDATE CountryGroups SET GroupName = @GroupName, GroupSortNumber = @SortNumber, GroupCode = @GroupCode  WHERE GroupName = 'Schweiz' OR GroupName = 'Sveits'
	  IF @SortNumber = 13   -- Italien
	    UPDATE CountryGroups SET GroupName = @GroupName, GroupSortNumber = @SortNumber, GroupCode = @GroupCode  WHERE GroupName = 'Italien' OR GroupName = 'Italia'
	  IF @SortNumber = 15   -- Belgien 
	    UPDATE CountryGroups SET GroupName = @GroupName, GroupSortNumber = @SortNumber, GroupCode = @GroupCode  WHERE GroupName = 'Belgien' OR GroupName = 'Belgia'
	  IF @SortNumber = 17   -- Grækenland 
	    UPDATE CountryGroups SET GroupName = @GroupName, GroupSortNumber = @SortNumber, GroupCode = @GroupCode  WHERE GroupName = 'Grækenland' OR GroupName = 'Grµkenland'
	  IF @SortNumber = 19   -- Luxemborg 
	    UPDATE CountryGroups SET GroupName = @GroupName, GroupSortNumber = @SortNumber, GroupCode = @GroupCode  WHERE GroupName = 'Luxemborg' OR GroupName = 'Luxembourg'
	  IF @SortNumber = 20   -- Tyrkiet 
	    UPDATE CountryGroups SET GroupName = @GroupName, GroupSortNumber = @SortNumber, GroupCode = @GroupCode  WHERE GroupName = 'Tyrkiet' OR GroupName = 'Tyrkia'
	  IF @SortNumber = 22   -- Tjekkiet
	    UPDATE CountryGroups SET GroupName = @GroupName, GroupSortNumber = @SortNumber, GroupCode = @GroupCode  WHERE GroupName = 'Tjekkiet' OR GroupName = 'Tsjekkia'
	  IF @SortNumber = 23   -- Slovakiet 
	    UPDATE CountryGroups SET GroupName = @GroupName, GroupSortNumber = @SortNumber, GroupCode = @GroupCode  WHERE GroupName = 'Slovakiet' OR GroupName = 'Slovakia'
	  IF @SortNumber = 26   -- Letland 
	    UPDATE CountryGroups SET GroupName = @GroupName, GroupSortNumber = @SortNumber, GroupCode = @GroupCode  WHERE GroupName = 'Letland' OR GroupName = 'Latvia'
	  IF @SortNumber = 28   -- Cypern 
	    UPDATE CountryGroups SET GroupName = @GroupName, GroupSortNumber = @SortNumber, GroupCode = @GroupCode  WHERE GroupName = 'Cypern' OR GroupName = 'Kypros'
	  IF @SortNumber = 30   -- Slovenien 
	    UPDATE CountryGroups SET GroupName = @GroupName, GroupSortNumber = @SortNumber, GroupCode = @GroupCode  WHERE GroupName = 'Slovenien' OR GroupName = 'Slovenia'
	  IF @SortNumber = 31   -- Ukraine
	    UPDATE CountryGroups SET GroupName = @GroupName, GroupSortNumber = @SortNumber, GroupCode = @GroupCode  WHERE GroupName = 'Ukraine' OR GroupName = 'Ukraina'
	  IF @SortNumber = 32   -- Rusland 
	    UPDATE CountryGroups SET GroupName = @GroupName, GroupSortNumber = @SortNumber, GroupCode = @GroupCode  WHERE GroupName = 'Rusland' OR GroupName = 'Russland'
	  IF @SortNumber = 33   -- Andre europæiske lande 
	    UPDATE CountryGroups SET GroupName = @GroupName, GroupSortNumber = @SortNumber, GroupCode = @GroupCode WHERE GroupName LIKE '%Europa i %vrigt%' OR GroupCode = 'EO' OR GroupName LIKE '%Andre europæiske lande%' OR GroupName = 'Europa ellers'
	  IF @SortNumber = 37   -- Brasilien 
	    UPDATE CountryGroups SET GroupName = @GroupName, GroupSortNumber = @SortNumber, GroupCode = @GroupCode WHERE GroupName = 'Brasilien' OR GroupName = 'Brasil'
	  IF @SortNumber = 38   -- Andre amerikanske lande 
	    UPDATE CountryGroups SET GroupName = @GroupName, GroupSortNumber = @SortNumber, GroupCode = @GroupCode WHERE GroupName LIKE '%Amerika i %vrigt' OR GroupCode = 'AO' OR GroupName LIKE '%Andre afrikanske lande%' OR GroupName = 'Amerika ellers'
	  IF @SortNumber = 39   -- Australien 
	    UPDATE CountryGroups SET GroupName = @GroupName, GroupSortNumber = @SortNumber, GroupCode = @GroupCode WHERE GroupName = 'Australien' OR GroupName = 'Australia'
	  IF @SortNumber = 40   -- Andre oceaniske lande 
	    UPDATE CountryGroups SET GroupName = @GroupName, GroupSortNumber = @SortNumber, GroupCode = @GroupCode WHERE GroupName LIKE '%Andre oceaniske lande%' 
	  IF @SortNumber = 42   -- Folkerepublikken Kina
	    UPDATE CountryGroups SET GroupName = @GroupName, GroupSortNumber = @SortNumber, GroupCode = @GroupCode WHERE GroupName LIKE '%Folkerepublikken Kina%' OR GroupName = 'Kina'
	  IF @SortNumber = 43   -- Sydkorea
	    UPDATE CountryGroups SET GroupName = @GroupName, GroupSortNumber = @SortNumber, GroupCode = @GroupCode  WHERE GroupName = 'Sydkorea' OR GroupName = 'Sør-Korea'
	  IF @SortNumber = 44   -- Andre asiatiske lande 
	    UPDATE CountryGroups SET GroupName = @GroupName, GroupSortNumber = @SortNumber, GroupCode = @GroupCode WHERE GroupName LIKE '%Syd%stasian i %vrigt%' OR GroupName LIKE '%Syd%stasien i %vrigt%' OR GroupCode = 'SO' OR GroupName = 'Andre land'
	  IF @SortNumber = 45   -- Sydafrika 
	    UPDATE CountryGroups SET GroupName = @GroupName, GroupSortNumber = @SortNumber, GroupCode = @GroupCode  WHERE GroupName = 'Sydafrika' Or GroupName = 'Sør-Afrika'
	  IF @SortNumber = 46   -- Andre afrikanske lande 
	    UPDATE CountryGroups SET GroupName = @GroupName, GroupSortNumber = @SortNumber, GroupCode = @GroupCode WHERE GroupName LIKE '%Andre amerikanske lande%' OR GroupName ='Afrika' OR GroupName = 'Øvrige lande'
    IF @@RowCount > 0
    BEGIN
	    PRINT 'Rename: ' + @GroupName
	    FETCH NEXT FROM db_cur INTO @GroupName, @SortNumber, @GroupCode
	    CONTINUE
    END
	  IF NOT EXISTS (SELECT GroupName FROM CountryGroups WITH (NOLOCK) WHERE GroupName = @GroupName) 
	  BEGIN
	    PRINT 'Insert: ' + @GroupName
	    INSERT INTO CountryGroups (GroupName, GroupSortNumber, GroupCode) VALUES (@GroupName,@SortNumber,@GroupCode)
	  END
	  ELSE
	  BEGIN
	    UPDATE CountryGroups SET GroupSortNumber = @SortNumber, GroupCode = @GroupCode WHERE GroupName = @GroupName
	    PRINT 'Update: ' + @GroupName
	  END
	  
    FETCH NEXT FROM db_cur INTO @GroupName, @SortNumber, @GroupCode
  END

  -- Some countryGroups should be deleted
  --Færøerne
  UPDATE Countries SET CountryGroupNumber = (SELECT TOP 1 GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupCode = 'DK') WHERE CountryGroupNumber = (SELECT TOP 1 GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupCode = 'FO')
  DELETE CountryGroups WHERE GroupCode = 'FO'
  --Grønland
  UPDATE Countries SET CountryGroupNumber = (SELECT TOP 1 GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupCode = 'DK') WHERE CountryGroupNumber = (SELECT TOP 1 GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupCode = 'GL')
  DELETE CountryGroups WHERE GroupCode = 'GL'


END --End norwegian CountryGroups

IF @TheCountryCode = 'DK'
BEGIN
  INSERT INTO #TempGroups VALUES ('Danmark',1,'DK')
  INSERT INTO #TempGroups VALUES ('Færøerne',2,'FO')
  INSERT INTO #TempGroups VALUES ('Grønland',3,'GL')
  INSERT INTO #TempGroups VALUES ('Belgien',4,'B')
  INSERT INTO #TempGroups VALUES ('Cypern',5,'CY')
  INSERT INTO #TempGroups VALUES ('Estland',6,'EE')
  INSERT INTO #TempGroups VALUES ('Finland',7,'FIN')
  INSERT INTO #TempGroups VALUES ('Frankrig',8,'F')
  INSERT INTO #TempGroups VALUES ('Grækenland',9,'GR')
  INSERT INTO #TempGroups VALUES ('Holland',10,'NL')
  INSERT INTO #TempGroups VALUES ('Irland',11,'IRL')
  INSERT INTO #TempGroups VALUES ('Island',12,'IS')
  INSERT INTO #TempGroups VALUES ('Italien',13,'I')
  INSERT INTO #TempGroups VALUES ('Letland',14,'LV')
  INSERT INTO #TempGroups VALUES ('Litauen',15,'LT')
  INSERT INTO #TempGroups VALUES ('Luxemborg',16,'L')
  INSERT INTO #TempGroups VALUES ('Malta',17,'MT')
  INSERT INTO #TempGroups VALUES ('Norge',18,'N')
  INSERT INTO #TempGroups VALUES ('Polen',19,'PL')
  INSERT INTO #TempGroups VALUES ('Portugal',20,'P')
  INSERT INTO #TempGroups VALUES ('Rusland',21,'RU')
  INSERT INTO #TempGroups VALUES ('Schweiz',22,'CH')
  INSERT INTO #TempGroups VALUES ('Slovakiet',23,'SK')
  INSERT INTO #TempGroups VALUES ('Slovenien',24,'SI')
  INSERT INTO #TempGroups VALUES ('Spanien',25,'E')
  INSERT INTO #TempGroups VALUES ('Storbritanien',26,'GB')
  INSERT INTO #TempGroups VALUES ('Sverige',27,'S')
  INSERT INTO #TempGroups VALUES ('Tjekkiet',28,'CZ')
  INSERT INTO #TempGroups VALUES ('Tyrkiet',29,'TR')
  INSERT INTO #TempGroups VALUES ('Tyskland',30,'D')
  INSERT INTO #TempGroups VALUES ('Ukraine',31,'UA')
  INSERT INTO #TempGroups VALUES ('Ungarn',32,'H')
  INSERT INTO #TempGroups VALUES ('Østrig',33,'A')
  INSERT INTO #TempGroups VALUES ('Andre europæiske lande',34,'OE')
  INSERT INTO #TempGroups VALUES ('Sydafrika',35,'ZA')
  INSERT INTO #TempGroups VALUES ('Andre afrikanske lande',36,'OA1')
  INSERT INTO #TempGroups VALUES ('USA',37,'USA')
  INSERT INTO #TempGroups VALUES ('Canada',38,'CA')
  INSERT INTO #TempGroups VALUES ('Brasilien',39,'BR')
  INSERT INTO #TempGroups VALUES ('Andre amerikanske lande',40,'OA')
  INSERT INTO #TempGroups VALUES ('Folkerepublikken Kina',41,'CN')
  INSERT INTO #TempGroups VALUES ('Japan',42,'JP')
  INSERT INTO #TempGroups VALUES ('Sydkorea',43,'KR')
  INSERT INTO #TempGroups VALUES ('Andre asiatiske lande',44,'OAL')
  INSERT INTO #TempGroups VALUES ('Australien',45,'AU')
  INSERT INTO #TempGroups VALUES ('Andre oceaniske lande',46,'OOL')

	DECLARE db_cur CURSOR FOR
	  SELECT * FROM #TempGroups WITH (nolock)
	OPEN db_cur 
	   
	FETCH NEXT FROM db_cur INTO @GroupName, @SortNumber, @GroupCode
	    
	WHILE @@fetch_status = 0 
	BEGIN
	  -- Der er nogle lande hvor GroupName skal renames
	  IF @SortNumber = 26   -- Storbritanien 
	  BEGIN
	    UPDATE CountryGroups SET GroupName = @GroupName, GroupSortNumber = @SortNumber, GroupCode = @GroupCode  WHERE GroupName LIKE '%/ Nord irland%' OR GroupName LIKE '%/ Nordirland%'
	    PRINT 'Rename: ' + @GroupName
	    FETCH NEXT FROM db_cur INTO @GroupName, @SortNumber, @GroupCode
	    CONTINUE
	  END
	  IF @SortNumber = 34   -- Andre europæiske lande 
	  BEGIN
	    UPDATE CountryGroups SET GroupName = @GroupName, GroupSortNumber = @SortNumber, GroupCode = @GroupCode WHERE GroupName LIKE '%Europa i %vrigt%' OR GroupCode = 'EO'
	    PRINT 'Rename: ' + @GroupName
	    FETCH NEXT FROM db_cur INTO @GroupName, @SortNumber, @GroupCode
	    CONTINUE
	  END
	  IF @SortNumber = 36   -- Andre afrikanske lande 
	  BEGIN
	    UPDATE CountryGroups SET GroupName = @GroupName, GroupSortNumber = @SortNumber, GroupCode = @GroupCode WHERE GroupName LIKE '%vrige lande%' OR GroupCode = 'OO'
	    PRINT 'Rename: ' + @GroupName
	    FETCH NEXT FROM db_cur INTO @GroupName, @SortNumber, @GroupCode
	    CONTINUE
	  END
	  IF @SortNumber = 40   -- Andre amerikanske lande 
	  BEGIN
	    UPDATE CountryGroups SET GroupName = @GroupName, GroupSortNumber = @SortNumber, GroupCode = @GroupCode WHERE GroupName LIKE '%Amerika i %vrigt' OR GroupCode = 'AO'
	    PRINT 'Rename: ' + @GroupName
	    FETCH NEXT FROM db_cur INTO @GroupName, @SortNumber, @GroupCode
	    CONTINUE
	  END
	  IF @SortNumber = 44   -- Andre asiatiske lande 
	  BEGIN
	    UPDATE CountryGroups SET GroupName = @GroupName, GroupSortNumber = @SortNumber, GroupCode = @GroupCode WHERE GroupName LIKE '%Syd%stasian i %vrigt%' OR GroupName LIKE '%Syd%stasien i %vrigt%' OR GroupCode = 'SO'
	    PRINT 'Rename: ' + @GroupName
	    FETCH NEXT FROM db_cur INTO @GroupName, @SortNumber, @GroupCode
	    CONTINUE
	  END
	  IF @SortNumber = 33   -- Østrig 
	  BEGIN
	    IF EXISTS (SELECT GroupName FROM CountryGroups WITH (NOLOCK) WHERE GroupName LIKE '%strig%')
	    BEGIN
	      UPDATE CountryGroups SET GroupName = @GroupName, GroupSortNumber = @SortNumber, GroupCode = @GroupCode WHERE GroupName LIKE '%strig%'
	      PRINT 'Rename: ' + @GroupName
	    END
	    ELSE
	    BEGIN
	      INSERT INTO CountryGroups (GroupName, GroupSortNumber, GroupCode) VALUES (@GroupName,@SortNumber,@GroupCode)
	    END
	    FETCH NEXT FROM db_cur INTO @GroupName, @SortNumber, @GroupCode
	    CONTINUE
	  END
	  
	  IF NOT EXISTS (SELECT GroupName FROM CountryGroups WITH (NOLOCK) WHERE GroupName = @GroupName) 
	  BEGIN
	    PRINT 'Insert: ' + @GroupName
	    INSERT INTO CountryGroups (GroupName, GroupSortNumber, GroupCode) VALUES (@GroupName,@SortNumber,@GroupCode)
	  END
	  ELSE
	  BEGIN
	    UPDATE CountryGroups SET GroupSortNumber = @SortNumber, GroupCode = @GroupCode WHERE GroupName = @GroupName
	    PRINT 'Update: ' + @GroupName
	  END
	  
	  FETCH NEXT FROM db_cur INTO @GroupName, @SortNumber, @GroupCode
	END
END  -- End Danish countrygroups
  
CREATE TABLE #TempCountries (CountryName     varchar(30) NOT NULL,
                             CountryCode     varchar(3)  NOT NULL,
                             CurrencyCode    varchar(3)  NOT NULL,
                             CurrencyName    varchar(30) NOT NULL)
  
IF @TheCountryCode = 'SE' OR @TheCountryCode = 'S'
BEGIN
	INSERT INTO #TempCountries VALUES ('Denmark', 'DK',	'DKK', 'Danish kr.')	
	INSERT INTO #TempCountries VALUES ('Norway', 'N',	'NOK', 'Norwegian kr.')
	INSERT INTO #TempCountries VALUES ('Sweden', 'S',	'SEK', 'Swedish kr.')
	INSERT INTO #TempCountries VALUES ('USA', 'USA',	'USD', 'US Dollar')
	INSERT INTO #TempCountries VALUES ('Great Britain', 'GB', 'GBP', 'Pund')
	INSERT INTO #TempCountries VALUES ('Germany', 'D',	'FRF', 'Mark')
	INSERT INTO #TempCountries VALUES ('Portugal', 'P',	'PTE', 'Escudos')
	INSERT INTO #TempCountries VALUES ('Belgium',	'B',	'BEF', 'Franc')
	INSERT INTO #TempCountries VALUES ('Australia',	'AU',	'AUD', 'Australian Dollar')
	INSERT INTO #TempCountries VALUES ('Euro', 'EMU',	'EUR', 'Euro')
	INSERT INTO #TempCountries VALUES ('Argentina', 'AR',	'ARS', 'Argentine peso')
	INSERT INTO #TempCountries VALUES ('Albania', 'AL',	'ALL', 'Lek')
	INSERT INTO #TempCountries VALUES ('Algeria', 'DZ',	'DZD', 'Algerian dinar')
	INSERT INTO #TempCountries VALUES ('Andorra', 'AD',	'FRF', 'Fr. Franc')
	INSERT INTO #TempCountries VALUES ('Austria', 'A', 'ATS', 'Schilling')
	INSERT INTO #TempCountries VALUES ('Brazil', 'BR',	'BRL', 'Brazilian real')
	INSERT INTO #TempCountries VALUES ('Bulgaria', 'BG',	'BGL', 'Lev')
	INSERT INTO #TempCountries VALUES ('Canada', 'CA',	'CAD', 'Canadian Dollar')
	INSERT INTO #TempCountries VALUES ('Chile', 'CL',	'CLP', 'Chilean peso')
	INSERT INTO #TempCountries VALUES ('China', 'CN',	'CNY', 'Renminbi')
	INSERT INTO #TempCountries VALUES ('Colombia', 'CO', 'COP', 'Colombian peso')
	INSERT INTO #TempCountries VALUES ('Congo', 'CG', 'XAF', 'CFA franc')
	INSERT INTO #TempCountries VALUES ('Costa Rica', 'CR', 'CRC', 'Costa Rican')
	INSERT INTO #TempCountries VALUES ('Cuba', 'CU', 'CUP', 'Cuban peso')
	INSERT INTO #TempCountries VALUES ('Cyprus', 'CY', 'CYP', 'Cyprus pound')
	INSERT INTO #TempCountries VALUES ('Czech Republic', 'CZ', 'CZK', 'Czech koruna')
	INSERT INTO #TempCountries VALUES ('Egypt', 'EG', 'EGP', 'Egyptian Pound')
	INSERT INTO #TempCountries VALUES ('Equador', 'EC', 'USD', 'US dollar')
	INSERT INTO #TempCountries VALUES ('Estonia', 'EE', 'EEK', 'Estonian Kroon')
	INSERT INTO #TempCountries VALUES ('Faroe Islands', 'FO', 'DK', 'Danish kr.')
	INSERT INTO #TempCountries VALUES ('Finland', 'FIN','FIM', 'Mark')
	INSERT INTO #TempCountries VALUES ('France', 'F', 'FRF', 'Franc')
	INSERT INTO #TempCountries VALUES ('Greece', 'GR', 'GRD', 'Drachmer')
	INSERT INTO #TempCountries VALUES ('Guatemala', 'GT', 'GTQ', 'Guatemalan Quetzal')
	INSERT INTO #TempCountries VALUES ('Hungary', 'HU', 'HUF', 'Forint')
	INSERT INTO #TempCountries VALUES ('Iceland', 'IS', 'ISK', 'Icelandish kr')
	INSERT INTO #TempCountries VALUES ('Haiti', 'HT', 'HTG', 'Gourde')
	INSERT INTO #TempCountries VALUES ('India', 'IN', 'INR', 'Indian Rupee')
	INSERT INTO #TempCountries VALUES ('Indonesia', 'ID', 'IDR', 'Rupiah')
	INSERT INTO #TempCountries VALUES ('Ireland', 'IRL', 'IEP', 'Pund')
	INSERT INTO #TempCountries VALUES ('Iran', 'IR', 'IRR', 'Iranian Rial')
	INSERT INTO #TempCountries VALUES ('Iraq', 'IQ', 'IQD', 'Iraqi Dinar')
	INSERT INTO #TempCountries VALUES ('Israel', 'IL', 'ILS', 'New Shekel')
	INSERT INTO #TempCountries VALUES ('Italia', 'I', 'ITL', 'Lire')
	INSERT INTO #TempCountries VALUES ('Japan', 'JP', 'JPY', 'Yen')
	INSERT INTO #TempCountries VALUES ('Jordan', 'JO', 'JOD', 'Jordanian Dinar')
	INSERT INTO #TempCountries VALUES ('Kenya', 'KE', 'KES', 'Kenyan Shilling')
	INSERT INTO #TempCountries VALUES ('Lebanon', 'LB', 'LBP', 'Lebanese Pound')
	INSERT INTO #TempCountries VALUES ('Libya', 'LY', 'LYD', 'Libyan Dinar')
	INSERT INTO #TempCountries VALUES ('Liechtenstein', 'LI', 'CHF', 'Swiss franc')
	INSERT INTO #TempCountries VALUES ('Luxembourg', 'L', 'LUF', 'Franc')
	INSERT INTO #TempCountries VALUES ('Malta', 'MT', 'MTL', 'Maltese lira')
	INSERT INTO #TempCountries VALUES ('Mexico', 'MX', 'MXN', 'Mexican Peso')
	INSERT INTO #TempCountries VALUES ('Monaco', 'MC', 'FRF', 'French Franc')
	INSERT INTO #TempCountries VALUES ('Morocco', 'MA', 'MAD', 'Moroccan Dirham')
	INSERT INTO #TempCountries VALUES ('Netherland', 'NL', 'NLG', 'Gylden')
	INSERT INTO #TempCountries VALUES ('New Zealand', 'NZ', 'NZD', 'N.Z. Dollar')
	INSERT INTO #TempCountries VALUES ('Nicaragua', 'NI', 'NIO', 'Córdoba')
	INSERT INTO #TempCountries VALUES ('Other America', 'OA', 'OA','')
	INSERT INTO #TempCountries VALUES ('Other Europe', 'OE', 'OE','')
	INSERT INTO #TempCountries VALUES ('Other Transatlantic', 'OT','OT','')
	INSERT INTO #TempCountries VALUES ('Other World', 'OW',	'OW','')
	INSERT INTO #TempCountries VALUES ('Pakistan', 'PK', 'PKR', 'Pakistani Rupee')
	INSERT INTO #TempCountries VALUES ('Panama', 'PA', 'PAB', 'Balboa')
	INSERT INTO #TempCountries VALUES ('Paraguay', 'PY', 'PYG', 'Guaraní')
	INSERT INTO #TempCountries VALUES ('Peru', 'PE', 'PEN', 'New Sol')
	INSERT INTO #TempCountries VALUES ('Philippines', 'PH', 'PHP', 'Philippine Peso')
	INSERT INTO #TempCountries VALUES ('Poland', 'PL', 'PLN', 'Zloty')
	INSERT INTO #TempCountries VALUES ('Romania', 'RO',	'ROL', 'Leu')
	INSERT INTO #TempCountries VALUES ('Russia', 'RU', 'RUB', 'Rubler')
	INSERT INTO #TempCountries VALUES ('Saudi Arabia', 'SA', 'SAR', 'Saudi Riyal')
	INSERT INTO #TempCountries VALUES ('Spain', 'E', 'ESP', 'Pesetas')
	INSERT INTO #TempCountries VALUES ('Singapore', 'SG', 'SGD', 'Dollar')
	INSERT INTO #TempCountries VALUES ('South Africa', 'ZA', 'ZAR', 'Rand')
	INSERT INTO #TempCountries VALUES ('Switzerland', 'CH', 'CHF', 'Franc')
	INSERT INTO #TempCountries VALUES ('Sri Lanca', 'LK', 'LKR', 'Sri Lankan Rupee')
	INSERT INTO #TempCountries VALUES ('Syria', 'SY','SYP', 'Syrian Pound')
	INSERT INTO #TempCountries VALUES ('Thailand', 'TH', 'THB', 'Baht')
	INSERT INTO #TempCountries VALUES ('Tunesia', 'TN', 'TND', 'Tunisian Dinar')
	INSERT INTO #TempCountries VALUES ('Turkey', 'TR', 'TRL', 'Lira')
	INSERT INTO #TempCountries VALUES ('Uruguay', 'UY', 'UYU', 'Uruguayan Peso')
	INSERT INTO #TempCountries VALUES ('Vatican', 'VA', 'ITL', 'Italian Lira')
	INSERT INTO #TempCountries VALUES ('Venezuela', 'VE', 'VEB', 'Bolívar')
	INSERT INTO #TempCountries VALUES ('Vietnam', 'VN', 'VND', 'Dong')
	INSERT INTO #TempCountries VALUES ('Yugoslavia', 'YU', 'DIN', 'Dinar')
	INSERT INTO #TempCountries VALUES ('Latvia', 'LV', 'LVL', 'Lats')
	INSERT INTO #TempCountries VALUES ('Lithuania', 'LT', 'LTL', 'Litas')
	INSERT INTO #TempCountries VALUES ('Slovakia', 'SK', 'SKK', 'Koruna')
	INSERT INTO #TempCountries VALUES ('Slovenia', 'SI', 'SIT', 'Tolar')
	INSERT INTO #TempCountries VALUES ('Åland', 'AK', '', '')
	INSERT INTO #TempCountries VALUES ('Greenland', 'GL', '', '')
	INSERT INTO #TempCountries VALUES ('Turkia', 'TR', '', '')
	INSERT INTO #TempCountries VALUES ('Ukraine', 'UA', '', '')
	INSERT INTO #TempCountries VALUES ('Other Africa', 'OA1', '', '')
	INSERT INTO #TempCountries VALUES ('South Korea', 'KR', '', '')
	INSERT INTO #TempCountries VALUES ('Other Asien', 'XX', '', '')

	DECLARE db_cur1 CURSOR FOR
	  SELECT * FROM #TempCountries WITH (nolock)
	OPEN db_cur1 
	 
	FETCH NEXT FROM db_cur1 INTO @CountryName, @CountryCode, @CurrencyCode, @CurrencyName
	  
	WHILE @@fetch_status = 0 
	BEGIN
	  IF @CountryName = 'Germany'
	  BEGIN
	    -- Eksistere som tyskland
	    IF EXISTS (SELECT CountryName FROM Countries WITH (NOLOCK) WHERE CountryName = 'TYSKLAND')
	    BEGIN
	      SELECT @CountryGroupNumber = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupCode = @CountryCode
	      UPDATE Countries SET CountryGroupNumber = @CountryGroupNumber, MarkAsDeleted = 0, CurrencyCode = @CurrencyCode, CurrencyName = @CurrencyName WHERE CountryName = 'TYSKLAND'   
	    END
	    ELSE
	    --Eksistere som Germany
	    IF EXISTS (SELECT CountryName FROM Countries WITH (NOLOCK) WHERE CountryName = @CountryName)
	    BEGIN
	      SELECT @CountryGroupNumber = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupCode = @CountryCode
	      UPDATE Countries SET CountryGroupNumber = @CountryGroupNumber, MarkAsDeleted = 0, CurrencyCode = @CurrencyCode, CurrencyName = @CurrencyName WHERE CountryName = @CountryName   
	    END
	    ELSE
	    BEGIN
	    -- Findes slet ikke i forvejen
	      SELECT @CountryGroupNumber = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupCode = @CountryCode
	      INSERT INTO Countries (CountryName, CountryCode, MarkAsDeleted, CountryGroupNumber, CurrencyCode, CurrencyName, SortNumber) VALUES (@CountryName,@CountryCode,0,@CountryGroupNumber,@CurrencyCode,@CurrencyName, 999)
	    END
	    FETCH NEXT FROM db_cur1 INTO @CountryName, @CountryCode, @CurrencyCode, @CurrencyName
	    CONTINUE
	  END
	
	  IF @CountryName = 'Norway'
	  BEGIN
	    -- Eksistere som Norge
	    IF EXISTS (SELECT CountryName FROM Countries WITH (NOLOCK) WHERE CountryName = 'Norge')
	    BEGIN
	      SELECT @CountryGroupNumber = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupCode = @CountryCode
	      UPDATE Countries SET CountryGroupNumber = @CountryGroupNumber, MarkAsDeleted = 0, CurrencyCode = @CurrencyCode, CurrencyName = @CurrencyName WHERE CountryName = 'Norge'   
	    END
	    ELSE
	    --Eksistere som Norway
	    IF EXISTS (SELECT CountryName FROM Countries WITH (NOLOCK) WHERE CountryName = @CountryName)
	    BEGIN
		SELECT @CountryGroupNumber = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupCode = @CountryCode
	      UPDATE Countries SET CountryGroupNumber = @CountryGroupNumber, MarkAsDeleted = 0, CurrencyCode = @CurrencyCode, CurrencyName = @CurrencyName WHERE CountryName = @CountryName   
	    END
	    ELSE
	    BEGIN
	    -- Findes slet ikke i forvejen
	      SELECT @CountryGroupNumber = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupCode = @CountryCode
	      INSERT INTO Countries (CountryName, CountryCode, MarkAsDeleted, CountryGroupNumber, CurrencyCode, CurrencyName, SortNumber) VALUES (@CountryName,@CountryCode,0,@CountryGroupNumber,@CurrencyCode,@CurrencyName,999)
	    END
	    FETCH NEXT FROM db_cur1 INTO @CountryName, @CountryCode, @CurrencyCode, @CurrencyName
	    CONTINUE
	  END
	  IF @CountryName = 'Italy'
	  BEGIN
	    -- Eksistere som Italien
	    IF EXISTS (SELECT CountryName FROM Countries WITH (NOLOCK) WHERE CountryName = 'Italien')
	    BEGIN
	      SELECT @CountryGroupNumber = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupCode = @CountryCode
	      UPDATE Countries SET CountryGroupNumber = @CountryGroupNumber, MarkAsDeleted = 0, CurrencyCode = @CurrencyCode, CurrencyName = @CurrencyName WHERE CountryName = 'Italien'   
	    END
	    ELSE
	    -- Eksistere som Italien
	    IF EXISTS (SELECT CountryName FROM Countries WITH (NOLOCK) WHERE CountryName = 'Italia')
	    BEGIN
	      SELECT @CountryGroupNumber = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupCode = @CountryCode
	      UPDATE Countries SET CountryGroupNumber = @CountryGroupNumber, MarkAsDeleted = 0, CurrencyCode = @CurrencyCode, CurrencyName = @CurrencyName WHERE CountryName = 'Italia'   
	    END
	    ELSE
	    --Eksistere som Italy
	    IF EXISTS (SELECT CountryName FROM Countries WITH (NOLOCK) WHERE CountryName = @CountryName)
	    BEGIN
	      SELECT @CountryGroupNumber = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupCode = @CountryCode
	      UPDATE Countries SET CountryGroupNumber = @CountryGroupNumber, MarkAsDeleted = 0, CurrencyCode = @CurrencyCode, CurrencyName = @CurrencyName WHERE CountryName = @CountryName   
	    END
	    ELSE
	    BEGIN
	    -- Findes slet ikke i forvejen
	      SELECT @CountryGroupNumber = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupCode = @CountryCode
	      INSERT INTO Countries (CountryName, CountryCode, MarkAsDeleted, CountryGroupNumber, CurrencyCode, CurrencyName, SortNumber) VALUES (@CountryName,@CountryCode,0,@CountryGroupNumber,@CurrencyCode,@CurrencyName,999)
	    END
	    FETCH NEXT FROM db_cur1 INTO @CountryName, @CountryCode, @CurrencyCode, @CurrencyName
	    CONTINUE
	  END
	
	  IF @CountryName = 'Sweden'
	  BEGIN
	    -- Eksistere som Sverige
	    IF EXISTS (SELECT CountryName FROM Countries WITH (NOLOCK) WHERE CountryName = 'Sverige')
	    BEGIN
	      SELECT @CountryGroupNumber = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupCode = @CountryCode
	      UPDATE Countries SET CountryGroupNumber = @CountryGroupNumber, MarkAsDeleted = 0, CurrencyCode = @CurrencyCode, CurrencyName = @CurrencyName WHERE CountryName = 'Sverige'   
	    END
	    ELSE
	    --Eksistere som Sweden
	    IF EXISTS (SELECT CountryName FROM Countries WITH (NOLOCK) WHERE CountryName = @CountryName)
	    BEGIN
	      SELECT @CountryGroupNumber = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupCode = @CountryCode
	      UPDATE Countries SET CountryGroupNumber = @CountryGroupNumber, MarkAsDeleted = 0, CurrencyCode = @CurrencyCode, CurrencyName = @CurrencyName WHERE CountryName = @CountryName   
	    END
	    ELSE
	    BEGIN
	    -- Findes slet ikke i forvejen
	      SELECT @CountryGroupNumber = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupCode = @CountryCode
	      INSERT INTO Countries (CountryName, CountryCode, MarkAsDeleted, CountryGroupNumber, CurrencyCode, CurrencyName,SortNumber) VALUES (@CountryName,@CountryCode,0,@CountryGroupNumber,@CurrencyCode,@CurrencyName,999)
	    END
	    FETCH NEXT FROM db_cur1 INTO @CountryName, @CountryCode, @CurrencyCode, @CurrencyName
	    CONTINUE
	  END
	
	  IF NOT EXISTS (SELECT CountryName FROM Countries WITH (NOLOCK) WHERE CountryName = @CountryName) 
	  BEGIN
	    -- landet findes ikke - indsæt det
	    PRINT 'Insert country: ' + @CountryName
	    SELECT @CountryGroupNumber = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupCode = @CountryCode
	    INSERT INTO Countries (CountryName, CountryCode, MarkAsDeleted, CountryGroupNumber, CurrencyCode, CurrencyName, SortNumber) VALUES (@CountryName,@CountryCode,0,@CountryGroupNumber,@CurrencyCode,@CurrencyName,999)
	  END
	  ELSE
	  BEGIN 
	    -- Landet findes - kontroller at groupnumber er korrekt.
	    PRINT 'Update country: ' + @CountryName
	    SELECT @CountryGroupNumber = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupCode = @CountryCode
	    UPDATE Countries SET CountryGroupNumber = @CountryGroupNumber, MarkAsDeleted = 0, CurrencyCode = @CurrencyCode, CurrencyName = @CurrencyName WHERE CountryName = @CountryName   
	  END
	
	  FETCH NEXT FROM db_cur1 INTO @CountryName, @CountryCode, @CurrencyCode, @CurrencyName
	END --End Swedish countries...
END

IF @TheCountryCode = 'DK' OR @TheCountryCode = 'N' OR @TheCountryCode = 'GL'
BEGIN
	INSERT INTO #TempCountries VALUES ('Denmark','DK','DKK','Danske kroner')
	INSERT INTO #TempCountries VALUES ('Faroe Islands','FO','DKK','Danske kroner')
	INSERT INTO #TempCountries VALUES ('Greenland','GL','DKK','Danske kroner')
	INSERT INTO #TempCountries VALUES ('Belgium','B','BEF','Franc')
	INSERT INTO #TempCountries VALUES ('Cyprus','CY','CYP','Pound')
	INSERT INTO #TempCountries VALUES ('Estonia','EE','EEK','Kroon')
	INSERT INTO #TempCountries VALUES ('Finland','FIN','FIM','Markka')
	INSERT INTO #TempCountries VALUES ('France','F','FRF','Franc')
	INSERT INTO #TempCountries VALUES ('Greece','GR','GRD','Drachma')
	INSERT INTO #TempCountries VALUES ('Netherland','NL','NLG','Guilder')
	INSERT INTO #TempCountries VALUES ('Ireland','IRL','IEP','Punt')
	INSERT INTO #TempCountries VALUES ('Iceland','IS','ISK','Krona')
	INSERT INTO #TempCountries VALUES ('Italy','I','ITL','Lira')
	INSERT INTO #TempCountries VALUES ('Latvia','LV','LVL','Lat')
	INSERT INTO #TempCountries VALUES ('Lithuania','LT','LTL','Lita')
	INSERT INTO #TempCountries VALUES ('Luxembourg','L','LUF','Franc')
	INSERT INTO #TempCountries VALUES ('Malta','MT','MTL','Lira')
	INSERT INTO #TempCountries VALUES ('Norway','N','NOK','Krone')
	INSERT INTO #TempCountries VALUES ('Poland','PL','PLN','Zloty')
	INSERT INTO #TempCountries VALUES ('Portugal','P','PTE','Escudo')
	INSERT INTO #TempCountries VALUES ('Russia','RU','RUR','Ruble')
	INSERT INTO #TempCountries VALUES ('Switzerland','CH','CHF','Franc')
	INSERT INTO #TempCountries VALUES ('Slovakia','SK','SKK','Koruna')
	INSERT INTO #TempCountries VALUES ('Slovenia','SI','SIT','Tolar')
	INSERT INTO #TempCountries VALUES ('Spain','E','ESP','Peseta')
	INSERT INTO #TempCountries VALUES ('Great Britain','GB','GBP','Pound Sterling')
	INSERT INTO #TempCountries VALUES ('Sweden','S','SEK','Krona')
	INSERT INTO #TempCountries VALUES ('Czech Republic','CZ','CZK','Koruna')
	INSERT INTO #TempCountries VALUES ('Turkia','TR','TRL','Lira')
	INSERT INTO #TempCountries VALUES ('Germany','D','DEM','Mark')
	INSERT INTO #TempCountries VALUES ('Ukraine','UA','UAK','Hryvnia')
	INSERT INTO #TempCountries VALUES ('Hungary','H','HUF','Forint')
	INSERT INTO #TempCountries VALUES ('Austria','A','ATS','Schilling')
	INSERT INTO #TempCountries VALUES ('Other Europe','OE','OE','')
	INSERT INTO #TempCountries VALUES ('South Africa','ZA','ZAR','Rand')
	INSERT INTO #TempCountries VALUES ('Other Africa','OA1','OA1','')
	INSERT INTO #TempCountries VALUES ('USA','USA','USD','Dollar')
	INSERT INTO #TempCountries VALUES ('Canada','CA','CAD','Dollar')
	INSERT INTO #TempCountries VALUES ('Brazil','BR','BRL','Real')
	INSERT INTO #TempCountries VALUES ('Other America','OA','OA','')
	INSERT INTO #TempCountries VALUES ('China','CN','CNY','Yuan Renminbi')
	INSERT INTO #TempCountries VALUES ('Japan','JP','JPY','Yen')
	INSERT INTO #TempCountries VALUES ('South Korea','KR','KRW','Won')
	INSERT INTO #TempCountries VALUES ('Other Asien','OAL','OAL','')
	INSERT INTO #TempCountries VALUES ('Australia','AU','AUD','Dollar')
	INSERT INTO #TempCountries VALUES ('Other Transatlantic','OOL','OOL','')
	INSERT INTO #TempCountries VALUES ('Liechtenstein','LI','CHF','Franc')
	INSERT INTO #TempCountries VALUES ('Mexico','Mx','MXN','Peso')

	DECLARE db_cur1 CURSOR FOR
	  SELECT * FROM #TempCountries WITH (nolock)
	OPEN db_cur1 
	 
	FETCH NEXT FROM db_cur1 INTO @CountryName, @CountryCode, @CurrencyCode, @CurrencyName
	  
	WHILE @@fetch_status = 0 
	BEGIN
	  IF @CountryName = 'Germany'
	  BEGIN
	    -- Eksistere som tyskland
	    IF EXISTS (SELECT CountryName FROM Countries WITH (NOLOCK) WHERE CountryName = 'TYSKLAND')
	    BEGIN
	      SELECT @CountryGroupNumber = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupCode = @CountryCode
	      UPDATE Countries SET CountryGroupNumber = @CountryGroupNumber, MarkAsDeleted = 0, CurrencyCode = @CurrencyCode, CurrencyName = @CurrencyName WHERE CountryName = 'TYSKLAND'   
	    END
	    ELSE
	    --Eksistere som Germany
	    IF EXISTS (SELECT CountryName FROM Countries WITH (NOLOCK) WHERE CountryName = @CountryName)
	    BEGIN
	      SELECT @CountryGroupNumber = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupCode = @CountryCode
	      UPDATE Countries SET CountryGroupNumber = @CountryGroupNumber, MarkAsDeleted = 0, CurrencyCode = @CurrencyCode, CurrencyName = @CurrencyName WHERE CountryName = @CountryName   
	    END
	    ELSE
	    BEGIN
	    -- Findes slet ikke i forvejen
	      SELECT @CountryGroupNumber = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupCode = @CountryCode
	      INSERT INTO Countries (CountryName, CountryCode, MarkAsDeleted, CountryGroupNumber, CurrencyCode, CurrencyName, SortNumber) VALUES (@CountryName,@CountryCode,0,@CountryGroupNumber,@CurrencyCode,@CurrencyName,999)
	    END
	    FETCH NEXT FROM db_cur1 INTO @CountryName, @CountryCode, @CurrencyCode, @CurrencyName
	    CONTINUE
	  END
	
	  IF @CountryName = 'Norway'
	  BEGIN
	    -- Eksistere som Norge
	    IF EXISTS (SELECT CountryName FROM Countries WITH (NOLOCK) WHERE CountryName = 'Norge')
	    BEGIN
	      SELECT @CountryGroupNumber = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupCode = @CountryCode
	      UPDATE Countries SET CountryGroupNumber = @CountryGroupNumber, MarkAsDeleted = 0, CurrencyCode = @CurrencyCode, CurrencyName = @CurrencyName WHERE CountryName = 'Norge'   
	    END
	    ELSE
	    --Eksistere som Norway
	    IF EXISTS (SELECT CountryName FROM Countries WITH (NOLOCK) WHERE CountryName = @CountryName)
	    BEGIN
		SELECT @CountryGroupNumber = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupCode = @CountryCode
	      UPDATE Countries SET CountryGroupNumber = @CountryGroupNumber, MarkAsDeleted = 0, CurrencyCode = @CurrencyCode, CurrencyName = @CurrencyName WHERE CountryName = @CountryName   
	    END
	    ELSE
	    BEGIN
	    -- Findes slet ikke i forvejen
	      SELECT @CountryGroupNumber = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupCode = @CountryCode
	      INSERT INTO Countries (CountryName, CountryCode, MarkAsDeleted, CountryGroupNumber, CurrencyCode, CurrencyName, SortNumber) VALUES (@CountryName,@CountryCode,0,@CountryGroupNumber,@CurrencyCode,@CurrencyName,999)
	    END
	    FETCH NEXT FROM db_cur1 INTO @CountryName, @CountryCode, @CurrencyCode, @CurrencyName
	    CONTINUE
	  END
	  IF @CountryName = 'Italy'
	  BEGIN
	    -- Eksistere som Italien
	    IF EXISTS (SELECT CountryName FROM Countries WITH (NOLOCK) WHERE CountryName = 'Italien')
	    BEGIN
	      SELECT @CountryGroupNumber = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupCode = @CountryCode
	      UPDATE Countries SET CountryGroupNumber = @CountryGroupNumber, MarkAsDeleted = 0, CurrencyCode = @CurrencyCode, CurrencyName = @CurrencyName WHERE CountryName = 'Italien'   
	    END
	    ELSE
	    -- Eksistere som Italien
	    IF EXISTS (SELECT CountryName FROM Countries WITH (NOLOCK) WHERE CountryName = 'Italia')
	    BEGIN
	      SELECT @CountryGroupNumber = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupCode = @CountryCode
	      UPDATE Countries SET CountryGroupNumber = @CountryGroupNumber, MarkAsDeleted = 0, CurrencyCode = @CurrencyCode, CurrencyName = @CurrencyName WHERE CountryName = 'Italia'   
	    END
	    ELSE
	    --Eksistere som Italy
	    IF EXISTS (SELECT CountryName FROM Countries WITH (NOLOCK) WHERE CountryName = @CountryName)
	    BEGIN
	      SELECT @CountryGroupNumber = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupCode = @CountryCode
	      UPDATE Countries SET CountryGroupNumber = @CountryGroupNumber, MarkAsDeleted = 0, CurrencyCode = @CurrencyCode, CurrencyName = @CurrencyName WHERE CountryName = @CountryName   
	    END
	    ELSE
	    BEGIN
	    -- Findes slet ikke i forvejen
	      SELECT @CountryGroupNumber = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupCode = @CountryCode
	      INSERT INTO Countries (CountryName, CountryCode, MarkAsDeleted, CountryGroupNumber, CurrencyCode, CurrencyName, SortNumber) VALUES (@CountryName,@CountryCode,0,@CountryGroupNumber,@CurrencyCode,@CurrencyName,999)
	    END
	    FETCH NEXT FROM db_cur1 INTO @CountryName, @CountryCode, @CurrencyCode, @CurrencyName
	    CONTINUE
	  END
	
	  IF @CountryName = 'Sweden'
	  BEGIN
	    -- Eksistere som Sverige
	    IF EXISTS (SELECT CountryName FROM Countries WITH (NOLOCK) WHERE CountryName = 'Sverige')
	    BEGIN
	      SELECT @CountryGroupNumber = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupCode = @CountryCode
	      UPDATE Countries SET CountryGroupNumber = @CountryGroupNumber, MarkAsDeleted = 0, CurrencyCode = @CurrencyCode, CurrencyName = @CurrencyName WHERE CountryName = 'Sverige'   
	    END
	    ELSE
	    --Eksistere som Sweden
	    IF EXISTS (SELECT CountryName FROM Countries WITH (NOLOCK) WHERE CountryName = @CountryName)
	    BEGIN
	      SELECT @CountryGroupNumber = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupCode = @CountryCode
	      UPDATE Countries SET CountryGroupNumber = @CountryGroupNumber, MarkAsDeleted = 0, CurrencyCode = @CurrencyCode, CurrencyName = @CurrencyName WHERE CountryName = @CountryName   
	    END
	    ELSE
	    BEGIN
	    -- Findes slet ikke i forvejen
	      SELECT @CountryGroupNumber = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupCode = @CountryCode
	      INSERT INTO Countries (CountryName, CountryCode, MarkAsDeleted, CountryGroupNumber, CurrencyCode, CurrencyName, SortNumber) VALUES (@CountryName,@CountryCode,0,@CountryGroupNumber,@CurrencyCode,@CurrencyName, 999)
	    END
	    FETCH NEXT FROM db_cur1 INTO @CountryName, @CountryCode, @CurrencyCode, @CurrencyName
	    CONTINUE
	  END
	
	  IF NOT EXISTS (SELECT CountryName FROM Countries WITH (NOLOCK) WHERE CountryName = @CountryName) 
	  BEGIN
	    -- landet findes ikke - indsæt det
	    PRINT 'Insert country: ' + @CountryName
	    SELECT @CountryGroupNumber = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupCode = @CountryCode
	    INSERT INTO Countries (CountryName, CountryCode, MarkAsDeleted, CountryGroupNumber, CurrencyCode, CurrencyName, SortNumber) VALUES (@CountryName,@CountryCode,0,@CountryGroupNumber,@CurrencyCode,@CurrencyName, 999)
	  END
	  ELSE
	  BEGIN 
	    -- Landet findes - kontroller at groupnumber er korrekt.
	    PRINT 'Update country: ' + @CountryName
	    SELECT @CountryGroupNumber = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupCode = @CountryCode
	    UPDATE Countries SET CountryGroupNumber = @CountryGroupNumber, MarkAsDeleted = 0, CurrencyCode = @CurrencyCode, CurrencyName = @CurrencyName WHERE CountryName = @CountryName   
	  END
	
	  FETCH NEXT FROM db_cur1 INTO @CountryName, @CountryCode, @CurrencyCode, @CurrencyName
	END --end Danish/Norwegian/Grønland countries
END

IF @TheCountryCode = 'S' OR @TheCountryCode = 'SE'
BEGIN
	--Hent Hvilket nr de enkelte grupper har fået.
	SELECT @OtherEurope = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupSortNumber = 28
	SELECT @OtherAfrica = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupSortNumber = 37
	SELECT @OtherAmerica = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupSortNumber = 31
	SELECT @OtherAsia = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupSortNumber = 34
	SELECT @OtherOceanic = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupSortNumber = 37
	SELECT @Denmark = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupSortNumber = 2
	SELECT @Finland = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupSortNumber = 4
END 

IF @TheCountryCode = 'N'
BEGIN
	--Hent Hvilket nr de enkelte grupper har fået.
	SELECT @OtherEurope = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupSortNumber = 33
	SELECT @OtherAfrica = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupSortNumber = 46
	SELECT @OtherAmerica = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupSortNumber = 38
	SELECT @OtherAsia = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupSortNumber = 44
	SELECT @OtherOceanic = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupSortNumber = 40
END 

IF @TheCountryCode = 'DK'
BEGIN
	--Hent Hvilket nr de enkelte grupper har fået.
	SELECT @OtherEurope = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupSortNumber = 34
	SELECT @OtherAfrica = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupSortNumber = 36
	SELECT @OtherAmerica = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupSortNumber = 40
	SELECT @OtherAsia = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupSortNumber = 44
	SELECT @OtherOceanic = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupSortNumber = 46
END
	
IF @TheCountryCode = 'GL'
BEGIN
	--Hent Hvilket nr de enkelte grupper har fået.
	SELECT @OtherEurope = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupSortNumber = 11
	SELECT @OtherAfrica = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupSortNumber = 15
	SELECT @OtherAmerica = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupSortNumber = 15
	SELECT @OtherAsia = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupSortNumber = 15
	SELECT @OtherOceanic = GroupNumber FROM CountryGroups WITH (NOLOCK) WHERE GroupSortNumber = 15
END

--hent liste over alle andre lande og del op i korrekte grupper
DECLARE db_cur2 CURSOR FOR
  SELECT CountryName, CountryGroupNumber FROM Countries WITH (nolock)
OPEN db_cur2 
 
FETCH NEXT FROM db_cur2 INTO @CountryName, @CountryGroupNumber
  
WHILE @@fetch_status = 0 
BEGIN
  IF @CountryName = 'Algeria' or @CountryName = 'Angola' or @CountryName = 'Benin' or @CountryName = 'Botswana' or @CountryName = 'Burkina Faso' or 
     @CountryName = 'Burundi' or @CountryName = 'Cameroon' or @CountryName = 'Cape Verde' or @CountryName = 'Central African Republic' or @CountryName = 'Chad' or 
     @CountryName = 'Comoros' or @CountryName = 'Cote dIvoire' or @CountryName = 'Egypt' or @CountryName = 'Equatorial Guinea' or @CountryName = 'Eritrea' or 
     @CountryName = 'Ethiopia' or @CountryName = 'Gabon' or @CountryName = 'Gambia' or @CountryName = 'Ghana' or @CountryName = 'Guinea-Bissau' or 
     @CountryName = 'Guinea' or @CountryName = 'Kenya' or @CountryName = 'Liberia' or @CountryName = 'Libya' or @CountryName = 'Madagascar' or @CountryName = 'Malawi' or 
     @CountryName = 'Mali' or @CountryName = 'Mauritania' or @CountryName = 'Mauritius' or @CountryName = 'Morocco' or @CountryName = 'Mozambique' or 
     @CountryName = 'Namibia' or @CountryName = 'Nigeria' or @CountryName = 'Niger' or @CountryName = 'Republic of the Congo' or @CountryName = 'Rwanda' or 
     @CountryName = 'Sao Tome and Principe' or @CountryName = 'Senegal' or @CountryName = 'Seychelles' or @CountryName = 'Sierra Leone' or @CountryName = 'Somalia' or 
     @CountryName = 'Sudan' or @CountryName = 'Swaziland' or @CountryName = 'Tanzania' or @CountryName = 'Togo' or @CountryName = 'Tunisia' or 
     @CountryName = 'Uganda' or @CountryName = 'Zambia' or @CountryName = 'Zimbabwe' or @CountryName = 'Congo' or @CountryName = 'Tunesia' 
  BEGIN
    -- Africa
    UPDATE Countries SET CountryGroupNumber = @OtherAfrica WHERE CountryName = @CountryName 
  END

  IF @CountryName = 'Armenia' OR @CountryName = 'ORAzerbaijan' OR @CountryName = 'Bangladesh' OR @CountryName = 'Brunei' OR @CountryName = 'Cambodia' OR
     @CountryName = 'Georgia' OR @CountryName = 'Hong Kong' OR @CountryName = 'India' OR @CountryName = 'Indonesia' OR @CountryName = 'Iran' OR
     @CountryName = 'Iraq' OR @CountryName = 'Israel' OR @CountryName = 'Jordan' OR @CountryName = 'Kazakhstan' OR @CountryName = 'Kuwait' OR
     @CountryName = 'Laos' OR @CountryName = 'Lebanon' OR @CountryName = 'Macao' OR @CountryName = 'Malaysia' OR @CountryName = 'Maldives' OR @CountryName = 'Mongolia' OR
     @CountryName = 'Myanmar' OR @CountryName = 'Nepal' OR @CountryName = 'North Korea' OR @CountryName = 'Oman' OR @CountryName = 'Pakistan' OR
     @CountryName = 'Philippines' OR @CountryName = 'Qatar' OR @CountryName = 'Saudi Arabia' OR @CountryName = 'Singapore' OR @CountryName = 'Sri Lanka' OR
     @CountryName = 'Syria' OR @CountryName = 'Taiwan' OR @CountryName = 'Tajikistan' OR @CountryName = 'Oman' OR @CountryName = 'Thailand' OR
     @CountryName = 'United Arab Emirates' OR @CountryName = 'Vietnam' OR @CountryName = 'Yemen' OR @CountryName = 'Sri Lanca'
  BEGIN
    -- Asia
    UPDATE Countries SET CountryGroupNumber = @OtherAsia WHERE CountryName = @CountryName 
  END

  IF @CountryName = 'Federated States of Micronesia' OR @CountryName = 'Fiji' OR @CountryName = 'Kiribati' OR @CountryName = 'Marshall Islands' OR
     @CountryName = 'Nauru' OR @CountryName = 'New Zealand' OR @CountryName = 'Palau' OR @CountryName = 'Papua New Guinea' OR @CountryName = 'Samoa' OR
     @CountryName = 'Solomon Islands' OR @CountryName = 'Tonga' OR @CountryName = 'Tuvalu' OR @CountryName = 'Vanuatu'
  BEGIN
    -- Oceanic
    UPDATE Countries SET CountryGroupNumber = @OtherOceanic WHERE CountryName = @CountryName 
  END

  IF @CountryName = 'Albania' or @CountryName = 'Andorra' or @CountryName = 'Belarus' or @CountryName = 'Bosnia and Herzegovina' or @CountryName = 'Bulgaria' or
     @CountryName = 'Croatia' or @CountryName = 'Liechtenstein' or @CountryName = 'Macedonia' or @CountryName = 'Moldova' or @CountryName = 'Romania' or
     @CountryName = 'San Marino' or @CountryName = 'Vatican City' or @CountryName = 'Yugoslavia' OR @CountryName = 'Euro' or @CountryName = 'Vatican'
  BEGIN
    -- Europe 
    UPDATE Countries SET CountryGroupNumber = @OtherEurope WHERE CountryName = @CountryName 
  END

  IF @CountryName = 'Belize' OR @CountryName = 'Costa Rica' OR @CountryName = 'Cuba' OR @CountryName = 'Dominican Republic' OR @CountryName = 'El Salvador' OR
     @CountryName = 'Guatemala' OR @CountryName = 'Haiti' OR @CountryName = 'Honduras' OR @CountryName = 'Jamaica' OR @CountryName = 'Mexico' OR
     @CountryName = 'Nicaragua' OR @CountryName = 'Panama' OR @CountryName = 'Argentina' OR @CountryName = 'Bolivia' OR @CountryName = 'Chile' OR
     @CountryName = 'Colombia' OR @CountryName = 'Ecuador' OR @CountryName = 'French Guiana' OR @CountryName = 'Guyana' OR @CountryName = 'Paraguay' OR
     @CountryName = 'Peru' OR @CountryName = 'Suriname' OR @CountryName = 'Uruguay' OR @CountryName = 'Venezuela' OR @CountryName = 'Equador'
  BEGIN
    --America
    UPDATE Countries SET CountryGroupNumber = @OtherAmerica WHERE CountryName = @CountryName 
  END

  -- For swedish report: Some countries should be handled another way
  IF @TheCountryCode = 'S' OR @TheCountryCode = 'SE'
  BEGIN
    IF @CountryName = 'Åland'
    BEGIN
      --Finland
  	  UPDATE Countries SET CountryGroupNumber = @Finland WHERE CountryName = @CountryName 
    END
    IF @CountryName = 'Greenland'
    BEGIN
      --Denmark
  	  UPDATE Countries SET CountryGroupNumber = @Denmark WHERE CountryName = @CountryName 
    END
    IF @CountryName = 'Other Transatlantic' OR @CountryName = 'Other World' OR @CountryName = 'South Africa' OR @CountryName = 'Other Africa' 
    BEGIN
      --Other world
  	  UPDATE Countries SET CountryGroupNumber = @OtherOceanic WHERE CountryName = @CountryName 
    END
    
    IF @CountryName = 'Brazil' or @CountryName = 'Other America'
    BEGIN
      --Other america
      UPDATE Countries SET CountryGroupNumber = @OtherAmerica WHERE CountryName = @CountryName
    END
    IF @CountryName = 'China' OR @CountryName = 'Other Asien'
    BEGIN
      -- Asia
	    UPDATE Countries SET CountryGroupNumber = @OtherAsia WHERE CountryName = @CountryName 
    END
    IF @CountryName = 'Cyprus' OR @CountryName = 'Faroe Islands' OR @CountryName = 'Israel' OR @CountryName = 'Malta' OR
       @CountryName = 'Monaco' OR @CountryName = 'Turkey' OR @CountryName = 'Other Europe' OR @CountryName = 'Ukraine' OR @CountryName = 'Turkia'
	  BEGIN
  	  -- Europe 
    	UPDATE Countries SET CountryGroupNumber = @OtherEurope WHERE CountryName = @CountryName 
	  END
  END

  IF @TheCountryCode = 'GL'
  BEGIN
	  IF @CountryName = 'Algeria' or @CountryName = 'Angola' or @CountryName = 'Benin' or @CountryName = 'Botswana' or @CountryName = 'Burkina Faso' or 
	     @CountryName = 'Burundi' or @CountryName = 'Cameroon' or @CountryName = 'Cape Verde' or @CountryName = 'Central African Republic' or @CountryName = 'Chad' or 
	     @CountryName = 'Comoros' or @CountryName = 'Cote dIvoire' or @CountryName = 'Egypt' or @CountryName = 'Equatorial Guinea' or @CountryName = 'Eritrea' or 
	     @CountryName = 'Ethiopia' or @CountryName = 'Gabon' or @CountryName = 'Gambia' or @CountryName = 'Ghana' or @CountryName = 'Guinea-Bissau' or 
	     @CountryName = 'Guinea' or @CountryName = 'Kenya' or @CountryName = 'Liberia' or @CountryName = 'Libya' or @CountryName = 'Madagascar' or @CountryName = 'Malawi' or 
	     @CountryName = 'Mali' or @CountryName = 'Mauritania' or @CountryName = 'Mauritius' or @CountryName = 'Morocco' or @CountryName = 'Mozambique' or 
	     @CountryName = 'Namibia' or @CountryName = 'Nigeria' or @CountryName = 'Niger' or @CountryName = 'Republic of the Congo' or @CountryName = 'Rwanda' or 
	     @CountryName = 'Sao Tome and Principe' or @CountryName = 'Senegal' or @CountryName = 'Seychelles' or @CountryName = 'Sierra Leone' or @CountryName = 'Somalia' or 
	     @CountryName = 'Sudan' or @CountryName = 'Swaziland' or @CountryName = 'Tanzania' or @CountryName = 'Togo' or @CountryName = 'Tunisia' or 
	     @CountryName = 'Uganda' or @CountryName = 'Zambia' or @CountryName = 'Zimbabwe' or @CountryName = 'Congo' or @CountryName = 'Tunesia' or @CountryName = 'South Africa' 
	  BEGIN
	    -- Africa
	    UPDATE Countries SET CountryGroupNumber = @OtherAfrica WHERE CountryName = @CountryName 
	  END
	
	  IF @CountryName = 'Armenia' OR @CountryName = 'ORAzerbaijan' OR @CountryName = 'Bangladesh' OR @CountryName = 'Brunei' OR @CountryName = 'Cambodia' OR
	     @CountryName = 'Georgia' OR @CountryName = 'Hong Kong' OR @CountryName = 'India' OR @CountryName = 'Indonesia' OR @CountryName = 'Iran' OR
	     @CountryName = 'Iraq' OR @CountryName = 'Israel' OR @CountryName = 'Jordan' OR @CountryName = 'Kazakhstan' OR @CountryName = 'Kuwait' OR
	     @CountryName = 'Laos' OR @CountryName = 'Lebanon' OR @CountryName = 'Macao' OR @CountryName = 'Malaysia' OR @CountryName = 'Maldives' OR @CountryName = 'Mongolia' OR
	     @CountryName = 'Myanmar' OR @CountryName = 'Nepal' OR @CountryName = 'North Korea' OR @CountryName = 'Oman' OR @CountryName = 'Pakistan' OR
	     @CountryName = 'Philippines' OR @CountryName = 'Qatar' OR @CountryName = 'Saudi Arabia' OR @CountryName = 'Singapore' OR @CountryName = 'Sri Lanka' OR
	     @CountryName = 'Syria' OR @CountryName = 'Taiwan' OR @CountryName = 'Tajikistan' OR @CountryName = 'Oman' OR @CountryName = 'Thailand' OR
	     @CountryName = 'United Arab Emirates' OR @CountryName = 'Vietnam' OR @CountryName = 'Yemen' OR @CountryName = 'Sri Lanca' or 
       @CountryName = 'China' or @CountryName = 'South Korea' OR @CountryName = 'Other Transatlantic' or @CountryName = 'Other Asien'
	  BEGIN
	    -- Asia
	    UPDATE Countries SET CountryGroupNumber = @OtherAsia WHERE CountryName = @CountryName 
	  END
	
	  IF @CountryName = 'Federated States of Micronesia' OR @CountryName = 'Fiji' OR @CountryName = 'Kiribati' OR @CountryName = 'Marshall Islands' OR
	     @CountryName = 'Nauru' OR @CountryName = 'New Zealand' OR @CountryName = 'Palau' OR @CountryName = 'Papua New Guinea' OR @CountryName = 'Samoa' OR
	     @CountryName = 'Solomon Islands' OR @CountryName = 'Tonga' OR @CountryName = 'Tuvalu' OR @CountryName = 'Vanuatu' or @CountryName = 'Australia'
	  BEGIN
	    -- Oceanic
	    UPDATE Countries SET CountryGroupNumber = @OtherOceanic WHERE CountryName = @CountryName 
	  END
	
	  IF @CountryName = 'Albania' or @CountryName = 'Andorra' or @CountryName = 'Belarus' or @CountryName = 'Bosnia and Herzegovina' or @CountryName = 'Bulgaria' or
	     @CountryName = 'Croatia' or @CountryName = 'Liechtenstein' or @CountryName = 'Macedonia' or @CountryName = 'Moldova' or @CountryName = 'Romania' or
	     @CountryName = 'San Marino' or @CountryName = 'Vatican City' or @CountryName = 'Yugoslavia' OR @CountryName = 'Euro' or @CountryName = 'Vatican' OR
       @CountryName = 'Faroe Islands' or @CountryName = 'Belgium' or @CountryName = 'Cyprus' or @CountryName = 'Estonia' or @CountryName = 'Finland' or @CountryName = 'Greece' or
       @CountryName = 'Ireland' or @CountryName = 'Latvia' or @CountryName = 'Lithuania' or @CountryName = 'Luxembourg' or @CountryName = 'Malta' or 
       @CountryName = 'Poland' or @CountryName = 'Portugal' or @CountryName = 'Russia' or @CountryName = 'Switzerland' or @CountryName = 'Slovakia' or 
       @CountryName = 'Slovenia' or @CountryName = 'Spain' or @CountryName = 'Czech Republic' or @CountryName = 'Turkia' or @CountryName = 'Ukraine' or
       @CountryName = 'Hungary' or @CountryName = 'Austria'
	  BEGIN
	    -- Europe 
	    UPDATE Countries SET CountryGroupNumber = @OtherEurope WHERE CountryName = @CountryName 
	  END
	
	  IF @CountryName = 'Belize' OR @CountryName = 'Costa Rica' OR @CountryName = 'Cuba' OR @CountryName = 'Dominican Republic' OR @CountryName = 'El Salvador' OR
	     @CountryName = 'Guatemala' OR @CountryName = 'Haiti' OR @CountryName = 'Honduras' OR @CountryName = 'Jamaica' OR @CountryName = 'Mexico' OR
	     @CountryName = 'Nicaragua' OR @CountryName = 'Panama' OR @CountryName = 'Argentina' OR @CountryName = 'Bolivia' OR @CountryName = 'Chile' OR
	     @CountryName = 'Colombia' OR @CountryName = 'Ecuador' OR @CountryName = 'French Guiana' OR @CountryName = 'Guyana' OR @CountryName = 'Paraguay' OR
	     @CountryName = 'Peru' OR @CountryName = 'Suriname' OR @CountryName = 'Uruguay' OR @CountryName = 'Venezuela' OR @CountryName = 'Equador' OR 
       @CountryName = 'Mexico' OR @CountryName = 'Brazil'
	  BEGIN
	    --America
	    UPDATE Countries SET CountryGroupNumber = @OtherAmerica WHERE CountryName = @CountryName 
	  END
  END

  FETCH NEXT FROM db_cur2 INTO @CountryName, @CountryGroupNumber
END

IF (@TheCountryCode = 'S' OR @TheCountryCode = 'SE')  
BEGIN
	UPDATE CountryGroups SET GroupCode = 'SWE' WHERE GroupCode = 'S'
	UPDATE CountryGroups SET GroupCode = 'DNK' WHERE GroupCode = 'DK'
	UPDATE CountryGroups SET GroupCode = 'NOK' WHERE GroupCode = 'N'
	UPDATE CountryGroups SET GroupCode = 'DEU' WHERE GroupCode = 'D'
	UPDATE CountryGroups SET GroupCode = 'NLD' WHERE GroupCode = 'NL'
	UPDATE CountryGroups SET GroupCode = 'BEL' WHERE GroupCode = 'B'
	UPDATE CountryGroups SET GroupCode = 'LUX' WHERE GroupCode = 'L'
	UPDATE CountryGroups SET GroupCode = 'FRA' WHERE GroupCode = 'FR'
	UPDATE CountryGroups SET GroupCode = 'ITA' WHERE GroupCode = 'I'
	UPDATE CountryGroups SET GroupCode = 'ESP' WHERE GroupCode = 'E'
	UPDATE CountryGroups SET GroupCode = 'AUT' WHERE GroupCode = 'A'
	UPDATE CountryGroups SET GroupCode = 'CHE' WHERE GroupCode = 'CH'
	UPDATE CountryGroups SET GroupCode = 'POL' WHERE GroupCode = 'PL'
	UPDATE CountryGroups SET GroupCode = 'CAN' WHERE GroupCode = 'CA'
	UPDATE CountryGroups SET GroupCode = 'JPN' WHERE GroupCode = 'JP'
	UPDATE CountryGroups SET GroupCode = 'ISL' WHERE GroupCode = 'IS'
	UPDATE CountryGroups SET GroupCode = 'PRT' WHERE GroupCode = 'P'
	UPDATE CountryGroups SET GroupCode = 'GRC' WHERE GroupCode = 'GR'
	UPDATE CountryGroups SET GroupCode = 'RUS' WHERE GroupCode = 'RU'
	UPDATE CountryGroups SET GroupCode = 'EST' WHERE GroupCode = 'EE'
	UPDATE CountryGroups SET GroupCode = 'LVA' WHERE GroupCode = 'LV'
	UPDATE CountryGroups SET GroupCode = 'LTU' WHERE GroupCode = 'LT'
	UPDATE CountryGroups SET GroupCode = 'CZE' WHERE GroupCode = 'CZ'
	UPDATE CountryGroups SET GroupCode = 'SVK' WHERE GroupCode = 'SK'
	UPDATE CountryGroups SET GroupCode = 'HUN' WHERE GroupCode = 'HU'
	UPDATE CountryGroups SET GroupCode = 'KOR' WHERE GroupCode = 'KR'
	UPDATE CountryGroups SET GroupCode = 'AUS' WHERE GroupCode = 'AU'
	UPDATE CountryGroups SET GroupCode = 'NZL' WHERE GroupCode = 'NZ'
END

IF @TheCountryCode = 'GL'
BEGIN
  -- Ryd op i Grønlandske grupper....
  UPDATE Countries SET CountryGroupNumber = 51 WHERE CountryCode = 'F'
  UPDATE Countries SET CountryGroupNumber = 61 WHERE CountryCode = 'JP' OR CountryName = 'JAPAN'
  DELETE CountryGroups WHERE GroupCode = 'FIN' OR GroupCode = 'BE' OR GroupCode = 'ES' OR  GroupCode = 'AU' 
  OR GroupCode = 'SC' OR GroupCode = 'PO' OR GroupCode = 'AO' OR GroupCode = 'SO'
END

IF @TheCountryCode = 'GL'
  DEALLOCATE db_curGL
ELSE
  DEALLOCATE db_cur
DEALLOCATE db_cur1
DEALLOCATE db_cur2

DROP TABLE #TempGroups
DROP TABLE #TempCountries