CTE 错误。任何人都可以帮我纠正以下错误
我正在编写下面的函数来返回后缀,传递名称作为参数。我将后缀可能存在于名称中的情况作为公用表表达式并尝试与之进行比较。任何人都可以向我解释一下正确的做法吗?
Alter function S (@Name varchar(100))
returns varchar(25)
as
begin
declare @Suffix varchar(25)
WITH SearchTerms(Term)
AS (SELECT ' I '
UNION ALL
SELECT ' II '
UNION ALL
SELECT ' III '
UNION All
SELECT ' MD '
UNION All
SELECT ' M.D '
UNION All
SELECT ' M.D. '
UNION All
SELECT ' D.O '
UNION All
SELECT ' D.O. '
UNION All
SELECT ' DO '
) ;
set @Suffix = (select Term from SearchTerms where Charindex(Term,@Name) > 0)
return @Suffix
end
错误信息。
Msg 319, Level 15, State 1, Procedure S, Line 6
Incorrect syntax near the keyword 'with'. If this statement is a common table expression,
an xmlnamespaces clause or a change tracking context clause, the previous statement must
be terminated with a semicolon.
I am writing below function to return Suffix passing name as parameter. I made possible cases of suffix exsist in name as common table expression and trying to compare with that. Could any one explain me the proper way of doing it.
Alter function S (@Name varchar(100))
returns varchar(25)
as
begin
declare @Suffix varchar(25)
WITH SearchTerms(Term)
AS (SELECT ' I '
UNION ALL
SELECT ' II '
UNION ALL
SELECT ' III '
UNION All
SELECT ' MD '
UNION All
SELECT ' M.D '
UNION All
SELECT ' M.D. '
UNION All
SELECT ' D.O '
UNION All
SELECT ' D.O. '
UNION All
SELECT ' DO '
) ;
set @Suffix = (select Term from SearchTerms where Charindex(Term,@Name) > 0)
return @Suffix
end
Error message.
Msg 319, Level 15, State 1, Procedure S, Line 6
Incorrect syntax near the keyword 'with'. If this statement is a common table expression,
an xmlnamespaces clause or a change tracking context clause, the previous statement must
be terminated with a semicolon.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我现在看到了,您的 CTE 声明的末尾有一个分号。你不能这样做,因为这会终止语句。试试这个:
根据新信息编辑,这是一个返回所有结果的表值函数:
I see it now, you have a semi-colon at the end of your CTE declaration. You can't do that since that terminates the statement. Try this:
EDIT based on new information, here is a table-valued function that returns all results: