CTE 错误。任何人都可以帮我纠正以下错误

发布于 2024-12-05 19:53:45 字数 1022 浏览 2 评论 0原文

我正在编写下面的函数来返回后缀,传递名称作为参数。我将后缀可能存在于名称中的情况作为公用表表达式并尝试与之进行比较。任何人都可以向我解释一下正确的做法吗?

  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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独闯女儿国 2024-12-12 19:53:45

我现在看到了,您的 CTE 声明的末尾有一个分号。你不能这样做,因为这会终止语句。试试这个:

ALTER FUNCTION dbo.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 '
    )
    SELECT @Suffix = Term 
        FROM SearchTerms 
        WHERE CHARINDEX(Term, ' ' + @Name + ' ') > 0;

    RETURN (LTRIM(RTRIM(@Suffix)));
END
GO

根据新信息编辑,这是一个返回所有结果的表值函数:

CREATE FUNCTION dbo.T
(
    @Name VARCHAR(100)
)
RETURNS TABLE
AS
    RETURN
    (
        SELECT 
            Term = CONVERT(VARCHAR(25), LTRIM(RTRIM(Term)))
        FROM
        (
            SELECT Term = ' 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 '
        ) AS Terms
        WHERE CHARINDEX(Term, ' ' + @Name + ' ') > 0
    );
GO

SELECT Term FROM dbo.T('Terry Allen MD III');

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:

ALTER FUNCTION dbo.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 '
    )
    SELECT @Suffix = Term 
        FROM SearchTerms 
        WHERE CHARINDEX(Term, ' ' + @Name + ' ') > 0;

    RETURN (LTRIM(RTRIM(@Suffix)));
END
GO

EDIT based on new information, here is a table-valued function that returns all results:

CREATE FUNCTION dbo.T
(
    @Name VARCHAR(100)
)
RETURNS TABLE
AS
    RETURN
    (
        SELECT 
            Term = CONVERT(VARCHAR(25), LTRIM(RTRIM(Term)))
        FROM
        (
            SELECT Term = ' 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 '
        ) AS Terms
        WHERE CHARINDEX(Term, ' ' + @Name + ' ') > 0
    );
GO

SELECT Term FROM dbo.T('Terry Allen MD III');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文