我有一个相当复杂的 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