我是说你的visittime的数据类型,如果是DATETIME select * from 表 where between '2004-12-9' and '2004-12-20'
不好意思: select * from 表 where visittime between '2004-12-9' and '2004-12-20'
select * from 表 where convert(char(10),visittime,120) between '2004-12-09' and '2004-12-20'
对年份没有要求啊! between是可以,但是要循环出每一天的记录,所以我想到了用while循环
set @mydate1 = 3 while (@mydate1<=20) begin select * from tblcount where convert(varchar(10),visittime,120) = '2004-12-'+Convert(varchar(2),@mydate1) set @mydate1 = @mydate1+1 end 还是不行,不能这样解决么?
不要把select * from tblcount ...写进 while 循环,你想啊,那要执行多少次 select 啊, 每一次select都放在不同的结果集,对你有什么用呢,你怎么看这个数据呢?
select * from t where visittime='2004-12-01' select * from t where visittime='2004-12-02' select * from t where visittime='2004-12-03' --- select * from t where visittime='2004-12-21' 该不会说是这样写吗? 我只是想动态地生成SQL语句,把后面的visittime增而已呀
select * from t where visittime='2004-12-01' select * from t where visittime='2004-12-02' select * from t where visittime='2004-12-03' --- select * from t where visittime='2004-12-21' 这样作和 select * from t where visittime between '2004-12-01' and '2004-12-21' 有什么区别吗? 在查询分析器里你上面的写法会出现n多个结果集框,后面的把所有结果放在一个框内。你非要用while的话 while datediff(d,@begindate,@enddate)>=0 begin ---这里放你要做的事情。 set @begindate=dateadd(d,1,@begindate) end
select * from 表 where convert(char(10),visittime,120) between '2004-12-09' and '2004-12-20'
可以使用游标来实现: 第一步:创建游标 DECLARE crr_record CURSOR SCROLL FOR SELECT * FROM 表 WHERE visittime BETWEEN '2004-12-09' AND '2004-12-20' 第二步:使用游标 declare @c1 类型 declare @c2 类型 declare @c3 类型 OPEN crr_record WHILE @@fetch_status=0 begin fetch next from crr_record into @c1,@c2,@c3 print @c1,@c2,@c3 end 说明:@c1,@c2,@c3为第一、二、三列的值
select * from 表 where between '2004-12-9' and '2004-12-20'
select * from 表 where visittime between '2004-12-9' and '2004-12-20'
between是可以,但是要循环出每一天的记录,所以我想到了用while循环
while (@mydate1<=20)
begin
select * from tblcount where convert(varchar(10),visittime,120) = '2004-12-'+Convert(varchar(2),@mydate1)
set @mydate1 = @mydate1+1
end
还是不行,不能这样解决么?
每一次select都放在不同的结果集,对你有什么用呢,你怎么看这个数据呢?
select * from t where visittime='2004-12-02'
select * from t where visittime='2004-12-03'
---
select * from t where visittime='2004-12-21'
该不会说是这样写吗?
我只是想动态地生成SQL语句,把后面的visittime增而已呀
select * from t where visittime='2004-12-02'
select * from t where visittime='2004-12-03'
---
select * from t where visittime='2004-12-21'
这样作和
select * from t where visittime between '2004-12-01' and '2004-12-21' 有什么区别吗?
在查询分析器里你上面的写法会出现n多个结果集框,后面的把所有结果放在一个框内。你非要用while的话
while datediff(d,@begindate,@enddate)>=0
begin
---这里放你要做的事情。
set @begindate=dateadd(d,1,@begindate)
end
第一步:创建游标
DECLARE crr_record CURSOR SCROLL FOR
SELECT * FROM 表 WHERE visittime BETWEEN '2004-12-09' AND '2004-12-20'
第二步:使用游标
declare @c1 类型
declare @c2 类型
declare @c3 类型
OPEN crr_record
WHILE @@fetch_status=0
begin
fetch next from crr_record into @c1,@c2,@c3
print @c1,@c2,@c3
end
说明:@c1,@c2,@c3为第一、二、三列的值