如何得到这种表?
在存储过程里面 有两个参数 一个开始日期,一个截至日期
我想得到一个表,这个表只有两列 一列自增,一列就是日期
比如
开始日期为2009-08-01 截至日期为2009-08-05 那么记录就为
id 日期
1 2009-08-01
2 2009-08-02
3 2009-08-03
4 2009-08-04
5 2009-08-05
越简单越好 谢谢先!
[解决办法]
http://topic.csdn.net/u/20090906/17/a91cd31e-abf7-4190-8486-c5491aa63c71.html?seed=1380177511&r=59598572#r_59598572
[解决办法]
==
[解决办法]
可以参考一楼的。
[解决办法]
if(object_id('p_test')>0)drop proc p_testgocreate proc p_test (@date1 datetime,@date2 datetime)asbegin select number=number+1,convert(varchar(10),@date1+number,120) from master..spt_values where type='p' and @date1+number<=@date2end goexec p_test '2009-01-01','2009-01-10'/*1 2009-01-012 2009-01-023 2009-01-034 2009-01-045 2009-01-056 2009-01-067 2009-01-078 2009-01-089 2009-01-0910 2009-01-10*/
[解决办法]
declare @date datetimeset @date='2009-02-05'select [day]=convert(varchar(6),@date,112)++right('0'+ltrim(number),2)from master..spt_valueswhere type='p' and number >=1 and number <= datediff(dd,@date,dateadd(month,1,@date))
[解决办法]
declare @begin datetime,@end datetimeselect @begin='2009-08-01',@end='2009-08-05' select id=number+1,日期=dateadd(dd,number,@begin) from master..spt_valueswhere type='p' and number between 0 and datediff(dd,@begin,@end)/*id 日期 ----------- ------------------------------------------------------ 1 2009-08-01 00:00:00.0002 2009-08-02 00:00:00.0003 2009-08-03 00:00:00.0004 2009-08-04 00:00:00.0005 2009-08-05 00:00:00.000(所影响的行数为 5 行)*/
[解决办法]
declare @begin datetime,@end datetimeselect @begin='2009-08-01',@end='2009-08-05' select id=number+1,日期=convert(varchar(10),dateadd(dd,number,@begin),120) from master..spt_valueswhere type='p' and number between 0 and datediff(dd,@begin,@end)/*id 日期 ----------- ---------- 1 2009-08-012 2009-08-023 2009-08-034 2009-08-045 2009-08-05(所影响的行数为 5 行)*/
[解决办法]
这个例子很多,我蹭分了
------解决方案--------------------
在存储过程里面 有两个参数 一个开始日期,一个截至日期 我想得到一个表,这个表只有两列 一列自增,一列就是日期 比如 开始日期为2009-08-01 截至日期为2009-08-05 那么记录就为 id 日期 1 2009-08-01 2 2009-08-02 3 2009-08-03 4 2009-08-04 5 2009-08-05 越简单越好 谢谢先!if OBJECT_ID('tb') is not null drop table tb gocreate table tb(id int identity(1,1),name nvarchar(10)) GOalter proc dbo.procnames @start_rq nvarchar(10), @end_rq nvarchar(10)asbegin declare @day1 int, @day2 int, @n int set @day1=DATEPART(day,@start_rq) set @day2=DATEPART(day,@end_rq) set @n=@day2-@day1+1 while @n>=1 begin insert into tb(name) select LEFT(@start_rq,8)+right(CAST(@day2-@n+101 as varchar),2) set @n=@n-1 endendgoexec dbo.procnames '2009-09-01','2009-09-10'select * from tb/*id name1 2009-09-012 2009-09-023 2009-09-034 2009-09-045 2009-09-056 2009-09-067 2009-09-078 2009-09-089 2009-09-0910 2009-09-10*/
[解决办法]
sybase 有这个表的。
[解决办法]
if OBJECT_ID('tb') is not null drop table tb gocreate table tb(id int identity(1,1),name nvarchar(10)) GOalter proc dbo.procnames @start_rq nvarchar(10), @end_rq nvarchar(10)asbegin declare @day1 int, @day2 int, @n int set @day1=DATEPART(day,@start_rq) set @day2=DATEPART(day,@end_rq) set @n=@day2-@day1+1 while @n>=1 begin insert into tb(name) select LEFT(@start_rq,8)+right(CAST(@day2-@n+101 as varchar),2) set @n=@n-1 endendgocreate proc dbo.procnames2 @start_rq nvarchar(10), @end_rq nvarchar(10)asbegininsert into tb(name) select left(@start_rq,8)+right(CAST(number+100 as varchar),2) from master..spt_values where type='p' and number between 1 and datediff(dd,@start_rq,@end_rq)+1endgoexec dbo.procnames '2009-09-01','2009-09-10'exec dbo.procnames '2009-09-01','2009-09-10'select * from tb/*id name1 2009-09-012 2009-09-023 2009-09-034 2009-09-045 2009-09-056 2009-09-067 2009-09-078 2009-09-089 2009-09-0910 2009-09-1011 2009-09-0112 2009-09-0213 2009-09-0314 2009-09-0415 2009-09-0516 2009-09-0617 2009-09-0718 2009-09-0819 2009-09-0920 2009-09-10*/
[解决办法]
declare @a datetime,@b datetimeset @a='2009-08-01' set @b='2009-08-05'select [day]=convert(varchar,DATEADD(mm,DATEDIFF(mm,0,@a),0)+number,112)from master..spt_valueswhere type='p'and number< datepart(dd,@b)/*day------------------------------2009080120090802200908032009080420090805(5 行受影响)*/
[解决办法]
declare @StartTime datetimedeclare @EndTime datetimedeclare @NowTime datetimeset @StartTime='2009-5-1'set @EndTime='2009-6-10'if @StartTime<@EndTimeselect number+1 as ID,dateadd(day,number,@StartTime) Date from master..spt_values where type='P' and datediff(day,@StartTime+number,@EndTime)>0ID Date----------- -----------------------1 2009-05-01 00:00:00.0002 2009-05-02 00:00:00.0003 2009-05-03 00:00:00.0004 2009-05-04 00:00:00.0005 2009-05-05 00:00:00.0006 2009-05-06 00:00:00.0007 2009-05-07 00:00:00.0008 2009-05-08 00:00:00.0009 2009-05-09 00:00:00.00010 2009-05-10 00:00:00.00011 2009-05-11 00:00:00.00012 2009-05-12 00:00:00.00013 2009-05-13 00:00:00.00014 2009-05-14 00:00:00.00015 2009-05-15 00:00:00.00016 2009-05-16 00:00:00.00017 2009-05-17 00:00:00.00018 2009-05-18 00:00:00.00019 2009-05-19 00:00:00.00020 2009-05-20 00:00:00.00021 2009-05-21 00:00:00.00022 2009-05-22 00:00:00.00023 2009-05-23 00:00:00.00024 2009-05-24 00:00:00.00025 2009-05-25 00:00:00.00026 2009-05-26 00:00:00.00027 2009-05-27 00:00:00.00028 2009-05-28 00:00:00.00029 2009-05-29 00:00:00.00030 2009-05-30 00:00:00.00031 2009-05-31 00:00:00.00032 2009-06-01 00:00:00.00033 2009-06-02 00:00:00.00034 2009-06-03 00:00:00.00035 2009-06-04 00:00:00.00036 2009-06-05 00:00:00.00037 2009-06-06 00:00:00.00038 2009-06-07 00:00:00.00039 2009-06-08 00:00:00.00040 2009-06-09 00:00:00.000
[解决办法]
declare @a datetime,@b datetimeset @a='2009-08-01' set @b='2009-09-05'select [day]=convert(varchar,DATEADD(mm,DATEDIFF(mm,0,@a),0)+number,112)from master..spt_valueswhere type='p'and number<=datediff(dd,@a,@b)/*day------------------------------200908012009080220090803200908042009080520090806200908072009080820090809200908102009081120090812200908132009081420090815200908162009081720090818200908192009082020090821200908222009082320090824200908252009082620090827200908282009082920090830200908312009090120090902200909032009090420090905(36 行受影响)*/
[解决办法]
SYBASE与SQLSERVER是不一样的,用SP来解决
or
with recursive ddq(id1) as
(
select cast('2009/8/1 00:00:00' as datetime) as id1
union all
select DATEADD( day, 1, id1) as id2 from ddq where id2<=cast('2009/8/9 00:00:00' as datetime))
select * from ddq
[解决办法]
上述代码是在ASA下的,不知道ASE是否支持,如不支持,用SP,
http://topic.csdn.net/u/20090910/14/0a99b73a-6a85-4821-a226-4505fea19c08.html?2103