日期:2014-05-18 浏览次数:20622 次
DECLARE @WHERES VARCHAR(1000) DECLARE @a datetime DECLARE @b datetime DECLARE @c varchar(100) set @a=N'2012-08-14 15:00:00' set @b=N'2012-08-14 19:30:00' set @c='CAA396ED-9DE2-4C80-B4A8-0BFBF3DBBC33' SET @WHERES='AND CompanyId='+@c+' AND GpsTime>='+quotename(@a,'''') +' AND GpsTime<='+QUOTENAME(@b,'''') print @WHERES --AND CompanyId=CAA396ED-9DE2-4C80-B4A8-0BFBF3DBBC33 AND GpsTime>='08 14 2012 3:00PM' AND GpsTime<='08 14 2012 7:30PM' if @@ERROR<>0 begin SET @WHERES='' end --直接用quotename就行了
------解决方案--------------------
DATETIME需要转换,GUID需要引号
DECLARE @WHERES VARCHAR(1000) DECLARE @a datetime DECLARE @b datetime DECLARE @c varchar(100) set @a=N'2012-08-14 15:00:00' set @b=N'2012-08-14 19:30:00' set @c='CAA396ED-9DE2-4C80-B4A8-0BFBF3DBBC33' SET @WHERES='AND CompanyId='''+@c+''' AND GpsTime>='''+CONVERT(VARCHAR(19),@a,120)+''' AND GpsTime<='''+CONVERT(VARCHAR(19),@b,120)+'''' if @@ERROR<>0 begin SET @WHERES='' end
------解决方案--------------------
DECLARE @WHERES VARCHAR(1000) DECLARE @a datetime DECLARE @b datetime DECLARE @c varchar(100) set @a=N'2012-08-14 15:00:00' set @b=N'2012-08-14 19:30:00' set @c='CAA396ED-9DE2-4C80-B4A8-0BFBF3DBBC33' SET @WHERES='AND CompanyId='''+@c+''' AND GpsTime>='''+CONVERT(VARCHAR(19),@a,120)+''' AND GpsTime<='''+CONVERT(VARCHAR(19),@b,120)+'''' if @@ERROR<>0 begin SET @WHERES='' end