在MS SQL 2005中创建标量函数时遇到问题。任何建议都将受到赞赏。 title1_type字段应根据case语句设置。传递sku,它应该确定case语句的类型。
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,'')
答案 0 :(得分:3)
你需要
END;
之后摆脱半冒号这不是声明的结尾。您可以选择在where sku = @sku
之后添加一个。case @title1
,而是使用Case
。你正在混淆2 forms of the CASE
expression。 END
以完成功能定义。WHERE
或添加FROM
WITH SCHEMABINDING
放在不进行数据访问的标量UDF上。在某些情况下,这可以帮助提高性能。给出以下
CREATE FUNCTION [dbo].[gettitle1_type] (@sku VARCHAR(50))
RETURNS VARCHAR(50)
WITH SCHEMABINDING
AS
BEGIN
DECLARE @title1_type VARCHAR(50)
DECLARE @title1 VARCHAR(50) /*This is never assigned to ?!*/
SELECT @title1_type = CASE
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
/*
FROM xyz
WHERE sku = @sku;*/
RETURN ISNULL(@title1_type,'')
END