sp_executesql存储过程给出错误并且没有意义

时间:2015-10-29 10:32:34

标签: sql sql-server tsql

我有以下存储过程:

[ChildActionOnly]
public PartialViewResult _RequestPartial()
{
      ... code that populates model you want to pass ...

      return PartialView(model);
}

[ChildActionOnly]
public PartialViewResult _LoginPartial()
{
      ... code that populates model you want to pass ...

      return PartialView(model);
}

现在,当我在SQL Server Management Studio中执行此存储过程时:

CREATE PROCEDURE [Students].[spExamsSchedulesRead]
    @ActionCode             nVarchar(10),
    @UserID                 BigInt,     
    @SearchText             nVarchar(MAX),
    @ExamScheduleID         nVarchar(50),
    @ExamTypeID             nVarchar(50),
    @ClassSectionID         nVarchar(50),
    @SessionID              nVarchar(50),   
    @SortExp                NVARCHAR(100),
    @SortDir                nVarchar(20)
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @Err_Message nVarChar(Max)
    DECLARE @FullSelect Nvarchar(MAX)   
    DECLARE @WhereClause Nvarchar(MAX)  

    DECLARE @tTable TABLE
        (   
           ExamScheduleID [bigint] PRIMARY KEY NOT NULL,                        
           ExamTypeID           [bigint],
           ClassSectionID       [bigint],
           ClassID              [bigint],
           SectionID            [bigint],
           SessionID            [bigint],         
           ExamTypeEN           [nVarchar](100),
           ExamType             [nVarchar](100),
           SessionEN            [nVarchar](100),
           [Session]            [nVarchar](100),
           SessionActive        [Bit],         
           ClassNameEN          [nVarchar](100),
           ClassName            [nVarchar](100),
           SectionNameEN        [nVarchar](100),
           SectionName          [nVarchar](100),
           ClassSectionActive   [Bit],
           DateFrom             [DateTime],
           DateTo               [DateTime],
           InstructionsEN       [nVarchar](max),
           Instructions         [nVarchar](max),
           Remarks              [nVarchar](max),
           Active               [bit],
           AddedBy              [bigint],
           AddedByUsername      [nVarchar](150),
           DateAdded            [datetime],
           LastModifiedBy       [bigint],
           LastModifiedByUsername [nVarchar](150),
           DateModified         [datetime]          
          )

    ---------------------------------------------------------
    SET @FullSelect =  'SELECT  * FROM [Students].[vExamsSchedules] '       
    SET @WhereClause =  ' WHERE ExamScheduleID <> 0 '
    ---------------------------------------------------------
    IF (ISNULL(@ExamScheduleID,0) <> 0)
        BEGIN
            SET @WhereClause= @WhereClause +' AND (ExamScheduleID = ' + @ExamScheduleID + ') '
        END

    IF (ISNULL(@ExamTypeID,'') <> '')
        BEGIN
            SET @WhereClause= @WhereClause +' AND (ExamTypeID IN (' + @ExamTypeID + ')) '
        END

    IF (ISNULL(@ClassSectionID,'') <> '')
        BEGIN
            SET @WhereClause= @WhereClause +' AND (ClassSectionID IN (' + @ClassSectionID + ')) '
        END

    IF (ISNULL(@SessionID,'') <> '')
        BEGIN
            SET @WhereClause= @WhereClause +' AND (SessionID IN (' + @SessionID + ')) '
        END 

    IF ISNULL(@SearchText, '') <> ''
        BEGIN
            SET @SearchText = '%' + @SearchText + '%'
            SET @WhereClause= @WhereClause + ' AND (ISNULL(ExamTypeEN,'''') + ISNULL(ExamType,'''') + ISNULL(SessionEN,'''') + 
                              ISNULL([Session],'''') + ISNULL(ClassNameEN,'''') + ISNULL(ClassName,'''') + 
                              ISNULL(SectionNameEN,'''') + ISNULL(SectionName,'''') + ISNULL(InstructionsEN,'''') + 
                              ISNULL(Instructions,'''') + ISNULL(Remarks,'''') + ISNULL(AddedByUsername,'''') + 
                              ISNULL(LastModifiedByUsername,'''') LIKE ''' + @SearchText + ''')'

        END

    SET @FullSelect =@FullSelect + @WhereClause 
    INSERT INTO @tTable
    EXEC sp_executesql @FullSelect
    --PRINT @FullSelect     

    BEGIN TRY
        BEGIN TRANSACTION

            IF @ActionCode ='1' --Read Data to display in ListView
                BEGIN               
                    SELECT * FROM @tTable ORDER BY 
                    CASE WHEN @SortExp = 'ExamTypeEN' AND @SortDir = 'ASC' THEN ExamTypeEN END ASC, 
                    CASE WHEN @SortExp = 'ExamTypeEN' AND @SortDir = 'DESC' THEN ExamTypeEN END DESC,
                    CASE WHEN @SortExp = 'ExamType' AND @SortDir = 'ASC' THEN ExamType END ASC, 
                    CASE WHEN @SortExp = 'ExamType' AND @SortDir = 'DESC' THEN ExamType END DESC,                   
                    CASE WHEN @SortExp = 'SessionEN' AND @SortDir = 'ASC' THEN SessionEN END ASC, 
                    CASE WHEN @SortExp = 'SessionEN' AND @SortDir = 'DESC' THEN SessionEN END DESC,
                    CASE WHEN @SortExp = 'Session' AND @SortDir = 'ASC' THEN [Session] END ASC, 
                    CASE WHEN @SortExp = 'Session' AND @SortDir = 'DESC' THEN [Session] END DESC,
                    CASE WHEN @SortExp = 'ClassNameEN' AND @SortDir = 'ASC' THEN ClassNameEN END ASC, 
                    CASE WHEN @SortExp = 'ClassNameEN' AND @SortDir = 'DESC' THEN ClassNameEN END DESC,
                    CASE WHEN @SortExp = 'ClassName' AND @SortDir = 'ASC' THEN ClassName END ASC, 
                    CASE WHEN @SortExp = 'ClassName' AND @SortDir = 'DESC'  THEN ClassName END DESC,
                    CASE WHEN @SortExp = 'SectionNameEN' AND @SortDir = 'ASC' THEN SectionNameEN END ASC,
                    CASE WHEN @SortExp = 'SectionNameEN' AND @SortDir = 'DESC' THEN SectionNameEN END DESC,
                    CASE WHEN @SortExp = 'SectionName' AND @SortDir = 'ASC' THEN SectionName END ASC, 
                    CASE WHEN @SortExp = 'SectionName' AND @SortDir = 'DESC'    THEN SectionName END DESC                   
                END

            ELSE IF @ActionCode ='1.1' --Read Data to display in ListView
                BEGIN               
                    SELECT * FROM @tTable WHERE SessionActive='True' AND ClassSectionActive='True' AND Active='True' ORDER BY
                    CASE WHEN @SortExp = 'ExamTypeEN' AND @SortDir = 'ASC' THEN ExamTypeEN END ASC, 
                    CASE WHEN @SortExp = 'ExamTypeEN' AND @SortDir = 'DESC' THEN ExamTypeEN END DESC,
                    CASE WHEN @SortExp = 'ExamType' AND @SortDir = 'ASC' THEN ExamType END ASC, 
                    CASE WHEN @SortExp = 'ExamType' AND @SortDir = 'DESC' THEN ExamType END DESC,                   
                    CASE WHEN @SortExp = 'SessionEN' AND @SortDir = 'ASC' THEN SessionEN END ASC, 
                    CASE WHEN @SortExp = 'SessionEN' AND @SortDir = 'DESC' THEN SessionEN END DESC,
                    CASE WHEN @SortExp = 'Session' AND @SortDir = 'ASC' THEN [Session] END ASC, 
                    CASE WHEN @SortExp = 'Session' AND @SortDir = 'DESC' THEN [Session] END DESC,
                    CASE WHEN @SortExp = 'ClassNameEN' AND @SortDir = 'ASC' THEN ClassNameEN END ASC, 
                    CASE WHEN @SortExp = 'ClassNameEN' AND @SortDir = 'DESC' THEN ClassNameEN END DESC,
                    CASE WHEN @SortExp = 'ClassName' AND @SortDir = 'ASC' THEN ClassName END ASC, 
                    CASE WHEN @SortExp = 'ClassName' AND @SortDir = 'DESC'  THEN ClassName END DESC,
                    CASE WHEN @SortExp = 'SectionNameEN' AND @SortDir = 'ASC' THEN SectionNameEN END ASC,
                    CASE WHEN @SortExp = 'SectionNameEN' AND @SortDir = 'DESC' THEN SectionNameEN END DESC,
                    CASE WHEN @SortExp = 'SectionName' AND @SortDir = 'ASC' THEN SectionName END ASC, 
                    CASE WHEN @SortExp = 'SectionName' AND @SortDir = 'DESC'    THEN SectionName END DESC                                   
                END

            ELSE IF @ActionCode ='2'
                BEGIN
                    SELECT * FROM @tTable WHERE ExamScheduleID=@ExamScheduleID
                END 


        COMMIT TRANSACTION
        RETURN @@ROWCOUNT

    END TRY
    BEGIN CATCH
        ROLLBACK TRANSACTION
        INSERT INTO [Security].ErrorLogs(ErrorDate, ErrorBy, ErrorNo, ErrorLine, ErrorState,
                    ErrorSeverity, ErrorProcedure, ErrorMessage) VALUES
                    ([Security].fnUTCDateTime(@UserID),@UserID, 
                     CAST(ERROR_NUMBER() AS nVarchar(20)),
                     CAST(ERROR_LINE() AS nVarchar(20)),
                     CAST(ERROR_STATE() AS nVarchar(20)),
                     CAST(ERROR_SEVERITY() AS nVarchar(20)),
                     CAST(ERROR_PROCEDURE() AS nVarchar(150)),
                     CAST(ERROR_MESSAGE() AS nVarchar(max)))
        RAISERROR (@Err_Message,11, 1)
        RETURN -1
    END CATCH
END

它返回以下错误:

  

Msg 8114,Level 16,State 1,Line 3
  将数据类型nvarchar转换为datetime时出错。

当我调试存储过程时,它会到达

DECLARE @return_value int

EXEC @return_value = [Students].[spExamsSchedulesRead]
     @ActionCode = N'1',
     @UserID = 1,
     @SearchText = NULL,
     @ExamScheduleID = NULL,
     @ExamTypeID = NULL,
     @ClassSectionID = NULL,
     @SessionID = NULL,
     @SortExp = NULL,
     @SortDir = NULL

SELECT 'Return Value' = @return_value
GO

然后它退出调试...并显示SQL查询....这实际上是正确的....因为当我单独复制并运行该SQL查询时正在运行...

请有人可以帮助解决此错误的原因吗?虽然我删除了视图和存储过程,但是使用相同的名称甚至使用不同的名称再次创建它们,我得到同样的错误。不要忘记我的所有其他表都有相同类型的存储过程,并且运行正常。只有这一个导致错误。

3 个答案:

答案 0 :(得分:0)

似乎您收到错误并且catch块中的插入正在给出错误:&#34; Msg 8114,Level 16,State 1,Line 3将数据类型nvarchar转换为datetime&#34时出错。 错误似乎即将到来,因为您的ActionCode为1,并且没有任何内容附加到OrderBy子句。

答案 1 :(得分:0)

这有用吗?我确保@FullSelect行语句与表变量顺序匹配。它解析,但我没有数据..

CREATE PROCEDURE [Students].[spExamsSchedulesRead]
    @ActionCode             nVarchar(10),
    @UserID                 BigInt,     
    @SearchText             nVarchar(MAX),
    @ExamScheduleID         nVarchar(50),
    @ExamTypeID             nVarchar(50),
    @ClassSectionID         nVarchar(50),
    @SessionID              nVarchar(50),   
    @SortExp                NVARCHAR(100),
    @SortDir                nVarchar(20)
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @Err_Message nVarChar(Max)
    DECLARE @FullSelect Nvarchar(MAX)   
    DECLARE @WhereClause Nvarchar(MAX)  

    DECLARE @tTable TABLE
        (   
           ExamScheduleID [bigint] PRIMARY KEY NOT NULL,                        
           ExamTypeID           [bigint],
           ClassSectionID       [bigint],
           ClassID              [bigint],
           SectionID            [bigint],
           SessionID            [bigint],         
           ExamTypeEN           [nVarchar](100),
           ExamType             [nVarchar](100),
           SessionEN            [nVarchar](100),
           [Session]            [nVarchar](100),
           SessionActive        [Bit],         
           ClassNameEN          [nVarchar](100),
           ClassName            [nVarchar](100),
           SectionNameEN        [nVarchar](100),
           SectionName          [nVarchar](100),
           ClassSectionActive   [Bit],
           DateFrom             [DateTime],
           DateTo               [DateTime],
           InstructionsEN       [nVarchar](max),
           Instructions         [nVarchar](max),
           Remarks              [nVarchar](max),
           Active               [bit],
           AddedBy              [bigint],
           AddedByUsername      [nVarchar](150),
           DateAdded            [datetime],
           LastModifiedBy       [bigint],
           LastModifiedByUsername [nVarchar](150),
           DateModified         [datetime]          
          )

    ---------------------------------------------------------
    SET @FullSelect =  'SELECT  ExamScheduleID,
                                ExamTypeID,
                                ClassSectionID,
                                ClassID,
                                SectionID,
                                SessionID,
                                ExamTypeEN,
                                ExamType,
                                SessionEN,
                                [Session],
                                SessionActive,
                                ClassNameEN,
                                ClassName,
                                SectionNameEN,
                                SectionName,
                                ClassSectionActive,
                                DateFrom,
                                DateTo,
                                InstructionsEN,
                                Instructions,
                                Remarks,
                                Active,
                                AddedBy,
                                AddedByUsername,
                                DateAdded,
                                LastModifiedBy,
                                LastModifiedByUsername,
                                DateModified FROM [Students].[vExamsSchedules] '       
    SET @WhereClause =  ' WHERE ExamScheduleID <> 0 '
    ---------------------------------------------------------
    IF (ISNULL(@ExamScheduleID,0) <> 0)
        BEGIN
            SET @WhereClause= @WhereClause +' AND (ExamScheduleID = ' + @ExamScheduleID + ') '
        END

    IF (ISNULL(@ExamTypeID,'') <> '')
        BEGIN
            SET @WhereClause= @WhereClause +' AND (ExamTypeID IN (' + @ExamTypeID + ')) '
        END

    IF (ISNULL(@ClassSectionID,'') <> '')
        BEGIN
            SET @WhereClause= @WhereClause +' AND (ClassSectionID IN (' + @ClassSectionID + ')) '
        END

    IF (ISNULL(@SessionID,'') <> '')
        BEGIN
            SET @WhereClause= @WhereClause +' AND (SessionID IN (' + @SessionID + ')) '
        END 

    IF ISNULL(@SearchText, '') <> ''
        BEGIN
            SET @SearchText = '%' + @SearchText + '%'
            SET @WhereClause= @WhereClause + ' AND (ISNULL(ExamTypeEN,'''') + ISNULL(ExamType,'''') + ISNULL(SessionEN,'''') + 
                              ISNULL([Session],'''') + ISNULL(ClassNameEN,'''') + ISNULL(ClassName,'''') + 
                              ISNULL(SectionNameEN,'''') + ISNULL(SectionName,'''') + ISNULL(InstructionsEN,'''') + 
                              ISNULL(Instructions,'''') + ISNULL(Remarks,'''') + ISNULL(AddedByUsername,'''') + 
                              ISNULL(LastModifiedByUsername,'''') LIKE ''' + @SearchText + ''')'

        END

    SET @FullSelect =@FullSelect + @WhereClause 
    INSERT INTO @tTable
    EXEC sp_executesql @FullSelect
    --PRINT @FullSelect     

    BEGIN TRY
        BEGIN TRANSACTION

            IF @ActionCode ='1' --Read Data to display in ListView
                BEGIN               
                    SELECT * FROM @tTable ORDER BY 
                    CASE WHEN @SortExp = 'ExamTypeEN' AND @SortDir = 'ASC' THEN ExamTypeEN END ASC, 
                    CASE WHEN @SortExp = 'ExamTypeEN' AND @SortDir = 'DESC' THEN ExamTypeEN END DESC,
                    CASE WHEN @SortExp = 'ExamType' AND @SortDir = 'ASC' THEN ExamType END ASC, 
                    CASE WHEN @SortExp = 'ExamType' AND @SortDir = 'DESC' THEN ExamType END DESC,                   
                    CASE WHEN @SortExp = 'SessionEN' AND @SortDir = 'ASC' THEN SessionEN END ASC, 
                    CASE WHEN @SortExp = 'SessionEN' AND @SortDir = 'DESC' THEN SessionEN END DESC,
                    CASE WHEN @SortExp = 'Session' AND @SortDir = 'ASC' THEN [Session] END ASC, 
                    CASE WHEN @SortExp = 'Session' AND @SortDir = 'DESC' THEN [Session] END DESC,
                    CASE WHEN @SortExp = 'ClassNameEN' AND @SortDir = 'ASC' THEN ClassNameEN END ASC, 
                    CASE WHEN @SortExp = 'ClassNameEN' AND @SortDir = 'DESC' THEN ClassNameEN END DESC,
                    CASE WHEN @SortExp = 'ClassName' AND @SortDir = 'ASC' THEN ClassName END ASC, 
                    CASE WHEN @SortExp = 'ClassName' AND @SortDir = 'DESC'  THEN ClassName END DESC,
                    CASE WHEN @SortExp = 'SectionNameEN' AND @SortDir = 'ASC' THEN SectionNameEN END ASC,
                    CASE WHEN @SortExp = 'SectionNameEN' AND @SortDir = 'DESC' THEN SectionNameEN END DESC,
                    CASE WHEN @SortExp = 'SectionName' AND @SortDir = 'ASC' THEN SectionName END ASC, 
                    CASE WHEN @SortExp = 'SectionName' AND @SortDir = 'DESC'    THEN SectionName END DESC                   
                END

            ELSE IF @ActionCode ='1.1' --Read Data to display in ListView
                BEGIN               
                    SELECT * FROM @tTable WHERE SessionActive='True' AND ClassSectionActive='True' AND Active='True' ORDER BY
                    CASE WHEN @SortExp = 'ExamTypeEN' AND @SortDir = 'ASC' THEN ExamTypeEN END ASC, 
                    CASE WHEN @SortExp = 'ExamTypeEN' AND @SortDir = 'DESC' THEN ExamTypeEN END DESC,
                    CASE WHEN @SortExp = 'ExamType' AND @SortDir = 'ASC' THEN ExamType END ASC, 
                    CASE WHEN @SortExp = 'ExamType' AND @SortDir = 'DESC' THEN ExamType END DESC,                   
                    CASE WHEN @SortExp = 'SessionEN' AND @SortDir = 'ASC' THEN SessionEN END ASC, 
                    CASE WHEN @SortExp = 'SessionEN' AND @SortDir = 'DESC' THEN SessionEN END DESC,
                    CASE WHEN @SortExp = 'Session' AND @SortDir = 'ASC' THEN [Session] END ASC, 
                    CASE WHEN @SortExp = 'Session' AND @SortDir = 'DESC' THEN [Session] END DESC,
                    CASE WHEN @SortExp = 'ClassNameEN' AND @SortDir = 'ASC' THEN ClassNameEN END ASC, 
                    CASE WHEN @SortExp = 'ClassNameEN' AND @SortDir = 'DESC' THEN ClassNameEN END DESC,
                    CASE WHEN @SortExp = 'ClassName' AND @SortDir = 'ASC' THEN ClassName END ASC, 
                    CASE WHEN @SortExp = 'ClassName' AND @SortDir = 'DESC'  THEN ClassName END DESC,
                    CASE WHEN @SortExp = 'SectionNameEN' AND @SortDir = 'ASC' THEN SectionNameEN END ASC,
                    CASE WHEN @SortExp = 'SectionNameEN' AND @SortDir = 'DESC' THEN SectionNameEN END DESC,
                    CASE WHEN @SortExp = 'SectionName' AND @SortDir = 'ASC' THEN SectionName END ASC, 
                    CASE WHEN @SortExp = 'SectionName' AND @SortDir = 'DESC'    THEN SectionName END DESC                                   
                END

            ELSE IF @ActionCode ='2'
                BEGIN
                    SELECT * FROM @tTable WHERE ExamScheduleID=@ExamScheduleID
                END 


        COMMIT TRANSACTION
        RETURN @@ROWCOUNT

    END TRY
    BEGIN CATCH
        ROLLBACK TRANSACTION
        INSERT INTO [Security].ErrorLogs(ErrorDate, ErrorBy, ErrorNo, ErrorLine, ErrorState,
                    ErrorSeverity, ErrorProcedure, ErrorMessage) VALUES
                    ([Security].fnUTCDateTime(@UserID),@UserID, 
                     CAST(ERROR_NUMBER() AS nVarchar(20)),
                     CAST(ERROR_LINE() AS nVarchar(20)),
                     CAST(ERROR_STATE() AS nVarchar(20)),
                     CAST(ERROR_SEVERITY() AS nVarchar(20)),
                     CAST(ERROR_PROCEDURE() AS nVarchar(150)),
                     CAST(ERROR_MESSAGE() AS nVarchar(max)))
        RAISERROR (@Err_Message,11, 1)
        RETURN -1
    END CATCH
END

答案 2 :(得分:0)

谢谢大家回答............终于经过几个小时的头痛......我遇到了问题....特别感谢Nick.McDermaid ........ 。我删除了所有列并开始逐个添加列....并猜测是什么....在视图中我的最后一列是在这个序列中。

&#13;
&#13;
AddedBy    [bigint],
AddedByUsername      [nVarchar](150),
DateAdded            [datetime],
&#13;
&#13;
&#13;

我在这个序列中添加了它们。

&#13;
&#13;
AddedBy    [bigint],
DateAdded            [datetime],
AddedByUsername      [nVarchar](150),
&#13;
&#13;
&#13;