我寫下面的函數返回後綴傳遞名稱作爲參數。我把名字後綴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();' –