2

我有这样的存储过程,根据我传递的搜索条件从数据库中获取值,这些值作为该过程的输入参数,但我在@StatusSelection

CREATE PROCEDURE [dbo].[tp_SelectTransactionHistorySearch]
(
   @OffSetRowNo INT,     
   @FetchRowNo INT,
   @StatusSelection NVARCHAR(MAX),
   @isReviewed Bit,
   @ProjectCaseNumber VARCHAR(MAX),
   @CostPageNumber VARCHAR(MAX),
   @TransactionTypeChange VARCHAR(MAX),
   @DescriptionChange VARCHAR(MAX),
   @TrasactionCreateOnBeginDate DATETIME,
   @TransactionCreatedOnEndDate DATETIME,
   @TransactionUpdatedOnBeginDate DATETIME,
   @TransactionUpdateOnEndDate DATETIME,
   @ItemID VARCHAR(MAX)
)
AS
 Select 
            TH.TransactionID,
            TH.IsReviewed,
            TH.ItemID,
            TH.CostPageNumber,
            TH.Comments,
            TH.CreatedBy,
            TH.CreatedDateTime,
            TH.UpdatedBy,
            TH.UpdatedDateTime,
            TH.TransactionDescription,
            TH.TransactionTypeID,
            IAccrualByItem.PROJCASE,
            IAccrualByItem.USTSTAT as TransactionStatusID,
            TStatType.Name AS TransactionStatusTypeName,
            TStatType.Description AS TransactionStatusTypeDescription,   
            TType.Name AS TransactionTypeName,
            TType.Description AS TransactionTypeDescription,
            COUNT(*) OVER () as TotalCount
        FROM TransactionHistory AS TH
        INNER JOIN TRVMVSDDVW001.INTERFACE_Files.dbo.DBATUSTA AS IAccrualByItem
            ON TH.TransactionID = CAST(IAccrualByItem.USTTRNNBR AS int)
        LEFT JOIN dbo.TransctionStatusType AS TStatType
            ON TStatType.TransactionStatusTypeID = IAccrualByItem.USTSTAT
        LEFT JOIN dbo.TransactionType AS TType
            ON TType.TransactionTypeID = CAST(TH.TransactionTypeID AS int)
        WHERE TStatType.Name = @StatusSelection AND TH.IsReviewed= @isReviewed
        AND IAccrualByItem.PROJCASE=@ProjectCaseNumber AND TH.CostPageNumber=@CostPageNumber
        AND TH.TransactionDescription=@TransactionTypeChange AND TType.Description=@DescriptionChange
        AND  (TH.CreatedDateTime >= CAST(@TrasactionCreateOnBeginDate AS DATE)) AND (TH.CreatedDateTime < CAST(@TransactionCreatedOnEndDate AS DATE))
         AND  (TH.UpdatedDateTime >= CAST( @TransactionUpdatedOnBeginDate AS DATE)) AND (TH.UpdatedDateTime < CAST(@TransactionUpdateOnEndDate AS DATE))
         @TH.StatusID= 1
GROUP BY TH.TransactionID,TH.IsReviewed,TH.ItemID,TH.CostPageNumber,TH.Comments,TH.CreatedBy,TH.CreatedDateTime,
TH.UpdatedBy,TH.UpdatedDateTime, TH.TransactionDescription, TH.TransactionTypeID,IAccrualByItem.PROJCASE,TransactionStatusID,
TStatType.Name,TStatType.Description,TType.Name,TType.Description
ORDER BY TH.TransactionID,TH.ItemID,TH.CostPageNumber
OFFSET ( @OffSetRowNo-1 ) * @FetchRowNo ROWS
FETCH NEXT @FetchRowNo ROWS ONLY

但我收到这样的错误

 `Msg 137, Level 15, State 2, Line 27
 Must declare the scalar variable "@StatusSelection"`.

我正在使用 sql server 2012 版本

任何人都可以帮助解决这个问题以及对此有任何想法.. 非常感谢.....

更新:

    DECLARE @return_value int

EXEC    @return_value = [dbo].[tp_SelectTransactionHistorySearch]
        @OffSetRowNo = 1,
        @FetchRowNo = 1,
        @StatusSelection = N's',
        @isReviewed = NULL,
        @ProjectCaseNumber = NULL,
        @CostPageNumber = NULL,
        @TransactionTypeChange = NULL,
        @DescriptionChange = NULL,
        @TrasactionCreateOnBeginDate = '10-03-2013',
        @TransactionCreatedOnEndDate = '20-03-2013',
        @TransactionUpdatedOnBeginDate = '20-05-2013',
        @TransactionUpdateOnEndDate = '04-06-2013',
        @ItemID = NULL

   SELECT   'Return Value' = @return_value

GO

得到这样的错误Msg 8114, Level 16, State 5, Procedure tp_SelectTransactionHistorySearch, Line 0 Error converting data type varchar to datetime

4

3 回答 3

2

试试这个——

CREATE PROCEDURE [dbo].[tp_SelectTransactionHistorySearch] 
(
    @OffSetRowNo INT,
    @FetchRowNo INT,
    @StatusSelection NVARCHAR(MAX),
    @isReviewed BIT,
    @ProjectCaseNumber VARCHAR(MAX),
    @CostPageNumber VARCHAR(MAX),
    @TransactionTypeChange VARCHAR(MAX),
    @DescriptionChange VARCHAR(MAX),
    @TrasactionCreateOnBeginDate DATE,
    @TransactionCreatedOnEndDate DATE,
    @TransactionUpdatedOnBeginDate DATE,
    @TransactionUpdateOnEndDate DATE,
    @ItemID VARCHAR(MAX)
)
AS
    SELECT TH.TransactionID
        ,  TH.IsReviewed
        ,  TH.ItemID
        ,  TH.CostPageNumber
        ,  TH.Comments
        ,  TH.CreatedBy
        ,  TH.CreatedDateTime
        ,  TH.UpdatedBy
        ,  TH.UpdatedDateTime
        ,  TH.TransactionDescription
        ,  TH.TransactionTypeID
        ,  IAccrualByItem.PROJCASE
        ,  IAccrualByItem.USTSTAT AS TransactionStatusID
        ,  TStatType.Name AS TransactionStatusTypeName
        ,  TStatType.[description] AS TransactionStatusTypeDescription
        ,  TType.Name AS TransactionTypeName
        ,  TType.[description] AS TransactionTypeDescription
        ,  COUNT(*) OVER () AS TotalCount
    FROM TransactionHistory AS TH
    JOIN TRVMVSDDVW001.INTERFACE_Files.dbo.DBATUSTA AS IAccrualByItem ON TH.TransactionID = CAST(IAccrualByItem.USTTRNNBR AS INT)
    LEFT JOIN dbo.TransctionStatusType AS TStatType ON TStatType.TransactionStatusTypeID= IAccrualByItem.USTSTAT
    LEFT JOIN dbo.TransactionType AS TType ON TType.TransactionTypeID = CAST(TH.TransactionTypeID AS INT)
    WHERE TStatType.Name = @StatusSelection
        AND TH.IsReviewed = @isReviewed
        AND IAccrualByItem.PROJCASE = @ProjectCaseNumber
        AND TH.CostPageNumber = @CostPageNumber
        AND TH.TransactionDescription = @TransactionTypeChange
        AND TType.[description] = @DescriptionChange
        AND TH.CreatedDateTime BETWEEN @TrasactionCreateOnBeginDate AND @TransactionCreatedOnEndDate
        AND TH.UpdatedDateTime BETWEEN @TransactionUpdatedOnBeginDate AND @TransactionUpdateOnEndDate
        AND TH.StatusID = 1
    GROUP BY
          TH.TransactionID
        , TH.IsReviewed
        , TH.ItemID
        , TH.CostPageNumber
        , TH.Comments
        , TH.CreatedBy
        , TH.CreatedDateTime
        , TH.UpdatedBy
        , TH.UpdatedDateTime
        , TH.TransactionDescription
        , TH.TransactionTypeID
        , IAccrualByItem.PROJCASE
        , TransactionStatusID
        , TStatType.Name
        , TStatType.[description]
        , TType.Name
        , TType.[description]
    ORDER BY 
          TH.TransactionID
        , TH.ItemID
        , TH.CostPageNumber
    OFFSET (@OffSetRowNo - 1) * @FetchRowNo ROWS FETCH NEXT @FetchRowNo ROWS ONLY

更新#1:

在执行查询之前设置此选项 -

SET DATEFORMAT dmy

或者,更可取的是,使用ISO格式yyyymmdd-

EXEC @return_value = [dbo].[tp_SelectTransactionHistorySearch]
    ...
    @TrasactionCreateOnBeginDate = '20130310',
    @TransactionCreatedOnEndDate = '20132003',
    @TransactionUpdatedOnBeginDate = '20130520',
    @TransactionUpdateOnEndDate = '20130604',
    @ItemID = NULL

更新#2:

DECLARE @temp TABLE
(
    string VARCHAR(10)
)

SET DATEFORMAT dmy 

INSERT INTO @temp (string)
VALUES 
    ('10-03-2013'),
    ('20-03-2013'),
    ('20-05-2013'),
    ('04-06-2013')

SELECT CAST(string AS DATE) 
FROM @temp
于 2013-08-20T06:34:10.753 回答
1

线

@TH.StatusID= 1

表示 CREATE 失败

所以你不能得到你给我们的代码显示的错误

有了这个,CREATE 工作

 AND TH.StatusID= 1
于 2013-08-20T06:50:28.173 回答
0

您是否在 side 后面的代码中传递了值?

     SqlCommand com = new SqlCommand("SpName",conectionStringObj);
    com.CommandType = CommandType.StoredProcedure;
     **com.Parameters.AddWithValue("@StatusSelection", "Any Value");**
       .
       .
       .
       .

请检查这个。

于 2013-08-20T06:30:58.173 回答