0

我有一个存储过程来搜索数据库中的数据,参数如下:

CREATE PROCEDURE [dbo].[Rintis_SearchPayment]
    -- Add the parameters for the stored procedure here
    @payIDin as int,
    @PayAccountin as varchar(10),
    @PayCustNamein as varchar(30),
    @PayAmountin as int,
    @PayAmountPaidin as int,
    @PayResponsein as char (2),
    @PayRefNoin as varchar (120),
    @PayScreenTextin as varchar (100),
    @PayReceiptTextin as varchar (350),
    @PayDatetimein as datetime,
    @PayBankCodein as varchar (6),

    @payIDou as int,
    @PayAccountou as varchar(10),
    @PayCustNameou as varchar(30),
    @PayAmountou as int,
    @PayAmountPaidou as int,
    @PayResponseou as char (2),
    @PayRefNoou as varchar (120),
    @PayScreenTextou as varchar (100),
    @PayReceiptTextou as varchar (350),
    @PayDatetimeou as datetime,
    @PayBankCodeouin as varchar (6)
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    select @payIDou = PayId ,
           @PayAccountou = PayAccount, 
           @PayCustNameou = PayCustName,
           @PayAmountou = PayAmount,
           @PayAmountPaidou = PayAmountPaid,
           @PayResponseou= PayResponse,
           @PayRefNoou= PayRefNo,
           @PayScreenTextou= PayScreenText,
           @PayReceiptTextou= PayReceiptText,
           @PayDatetimeou= PayDatetime,
           @PayBankCodeouin= PayBankCode
    from Payment
    where PayId is not null AND
          PayAccount like '%'+ISNULL((@PayAccountin),'')+'%' AND
          PayCustName LIKE '%'+ISNULL((@PayAccountin),'')+'%' AND
          PayAmount LIKE '%'+ISNULL((@PayAccountin),'')+'%' AND
          PayAmountPaid LIKE '%'+ISNULL((@PayAccountin),'')+'%' AND
          PayResponse  LIKE  '%'+ISNULL((@PayAccountin),'')+'%' AND
          PayRefNo LIKE '%'+ISNULL((@PayAccountin),'')+'%' AND
          cast(PayDatetime as date) = ( select convert(date , ''+ISNULL((@PayAccountin),'')+'', 103)) order by PayDatetime DESC

    select @payIDou
    -- Insert statements for procedure here
END

但是当我执行它时,我没有给出值,因为我知道某些类似的语句是不正确的,所以它永远不会给出返回值。

起初我在我的 vb .net 上做这样的查询:

   Public Function SearchPayment(ByVal PayAccount As String, ByVal PayCustName As String, ByVal PayAmount As String, ByVal PayAmountPaid As String, ByVal PayResponse As String, ByVal PayRefNo As String, ByVal PayDatetime As String) As Boolean
        Dim strsql As String = " select top 100 *  from Payment where PayId is not null   "
        If PayAccount <> "" Then
            strsql &= "and PayAccount like '%" & PayAccount & "%'"
        End If
        If PayCustName <> "" Then
            strsql &= "and PayCustName like '%" & PayCustName & "%'"
        End If
        If PayAmount <> "" Then
            strsql &= "and PayAmount like '%" & PayAmount & "%'"
        End If
        '' add PayAmountPaid
        If PayAmountPaid <> "" Then
            strsql &= "and PayAmountPaid like '%" & PayAmountPaid & "%'"
        End If
        If PayResponse <> "" Then
            strsql &= "and PayResponse like '%" & PayResponse & "%'"
        End If
        '' add PayRefNo
        If PayRefNo <> "" Then
            strsql &= "and PayRefNo like '%" & PayRefNo & "%'"
        End If
        If PayDatetime <> "" Then
            ''select *  from Inquiry where InquiryId is not null   and cast(InquiryDate as date) = ( select convert(date ,'7/05/2013' ,   103) )
            strsql &= "and cast(PayDatetime as date) = ( select convert(date , '" & PayDatetime & "', 103)) "
        End If
        strsql &= "order by PayDatetime DESC"
        Return runQuery(strsql)

    End Function

Where是否可以像我在 vb.net 上一样通过首先检查输入值来添加参数?

4

4 回答 4

1

如果您可以更改存储过程,那么最简单的解决方案是在 WHERE 子句中添加 IS NULL 检查。像这样的东西:

 select @payIDou = PayId ,
       @PayAccountou = PayAccount, 
       @PayCustNameou = PayCustName,
       @PayAmountou = PayAmount,
       @PayAmountPaidou = PayAmountPaid,
       @PayResponseou= PayResponse,
       @PayRefNoou= PayRefNo,
       @PayScreenTextou= PayScreenText,
       @PayReceiptTextou= PayReceiptText,
       @PayDatetimeou= PayDatetime,
       @PayBankCodeouin= PayBankCode
from Payment
where PayId is not null AND
      ((PayAccount like '%'+ @PayAccountin+'%') OR @PayAccountin IS NULL) AND
      ((PayCustName LIKE '%'+ @PayAccountin+'%') OR @PayAccountin IS NULL) AND... //the rest of your WHERE clause

这将有效地忽略作为 NULL 传递的参数。希望我正确理解了你的问题。

于 2013-06-03T08:22:08.350 回答
1

你可以这样做:

(@PayAccountin IS NULL OR
 PayAccount like '%'+ISNULL((@PayAccountin),'')+'%') AND

如果参数为空,则为真并忽略条件

于 2013-06-03T08:22:18.183 回答
1

可能这对您有帮助-

CREATE PROCEDURE [dbo].[Rintis_SearchPayment]

@payIDin AS INT,
...

AS
BEGIN

    SET NOCOUNT ON;

    SELECT @payIDou = PayId ,
           @PayAccountou = PayAccount, 
           @PayCustNameou = PayCustName,
           @PayAmountou = PayAmount,
           @PayAmountPaidou = PayAmountPaid,
           @PayResponseou= PayResponse,
           @PayRefNoou= PayRefNo,
           @PayScreenTextou= PayScreenText,
           @PayReceiptTextou= PayReceiptText,
           @PayDatetimeou= PayDatetime,
           @PayBankCodeouin= PayBankCode
    FROM dbo.Payment
    WHERE PayId IS NOT NULL
        AND (
            @PayAccountin IS NULL 
            OR 
                (
                         PayAccount LIKE '%' + @PayAccountin + '%'
                    AND PayCustName LIKE '%' + @PayAccountin + '%'
                    AND PayAmount LIKE '%'+ @PayAccountin +'%'
                    AND PayAmountPaid LIKE '%' + @PayAccountin +'%' 
                    AND PayResponse  LIKE  '%'+ @PayAccountin + '%' 
                    AND PayRefNo LIKE '%'+ @PayAccountin + '%' 
                )
        )
        AND (
              @PayAccountin IS NULL 
            OR 
              CAST(PayDatetime AS DATE) = CONVERT(DATE , @PayAccountin, 103)
        )
    ORDER BY PayDatetime DESC

    SELECT @payIDou

END
于 2013-06-03T08:25:37.367 回答
0

感谢您的回复,但我找到了另一个解决方案,使用sp_Executesql并且更有效:

Create Procedure SearchPaymentDev



    @PayAccountin as varchar(10),
    @PayCustNamein as varchar(30),
    @PayAmountin as int,
    @PayAmountPaidin as int,
    @PayResponsein as char (2),
    @PayRefNoin as varchar (120),
    @PayScreenTextin as varchar (100),
    @PayReceiptTextin as varchar (350),
    @PayDatetimein as datetime,
    @PayBankCodein as varchar (6),


AS
    Set NoCount ON
    /* Variable Declaration */
    Declare @SQLQuery AS NVarchar(4000)
    Declare @ParamDefinition AS NVarchar(2000) 
    /* Build the Transact-SQL String with the input parameters */ 
    Set @SQLQuery = 'Select * From payment where PayId is not null  ' 
    /* check for the condition and build the WHERE clause accordingly */
    If @PayAccountin Is Not Null 
         Set @SQLQuery = @SQLQuery + ' And (PayAccount = @PayAccountin)'

    If @PayCustNamein Is Not Null
         Set @SQLQuery = @SQLQuery + ' And (PayCustName = @PayCustNamein)' 

    If @PayAmountin Is Not Null
         Set @SQLQuery = @SQLQuery + ' And (PayAmount = @PayAmountin)'

    If @PayAmountPaidin Is Not Null
         Set @SQLQuery = @SQLQuery + ' And (PayAmountPaid = @PayAmountPaidin)'

    --If (@StartDate Is Not Null) AND (@EndDate Is Not Null)
    --     Set @SQLQuery = @SQLQuery + ' And (JoiningDate 
    --     BETWEEN @StartDate AND @EndDate)'
    /* Specify Parameter Format for all input parameters included 
     in the stmt */
    Set @ParamDefinition =      '@PayAccountin as varchar(10),
                @PayCustNamein as varchar(30),
                @PayAmountin as int,
                @PayAmountPaidin as int'
    /* Execute the Transact-SQL String with all parameter value's 
       Using sp_executesql Command */
    Execute sp_Executesql     @SQLQuery, 
                @ParamDefinition, 
                @PayAccountin, 
                @PayCustNamein, 
                @PayAmountin, 
                @PayAmountPaidin

    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
    Return(0)

ErrorHandler:
    Return(@@ERROR)
GO
于 2013-06-03T08:46:20.353 回答