Files
DP44/DataPRO_sql/dbo.foo_IdGetDAS.UserDefinedFunction.sql
2026-04-17 14:55:32 -04:00

29 lines
1.7 KiB
Transact-SQL
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[foo_IdGetDAS]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[foo_IdGetDAS]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[foo_IdGetDAS]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'CREATE FUNCTION [dbo].[foo_IdGetDAS]
(
@HardwareId varchar(50)
)
RETURNS int
AS
BEGIN
RETURN (case
when @HardwareId is null then 0
when charindex(''_'', @HardwareId) = 0 then isnull((select top 1 d.DASId from [dbo].[DAS] d where d.SerialNumber= @HardwareId), 0)
else isnull((select top 1 d.DASId from [dbo].[DAS] d where d.SerialNumber= left(@HardwareId, charindex(''_'', @HardwareId)-1)), 0)
end)
END
'
END
GO