Stored Procedure:
CREATE PROCEDURE [dbo].[Select_Payments]
@PaymentID as int,
@PaymentCode as int,
@PaymentDtTmStart as datetime,
@PaymentDtTmEnd as datetime,
@PaymentNumber as nvarchar(50),
@PaymentAmt as decimal,
@DepositDtTmStart as datetime,
@DepositDtTmEnd as datetime,
@LedgerDtTmStart as datetime,
@LedgerDtTmEnd as datetime,
@Void as tinyint,
@ReceiptID as int
AS
SELECT * FROM Payments
WHERE
((@PaymentID IS NULL)
OR (@PaymentID = 0)
OR (PaymentID = @PaymentID))
AND ((@PaymentCode IS NULL)
OR (@PaymentCode = 0)
OR (PaymentCode = @PaymentCode))
AND (PaymentDtTm BETWEEN ISNULL(@PaymentDtTmStart,'1/1/1900') AND ISNULL(@PaymentDtTmEnd,GETDATE()))
AND ((@PaymentNumber IS NULL)
OR (@PaymentNumber = '*')
OR (PaymentNumber = @PaymentNumber))
AND ((@PaymentAmt IS NULL)
OR (@PaymentAmt = 0)
OR (PaymentAmt = @PaymentAmt))
AND (DepositDtTm BETWEEN ISNULL(@DepositDtTmStart, '1/1/1900') AND ISNULL(@DepositDtTmEnd,GETDATE()))
AND (LedgerDtTm BETWEEN ISNULL(@LedgerDtTmStart, '1/1/1900') AND ISNULL(@LedgerDtTmEnd,GETDATE()))
AND ((@Void IS NULL)
OR (@Void = 0)
OR (Void = @Void))
AND ((@ReceiptID IS NULL)
OR (@ReceiptID = 0)
OR (ReceiptID = @ReceiptID))
AND (@DeletedDtTm IS NULL)
GO
Test code:
DECLARE @PaymentID as int
DECLARE @PaymentCode as int
DECLARE @PaymentDtTmStart as datetime
DECLARE @PaymentDtTmEnd as datetime
DECLARE @PaymentNumber as nvarchar(50)
DECLARE @PaymentAmt as decimal
DECLARE @DepositDtTmStart as datetime
DECLARE @DepositDtTmEnd as datetime
DECLARE @LedgerDtTmStart as datetime
DECLARE @LedgerDtTmEnd as datetime
DECLARE @Void as tinyint
DECLARE @ReceiptID as int
DECLARE @DeletedDtTm as datetime
SET @PaymentID = NULL
SET @PaymentCode = NULL
SET @PaymentDtTmStart = NULL
SET @PaymentDtTmEnd = NULL
SET @PaymentNumber = NULL
SET @PaymentAmt = NULL
SET @DepositDtTmStart = '1/5/2004 12:00:00 AM'
SET @DepositDtTmEnd = '2/4/2004 12:32:52 PM'
SET @LedgerDtTmStart = NULL
SET @LedgerDtTmEnd = NULL
SET @Void = NULL
SET @ReceiptID = NULL
SET @DeletedDtTm = NULL
SELECT * FROM ptPayments
WHERE
((@PaymentID IS NULL)
OR (@PaymentID = 0)
OR (PaymentID = @PaymentID))
AND ((@PaymentCode IS NULL)
OR (@PaymentCode = 0)
OR (PaymentCode = @PaymentCode))
AND (PaymentDtTm BETWEEN ISNULL(@PaymentDtTmStart,'1/1/1900') AND ISNULL(@PaymentDtTmEnd,GETDATE()))
AND ((@PaymentNumber IS NULL)
OR (@PaymentNumber = '*')
OR (PaymentNumber = @PaymentNumber))
AND ((@PaymentAmt IS NULL)
OR (@PaymentAmt = 0)
OR (PaymentAmt = @PaymentAmt))
AND (DepositDtTm BETWEEN ISNULL(@DepositDtTmStart, '1/1/1900') AND ISNULL(@DepositDtTmEnd,GETDATE()))
AND (LedgerDtTm BETWEEN ISNULL(@LedgerDtTmStart, '1/1/1900') AND ISNULL(@LedgerDtTmEnd,GETDATE()))
AND ((@Void IS NULL)
OR (@Void = 0)
OR (Void = @Void))
AND ((@ReceiptID IS NULL)
OR (@ReceiptID = 0)
OR (ReceiptID = @ReceiptID))
AND (@DeletedDtTm IS NULL)
GO
ALTER PROCEDURE [dbo].[Select_Payments]
@PaymentID as int,
@PaymentCode as int,
@PaymentDtTmStart as datetime,
@PaymentDtTmEnd as datetime,
@PaymentNumber as nvarchar(50),
@PaymentAmt as decimal,
@DepositDtTmStart as datetime,
@DepositDtTmEnd as datetime,
@LedgerDtTmStart as datetime,
@LedgerDtTmEnd as datetime,
@Void as tinyint,
@ReceiptID as int
AS
declare @sql varchar(max)
set @sql = 'SELECT * FROM Payments
WHERE (DeletedDtTm IS NULL)'
+ case when isnull(@paymentid,0) > 0 then ' and (PaymentID = @PaymentID) ' else '' end
+ case when isnull(@paymentCode,0) > 0 then ' and (PaymentCode = @PaymentCode) ' else '' end
+ case when isnull(@paymentDtTmStart,'01/01/1900') > '01/01/1900' then ' and (PaymentDtTm >= @PaymentDtTmStart) ' else '' end
+ case when isnull(@paymentDtTmEnd,'01/01/1900') > '01/01/1900' then ' and (PaymentDtTm <= @PaymentDtTmEnd) ' else '' end
+ case when isnull(@paymentNumber,'*') <> '*' and len(rtrim(@paymentNumber)) > 0 then ' and (PaymentNumber = @PaymentNumber) ' else '' end
+ case when isnull(@paymentAmt,0) > 0 then ' and (PaymentCode = @PaymentAmt) ' else '' end
+ case when isnull(@depositDtTmStart,'01/01/1900') > '01/01/1900' then ' and (depositDtTm >= @depositDtTmStart) ' else '' end
+ case when isnull(@depositDtTmEnd,'01/01/1900') > '01/01/1900' then ' and (depositDtTm <= @depositDtTmEnd) ' else '' end
+ case when isnull(@ledgerDtTmStart,'01/01/1900') > '01/01/1900' then ' and (ledgerDtTm >= @ledgerDtTmStart) ' else '' end
+ case when isnull(@ledgerDtTmEnd,'01/01/1900') > '01/01/1900' then ' and (ledgerDtTm <= @ledgerDtTmEnd) ' else '' end
+ case when isnull(@void,0) > 0 then ' and (PaymentCode = @void) ' else '' end
+ case when isnull(@receiptid,0) > 0 then ' and (PaymentCode = @receiptid) ' else '' end
print @sql
--exec(@sql)
GO
ASKER
DECLARE @PaymentID as int
DECLARE @PaymentCode as int
DECLARE @PaymentDtTmStart as datetime
DECLARE @PaymentDtTmEnd as datetime
DECLARE @PaymentNumber nvarchar(50)
DECLARE @PaymentAmt as decimal(18,2)
DECLARE @DepositDtTmStart as datetime
DECLARE @DepositDtTmEnd as datetime
DECLARE @LedgerDtTmStart as datetime
DECLARE @LedgerDtTmEnd as datetime
DECLARE @Void as int
DECLARE @ReceiptID as int
----------------------------------------
-- Set scalar values based on table data
----------------------------------------
SET @PaymentID = 6437941
SET @PaymentCode = NULL
SET @PaymentDtTmStart = NULL
SET @PaymentDtTmEnd = NULL
SET @PaymentNumber = NULL
SET @PaymentAmt = NULL
SET @DepositDtTmStart = NULL
SET @DepositDtTmEnd = NULL
SET @LedgerDtTmStart = NULL
SET @LedgerDtTmEnd = NULL
SET @Void = NULL
SET @ReceiptID = NULL
exec Select_Payments @PaymentID, @PaymentCode, @PaymentDtTmStart, @PaymentDtTmEnd, @PaymentNumber, @PaymentAmt, @DepositDtTmStart, @DepositDtTmEnd, @LedgerDtTmStart, @LedgerDtTmEnd, @Void, @ReceiptID
-- Even tried this way, Same error
--exec Select_Payments 6437941,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL
-- This is the SQL printed:
SELECT * FROM ptPayments
WHERE (DeletedDtTm IS NULL) AND (PaymentID = @PaymentID)
ALTER PROCEDURE [dbo].[Select_Payments]
@PaymentID as int,
@PaymentCode as int,
@PaymentDtTmStart as datetime,
@PaymentDtTmEnd as datetime,
@PaymentNumber as nvarchar(50),
@PaymentAmt as decimal,
@DepositDtTmStart as datetime,
@DepositDtTmEnd as datetime,
@LedgerDtTmStart as datetime,
@LedgerDtTmEnd as datetime,
@Void as tinyint,
@ReceiptID as int
AS
declare @sql varchar(max)
set @sql = 'SELECT * FROM Payments
WHERE (DeletedDtTm IS NULL)'
+ case when isnull(@paymentid,0) > 0 then ' and (PaymentID = '+convert(varchar(10),@PaymentID)+') ' else '' end
+ case when isnull(@paymentCode,0) > 0 then ' and (PaymentCode = '+convert(varchar(10),@PaymentCode)+') ' else '' end
+ case when isnull(@paymentDtTmStart,'01/01/1900') > '01/01/1900' then ' and (PaymentDtTm >= '''+convert(varchar(20),@PaymentDtTmStart,112)+''') ' else '' end
+ case when isnull(@paymentDtTmEnd,'01/01/1900') > '01/01/1900' then ' and (PaymentDtTm <= '''+convert(varchar(20),@PaymentDtTmEnd,112)+''') ' else '' end
+ case when isnull(@paymentNumber,'*') <> '*' and len(rtrim(@paymentNumber)) > 0 then ' and (PaymentNumber = '+convert(varchar(10),@PaymentNumber)+') ' else '' end
+ case when isnull(@paymentAmt,0) > 0 then ' and (PaymentCode = '+convert(varchar(10),@PaymentAmt)+') ' else '' end
+ case when isnull(@depositDtTmStart,'01/01/1900') > '01/01/1900' then ' and (depositDtTm >= '''+convert(varchar(20),@depositDtTmStart,112)+''') ' else '' end
+ case when isnull(@depositDtTmEnd,'01/01/1900') > '01/01/1900' then ' and (depositDtTm <= '''+convert(varchar(20),@depositDtTmEnd,112)+''') ' else '' end
+ case when isnull(@ledgerDtTmStart,'01/01/1900') > '01/01/1900' then ' and (ledgerDtTm >= '''+convert(varchar(20),@ledgerDtTmStart,112)+''') ' else '' end
+ case when isnull(@ledgerDtTmEnd,'01/01/1900') > '01/01/1900' then ' and (ledgerDtTm <= '''+convert(varchar(20),@ledgerDtTmEnd,112)+''') ' else '' end
+ case when isnull(@void,0) > 0 then ' and (PaymentCode = '+convert(varchar(10),@void)+') ' else '' end
+ case when isnull(@receiptid,0) > 0 then ' and (PaymentCode = '+convert(varchar(10),@receiptid)+') ' else '' end
print @sql
--exec(@sql)
GO
ASKER
ASKER
Microsoft SQL Server 2005 is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning. It includes support for managing XML data and allows a database server to be exposed over web services using Tabular Data Stream (TDS) packets encapsulated within SOAP (protocol) requests.
TRUSTED BY
ASKER
Open in new window