存储过程中的转换表达式

时间:2015-06-30 07:47:49

标签: sql sql-server tsql stored-procedures

我是Transact-Sql的新手,我创建了这个存储过程:

CREATE PROCEDURE SP_Passed_Time_Day
    -- Add the parameters for the stored procedure here
    @LaDate date ,
    @LeCollaborateur int null,
    @LEquipe int null 
AS
    SELECT
        [dbo].[Pointage].[Id], [dbo].[Pointage].[User],
        [dbo].[Pointage].[Type], [dbo].[Pointage].[PointageDateTime] 
    FROM
        [dbo].[Pointage], [dbo].[Collaborateur]
    WHERE
        [dbo].[Pointage].[User] = [dbo].[Collaborateur].[User] 
        AND CONVERT(VARCHAR(10), [dbo].[Pointage].PointageDateTime, 103) = CONVERT(VARCHAR(10), @LaDate, 103)
        AND CAST(
           CASE WHEN @LEquipe = null and @LeCollaborateur != null 
                   THEN @LeCollaborateur = [dbo].[Collaborateur].[User] 
           CASE WHEN @LEquipe != null THEN @LEquipe = [dbo].[Collaborateur].id_equipe_fk) 
    GO

我在Cast case表达式中遇到语法错误。我该如何修复这个脚本?

2 个答案:

答案 0 :(得分:3)

应该是这样的:

case  when @LEquipe is null and @LeCollaborateur is not null then @LeCollaborateur = [Collaborateur].[User] 
      when  @LEquipe is not null  then @LEquipe =[Collaborateur].id_equipe_fk end

使用此代码:

 CREATE PROCEDURE SP_Passed_Time_Day
    -- Add the parameters for the stored procedure here
    @LaDate date ,
    @LeCollaborateur int null,
    @LEquipe int null 
AS
    select [dbo].[Pointage].[Id],[dbo].[Pointage].[User],[dbo].[Pointage].[Type],[dbo].[Pointage].[PointageDateTime] 
          from  [dbo].[Pointage], [dbo].[Collaborateur]
          where [dbo].[Pointage].[User] = [dbo].[Collaborateur].[User] 
          and  CONVERT(VARCHAR(10), [dbo].[Pointage].PointageDateTime, 103) = CONVERT(VARCHAR(10), @LaDate, 103)
          and case  when @LEquipe = null and @LeCollaborateur != null then @LeCollaborateur = [dbo].[Collaborateur].[User] 
                    when  @LEquipe != null   then @LEquipe = [dbo].[Collaborateur].id_equipe_fk end

    GO

答案 1 :(得分:1)

试试这个:

case  when @LEquipe is null and @LeCollaborateur is not null then @LeCollaborateur = [Collaborateur].[User] 
          case when  @LEquipe is not null  then @LEquipe =[Collaborateur].id_equipe_fk end