请教一条拼接sql
declare @sdate datetime
set @sdate='2014-03-09'
declare @city varchar(100)
print ' select * from dbo.ShengHuo where tiaotime between DATEADD(Hour,6,'''+ CONVERT(varchar(100), @sdate, 120)+''') and DATEADD(day,1,DATEADD(Hour,6,'''+CONVERT(varchar(100), @sdate, 120)+'''))'
结果是
select * from dbo.ShengHuo where tiaotime between DATEADD(Hour,6,'2014-03-09 00:00:00') and DATEADD(day,1,DATEADD(Hour,6,'2014-03-09 00:00:00'))
但是我要是不打印而是用exec执行的话就报
关键字 'CONVERT' 附近有语法错误。
------解决方案--------------------这样试试:
declare @sdate datetime
set @sdate='2014-03-09'
SET @sdate=CONVERT(varchar(100), @sdate, 120)
declare @city varchar(100)
EXEC( ' select * from dbo.ShengHuo where tiaotime between DATEADD(Hour,6,'''+@sdate +''') and DATEADD(day,1,DATEADD(Hour,6,'''+@sdate+'''))')