CREATE FUNCTION [dbo].[GetHotelSetupKeyValue] (@Setting varchar(60), @CustomerNumber int = -1)
returns varchar(2000) AS
/*
  Created by ???
  Changed:
  20101013 OF: Bug: Output length changed to 2000. Now matching hotelsetup keyvalue.
  20101013 OF: EXT: Implementation of resolving of default hotelsetup setting if setting not found in hotelsetup.
  20071003 JP: BUG: Nothing is correct so recoded
*/
begin
declare @Value varchar(2000)

if (@CustomerNumber < 0) 
begin
  -- get hotelsetup setting
  select @Value= KeyValue from dbo.HotelSetup WITH (nolock) where Upper(Searchkey) = Upper(@Setting)
  if @@rowcount=0
	-- get default hotelsetup setting if not found in hotelsetup
	select @Value = DefaultValue from dbo.HotelSetup_DefaultValues  WITH (NOLOCK) WHERE Upper(SearchKey) = Upper(@Setting)
end
else
begin
  -- get hotelsetup setting for the customer
  select top 1 @Value = isnull(isnull(v.KeyValue, h.KeyValue), '') from dbo.HotelSetup h WITH (nolock) 
  inner join dbo.HotelSetupValueXTab vx WITH (nolock) on vx.HotelSetupNumber = h.HotelSetupNumber and Upper(h.SearchKey) = Upper(@Setting) and vx.customernumber = @CustomerNumber
  inner join dbo.HotelSetupValue v WITH (nolock) on vx.fk_ValueNumber = v.Pk_ValueNumber
  order by Pk_ValueNumber desc
  if @@rowcount=0
  begin
    -- get hotelsetup setting if not found for the customer
    select @Value= KeyValue from dbo.HotelSetup WITH (nolock) where Upper(Searchkey) = Upper(@Setting) 
    if @@rowcount=0
      -- get default hotelsetup setting if not found in hotelsetup
      select @Value = DefaultValue from dbo.HotelSetup_DefaultValues  WITH (NOLOCK) WHERE Upper(SearchKey) = Upper(@Setting) 
  end
end

return @value
 
end