I have created a procedure with this structure but it doesn't work for datetime
input parameter
I executed this query but
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
but SQL Server got me this error
Conversion failed when converting date and/or time from character string.
but when I test with this query it works
exec LogProcedure 'AccountLog',N'test'
Stored procedure code:
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
This query is true too
SELECT *
FROM AccountLog
where 1=2 or convert (Date, DateLog) >= '2012/04/06'
and convert (Date, DateLog) <='2012/08/06'