Having trouble creating a scalar function inside MS SQL 2005. Any suggestions would be appreciated. The title1_type field should be set according to the case statement. The sku is passed which should determine the type base on the case statement.
create FUNCTION [dbo].[gettitle1_type] (@sku varchar(50))
RETURNS varchar(50) AS
BEGIN
DECLARE @title1_type varchar(50)
DECLARE @title1 varchar(50)
select @title1_type = case @title1
when @title1 like '%SMALL%' then 'size'
when @title1 like '%large%' then 'size'
when @title1 like '%pink%' then 'color'
when @title1 like '%red%' then 'color'
when @title1 like '%brunette%' then 'color'
else ''
END;
where sku = @sku
RETURN isnull(@title1_type,'')
You need to
END;This is not the end of the statement. Optionally you could put one afterwhere sku = @skuthough.case @title1, useCaseinstead. You are mingling the 2 forms of theCASEexpression.ENDto finish the function definition.WHEREor add aFROMWITH SCHEMABINDINGon scalar UDFs that do not do data access. This can help performance in some cases.Giving the below