1

是否有可能具有允许传递除 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

感谢您的回答

4

1 回答 1

1

您可以创建一个SEARCH带有可选日期参数的方法:

SEARCH(date fromDate, date toDate)
WHERE CreationDate >= @fromDate AND CreationDate <= @toDate

这会生成以下 SQL:

SELECT @sql=
'SELECT DISTINCT [Employee_Id], [Employee_FirstName], [Employee_CreationDate] 
    FROM [Employee]
    WHERE (1 = 1)'
SELECT @paramlist = '@fromDate datetime, @toDate datetime'
IF @fromDate IS NOT NULL
    SELECT @sql = @sql + ' AND (([Employee_CreationDate] >= @fromDate))'
IF @toDate IS NOT NULL
    SELECT @sql = @sql + ' AND (([Employee_CreationDate] <= @toDate))'

fromDatetoDate参数在生成的 BOM 中不能为空。但是,如果您使用默认等于默认值的默认值CodeFluentPersistence.DefaultDateTimeValue更多关于默认值),NULL将被发送到存储过程。

如果您更愿意DateTime?将参数的类型从更改datedate?

SEARCH(date? fromDate, date? toDate)
WHERE CreationDate >= @fromDate AND CreationDate <= @toDate

生成以下 C# 方法:

public static EmployeeCollection Search(DateTime? fromDate, DateTime? toDate)
于 2016-03-10T16:19:04.840 回答