是否有可能具有允许传递除 varchar 以外的可选参数(即 DateTime、decimal ..)的搜索功能?
我需要搜索日期范围内的行。用户应该能够仅搜索 FROM 一个日期,或者最多一个日期,甚至在 FROM-TO 范围内。
我的方法是使用 Date 参数创建一个 SEARCH 方法。这些已正确转换为 CFE 中的 DateTime 对象,但我将无法传递空值。
我设法用未经检查的部分创建了一个 SEARCH 方法,但这些部分在函数体中不会是可选的。
如何将 fromDate 和 toDate 参数设置为可选?
CFQL 身体是
SEARCH(string amount, string fromDate, string toDate, string otherEntityId, date date) WHERE [$Entity::Date$ >= CAST(@fromDate AS DATE)] AND [$Entity::Date$ <= CAST(@toDate AS DATE)] AND OtherEntity.OtherEntityId = @otherEntityId AND Amount = @amount and date = @date
生成的正文过程如下。注意不是可选的未选中部分
CREATE PROCEDURE [Schema].[Entity_NameOfProcedure]
(
@amount [nvarchar] (256) = NULL,
@fromDate [nvarchar] (256) = NULL,
@toDate [nvarchar] (256) = NULL,
@OtherEntityId [nvarchar] (256) = NULL,
@date [date] = NULL,
@_orderBy0 [nvarchar] (64) = NULL,
@_orderByDirection0 [bit] = 0
)
AS
SET NOCOUNT ON
DECLARE @sql nvarchar(max), @paramlist nvarchar(max)
SELECT @sql=
'SELECT DISTINCT * /*on purpose for stackoverflow */
FROM [Schema].[Entity]
LEFT OUTER JOIN [Schema].[OtherEntity] ON ([Schema].[Entity].[Entity_OtherEntity_OtherEntityId] = [Schema].[OtherEntity].[OtherEntity_OtherEntityId])
WHERE (([Entity].[Entity_Date] >= CAST(@fromDate AS DATE) AND ([Entity].[Entity_Date] <= CAST(@toDate AS DATE) AND ((1 = 1) AND ((1 = 1) AND (([Schema].[OtherEntity].[OtherEntity_NumberInt] LIKE ''6%'') AND (1 = 1)))))) AND (1 = 1))'
SELECT @paramlist = '@amount nvarchar (256),
@fromDate nvarchar (256),
@toDate nvarchar (256),
@otherEntityId nvarchar (256),
@date date,
@_orderBy0 nvarchar (64),
@_orderByDirection0 bit'
IF @amount IS NOT NULL
SELECT @sql = @sql + ' AND (([Schema].[Entity].[Entity_amount] = @amount))'
IF @otherEntityId IS NOT NULL
SELECT @sql = @sql + ' AND (([Schema].[OtherEntity].[OtherEntity_OtherEntityId] = @OtherEntityId))'
IF @date IS NOT NULL
SELECT @sql = @sql + ' AND (([Schema].[Entity].[Entity_Date] = @date))'
EXEC sp_executesql @sql, @paramlist,
@amount,
@fromDate,
@toDate,
@OtherEntityId,
@date,
@_orderBy0,
@_orderByDirection0
RETURN
GO
感谢您的回答