变量外加单引号的问题,头大了!!!!!!!!!!!!!!!

aohan 2005-01-13 11:45:28
测试如下,发现@sql变量不是想像中的能正确执行
declare @startdate char(10)
declare @Enddate char(10)
declare @sql varchar(500)
set @startdate='2005-01-01'
set @enddate='2005-01-10'
set @sql ='select empid,kqdate,min(kqtime)as kqtime from att_kqdata where
kqdate between '+ @startdate +' and '+ @Enddate + ' group by empid,kqdate order by kqdate'
--print @sql
exec(@sql)


print 结果为:
select empid,kqdate,min(kqtime)as kqtime from att_kqdata where
kqdate between 2005-01-01 and 2005-01-10 group by empid,kqdate order by kqdate

而我想要的结果为

select empid,kqdate,min(kqtime)as kqtime from att_kqdata where
kqdate between '2005-01-01' and '2005-01-10' group by empid,kqdate order by kqdate

不知变量外要加''怎么处理了

单独执行:

select empid,kqdate,min(kqtime)as kqtime from att_kqdata where
kqdate between @startdate and @enddate group by empid,kqdate order by kqdate


是有返回结果的,如:

empid kqdate kqtime
----------- ------------------------------------------------------ -----------
10 2005-01-01 00:00:00.000 28495
11 2005-01-01 00:00:00.000 28110
12 2005-01-01 00:00:00.000 59480
14 2005-01-01 00:00:00.000 27769
16 2005-01-01 00:00:00.000 27771
20 2005-01-01 00:00:00.000 27789
24 2005-01-01 00:00:00.000 37496
27 2005-01-01 00:00:00.000 50614
28 2005-01-01 00:00:00.000 27782
29 2005-01-01 00:00:00.000 51883



头大了!!!!!!!!!!!!!!!!!
...全文
119 2 打赏 收藏 举报
写回复
2 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
zhongxin799 2005-01-14
set @sql ='select empid,kqdate,min(kqtime)as kqtime from att_kqdata where
kqdate between '+ ''''+ @startdate +'''' +' and '+ ''''+ @Enddate +''''+ ' group by empid,kqdate order by kqdate'
  • 打赏
  • 举报
回复
aohan 2005-01-13
怎么发的问题在页面上看不到?
  • 打赏
  • 举报
回复
相关推荐
发帖
应用实例
加入

2.7w+

社区成员

MS-SQL Server 应用实例
申请成为版主
帖子事件
创建了帖子
2005-01-13 11:45
社区公告
暂无公告