我有一个存储过程来搜索数据库中的数据,参数如下:
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上一样?
答案 0 :(得分: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传递的参数。 希望我能正确理解你的问题。
答案 1 :(得分:1)
你可以这样做:
(@PayAccountin IS NULL OR
PayAccount like '%'+ISNULL((@PayAccountin),'')+'%') AND
如果参数为null,则为true,将忽略条件
答案 2 :(得分: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
答案 3 :(得分: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