

/****** Object:  UserDefinedFunction [dbo].[apiGetCustLocIdGetERP]    Script Date: 07.09.2021 06:10:21 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



CREATE FUNCTION [dbo].[apiGetCustLocIdGetERP]
(
	@CustLocId int,
	@VendorId int,
	@DeviceUid nvarchar(50),
	@TS int
)
RETURNS  int
BEGIN
   DECLARE @res int;
 set @res = ISNULL((
	  select top 1 Id from dbo.ApiCustomerLocations where  
  CustLocId = @CustLocId
  and VendorId = @VendorId  
  and ISNULL(DeviceUid,'') = ISNULL(@DeviceUid,'')
  and (ABS(SaveTS - @TS))/(60*60*24)< 10 -- nie więcej niż 10 dni różnicy
       ORDER BY SaveTS DESC
  )
	
	--,*
	 ,0);
	 return @res;
END




GO


