-6

我正在尝试比较两个日期时间,但它在动态查询中不起作用。为什么部分时间不起作用?我的代码是这些

    ALTER PROCEDURE [dbo].[testdate]    
    @startDate datetime = null  ,
    @endDate datetime = null,
    @TableName nvarchar(100) ,
    @SearchString NVARCHAR(200)
AS
BEGIN   
    SET NOCOUNT ON; 
    declare @sqlcmd nvarchar(max)


    set  @sqlcmd= 'select * from '+@TableName  +' where '   
    if @startDate is not null and @endDate is not null
    begin

        /*set @sqlcmd=@sqlcmd + ' ( DateLog between'''
        + convert (nvarchar(50), @startDate, 101)
        +''' and ''' 
        --+ convert (nvarchar(50),DATEADD(D,1, @endDate), 101)
        + convert (nvarchar(50), @endDate, 101)
        + ''' ) and '   
        */
        set @sqlcmd=@sqlcmd + ' ( DateLog between @startDate and @endDate ) and '

    end     


    set @sqlcmd=@sqlcmd + '('
    SELECT 
            @sqlcmd  = @sqlcmd + '  [' + SYSCOLUMNS.NAME + '] LIKE N''%'+@SearchString+'%'' or '    
            FROM SYSCOLUMNS 
            WHERE OBJECT_NAME(id) =  @TableName AND TYPE_NAME(SYSCOLUMNS.XTYPE) IN ('VARCHAR','NVARCHAR','CHAR','NCHAR','INT','DECIMAL')

        set @sqlcmd  = @sqlcmd  + '1<>1)'                           
    --print @sqlcmd                                             
    Exec(@sqlcmd)
END

我的表 AccountLog 有这些字段和数据

     UserName     DateLog
     Salah        2012-04-06 22:06:19.493
     John         2012-08-06 22:06:22.800

这个查询是真的

 select * from AccountLog where DateLog between '2012-04-06T22:06:19' and '2012-08-06T22:06:23'

但是这个查询没有给我适当的结果

declare @a datetime
declare @b datetime 
set @a = '2012-04-06T22:06:19'  
set @b = '2012-08-06T22:06:23'
exec testdate @a,@b,'AccountLog',N'test' 
4

2 回答 2

3

您应该使用强类型参数并完全抛弃任何转换为​​ nvarchar 的废话。

EDIT更新了更多需求更改。

ALTER PROCEDURE [dbo].[testdate]    
  @startDate    DATETIME = NULL,
  @endDate      DATETIME = NULL,
  @TableName    NVARCHAR(100),
  @SearchString NVARCHAR(200)
AS
BEGIN 
  SET NOCOUNT ON;

  DECLARE @sql NVARCHAR(MAX);
  SET @sql = N'SELECT * FROM dbo.' + QUOTENAME(@TableName) 
    + ' WHERE (1 = 0 ';

  SELECT @sql = @sql + ' OR ' + QUOTENAME(name) + ' LIKE N''%'+@SearchString+'%'''  
    FROM sys.columns WHERE [object_id] = OBJECT_ID('dbo.' + @TableName)
    AND system_type_id IN (56,106,167,175,231,239);

  SET @sql = @sql + ')';

  IF @startDate IS NOT NULL AND @endDate IS NOT NULL
  BEGIN
    SET @sql = @sql + ' AND DateLog BETWEEN @s AND @e';
  END

  EXEC sp_executesql @sql, N'@s DATETIME, @e DATETIME', @startDate, @endDate;
END
GO
于 2012-04-12T15:19:13.143 回答
1

您将日期和时间传递给您的过程,但是当您将它们转换为动态 SQL 时,您使用的是格式 101,它删除了时间部分。尝试将 101 更改为 126(您的代码中有 2 个地方使用了 101,请确保同时更改它们。

于 2012-04-12T16:38:43.217 回答