我已经创建了一个具有这种结构的过程,但它不适用于datetime
输入参数
我执行了这个查询但是
declare @a datetime
declare @b datetime
set @a='2012/04/06 12:23:45'
set @b='2012/08/06 21:10:12'
exec LogProcedure 'AccountLog', N'test', @a, @b
但是 SQL Server 让我遇到了这个错误
从字符串转换日期和/或时间时转换失败。
但是当我用这个查询进行测试时,它可以工作
exec LogProcedure 'AccountLog',N'test'
存储过程代码:
alter PROCEDURE LogProcedure
@TableName VARCHAR(60),
@SearchString NVARCHAR(50),
@DateFirst DateTime = '',
@DateLast DateTime = ''
AS
BEGIN
SET NOCOUNT ON
DECLARE @FinalSQL NVARCHAR(MAX)
SET @FINALSQL = 'SELECT * FROM [' + @TableName + '] where 1=2 '
IF @DateFirst <> '' and @DateLast <> ''
set @FinalSQL = @FinalSQL + ' or convert (Date,DateLog) >= '''+@DateFirst + ' and convert (Date,DateLog) <='''+@DateLast
SELECT
@FinalSQL = @FinalSQL + ' or [' + SYSCOLUMNS.NAME + '] LIKE N''%' + @SearchString + '%'' '
FROM SYSCOLUMNS
WHERE OBJECT_NAME(id) = @TableName
AND TYPE_NAME(SYSCOLUMNS.XTYPE) IN ('VARCHAR','NVARCHAR','CHAR','NCHAR','INT','DECIMAL')
ORDER BY COLID
EXEC(@FinalSQL)
END
这个查询也是真的
SELECT *
FROM AccountLog
where 1=2 or convert (Date, DateLog) >= '2012/04/06'
and convert (Date, DateLog) <='2012/08/06'