2 つの日時を比較しようとしていますが、動的クエリでは機能しません。時間の一部が機能しないのはなぜですか?私のコードはこれらです
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
このクエリは true
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'