1

我有一个相当复杂的 CTE,我正试图将其合并到存储过程中。当直接从 SQL Server Management Studio 操作时,它就可以工作。当我尝试创建存储过程时,出现错误:

消息 102,级别 15,状态 1,过程 spMyCrazyProc,第 56 行

“,”附近的语法不正确。

尝试将 CTE 合并到存储过程中时,我在语法上做错了什么?

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[spMyCrazyProc]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[spMyCrazyProc]
GO

SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE dbo.spMyCrazyProc
    @CompanyId  Int,
    @EmployeeIds varchar(MAX)
AS


;with SelectedEmployees as ( 
    select * from  vwEmployee e
        where e.CompanyId = @CompanyId 
            and (@EmployeeIds is null or @EmployeeIds='' or exists(select ce.SelectedEmployeeId from #myTmpTable ce where ce.SelectedEmployeeId=e.EmployeeId)
), MyStuffA as ( 
    select * from SelectedEmployees
)
select * from MyStuffA

GO
4

1 回答 1

4

使用合理的编码约定并考虑可读性(缩进、回车)会更清楚地产生这个简单的错误。删除了 500 个字符宽的代码:

;with SelectedEmployees as 
( 
    select * from  vwEmployee e
    where e.CompanyId = @CompanyId 
    and 
    (
      @EmployeeIds is null or @EmployeeIds='' or exists
      (
         select ce.SelectedEmployeeId from #myTmpTable ce 
          where ce.SelectedEmployeeId=e.EmployeeId
      )

----^----- oops! Missing closing paren here.

), MyStuffA as 
( 
  select * from SelectedEmployees
)
select * from MyStuffA
于 2013-09-06T20:21:53.427 回答