我写下面的函数返回后缀传递名称作为参数。我把名字后缀exsist作为普通表格表达并试图与之比较。任何人都可以解释我做这件事的正确方法吗?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.
的错误是不言自明的,不是吗?看到这篇博客文章,你应该习惯使用语句终结符:http://sqlblog.com/blogs/aaron_bertrand/archive/2009/09/03/ladies-and-gentlemen-start-your-semi-colons .aspx另外这个关于使用模式前缀(特别适用于用户定义的函数):http://sqlblog.com/blogs/aaron_bertrand/archive/2009/10/11/bad-habits-to-kick-avoiding- the-schema-prefix.aspx –
将下列错误放置后,仍然出现以下错误:为CTE之前的diclaration。 Msg 102,Level 15,State 1,Procedure S,Line 24 ';'附近语法不正确。 – Shine
这会产生什么? 'SELECT compatibility_level FROM sys.databases WHERE database_id = DB_ID();' –