不知道各位大侠还有其他的什么好方法能实现吗?

大家好,我有一个数据表(如表1所示),我想对每个站在开始时间和结束时间之间插入时间段,步长为1天,得到的结果应该为表2所示,

表1结构如下
站名 开始时间 结束时间
s1 1980-1-1 1980-2-1
s2 1992-2-1 1993-3-1

表2
站名 时间
s1 1980-1-1
s1 1980-1-2
s1 1980-1-3
................
s1 1980-2-1
s2 1992-2-1
s2 1992-2-2
..........
s2 1993-3-1

我自己编了一个表函数,但是调用函数时怎么才能把各个站点的参数传递到函数里呢?谢谢各位大侠了
--@tminit是开始时间,@tmend是结束时间,TM是时间
CREATE FUNCTION CaculateDailyTM(@STCD char(8),@tminit datetime,@tmend datetime)
returns @mytbl table (STCD char(8),TM datetime)
as
begin

declare @temptm datetime
set @temptm=@tminit
while @temptm<=@tmend
begin
insert into @mytbl select STCD,@temptm
set @temptm=@temptm+1
end
return
RETURN
end

GO

慕妹3146593
浏览 115回答 2
2回答

一只斗牛犬

这个问题用标致函数比较难,1楼调用标致函数的方法也是错误的,正确的方法应该是:SELECT * FROM DBO.CaculateDailyTM()我用游标实现 ,比较好理解:create table tb1(zm char(8),sd datetime,ed datetime)GOinsert into tb1 values('s1','1980-1-1','1980-2-1')insert into tb1 values('s2','1992-2-1','1993-3-1')GO--建立测试数据declare zm_cursor cursor fast_forward for select zm from tb1declare @t1 datetime,@t2 datetime,@zm char(8)declare @tb table(zm char(8),d datetime)open zm_cursorfetch next from zm_cursor into @zmwhile @@fetch_status=0beginselect @t1=sd,@t2=ed from tb1 where zm=@zmwhile @t1<=@t2begininsert into @tb values(@zm,@t1)set @t1=dateadd(day,1,@t1)endfetch next from zm_cursor into @zmendselect * from @tbclose zm_cursordeallocate zm_cursorGODROP TABLE TB1有问题HI我这个问题用标致函数比较难,1楼调用标致函数的方法也是错误的,正确的方法应该是:SELECT * FROM DBO.CaculateDailyTM()我用游标实现 ,比较好理解:create table tb1(zm char(8),sd datetime,ed datetime)GOinsert into tb1 values('s1','1980-1-1','1980-2-1')insert into tb1 values('s2','1992-2-1','1993-3-1')GO--建立测试数据declare zm_cursor cursor fast_forward for select zm from tb1declare @t1 datetime,@t2 datetime,@zm char(8)declare @tb table(zm char(8),d datetime)open zm_cursorfetch next from zm_cursor into @zmwhile @@fetch_status=0beginselect @t1=sd,@t2=ed from tb1 where zm=@zmwhile @t1<=@t2begininsert into @tb values(@zm,@t1)set @t1=dateadd(day,1,@t1)endfetch next from zm_cursor into @zmendselect * from @tbclose zm_cursordeallocate zm_cursorGODROP TABLE TB1有问题HI我这个问题用标致函数比较难,1楼调用标致函数的方法也是错误的,正确的方法应该是:SELECT * FROM DBO.CaculateDailyTM()我用游标实现 ,比较好理解:create table tb1(zm char(8),sd datetime,ed datetime)GOinsert into tb1 values('s1','1980-1-1','1980-2-1')insert into tb1 values('s2','1992-2-1','1993-3-1')GO--建立测试数据declare zm_cursor cursor fast_forward for select zm from tb1declare @t1 datetime,@t2 datetime,@zm char(8)declare @tb table(zm char(8),d datetime)open zm_cursorfetch next from zm_cursor into @zmwhile @@fetch_status=0beginselect @t1=sd,@t2=ed from tb1 where zm=@zmwhile @t1<=@t2begininsert into @tb values(@zm,@t1)set @t1=dateadd(day,1,@t1)endfetch next from zm_cursor into @zmendselect * from @tbclose zm_cursordeallocate zm_cursorGODROP TABLE TB1有问题HI我这个问题用标致函数比较难,1楼调用标致函数的方法也是错误的,正确的方法应该是:SELECT * FROM DBO.CaculateDailyTM()我用游标实现 ,比较好理解:create table tb1(zm char(8),sd datetime,ed datetime)GOinsert into tb1 values('s1','1980-1-1','1980-2-1')insert into tb1 values('s2','1992-2-1','1993-3-1')GO--建立测试数据declare zm_cursor cursor fast_forward for select zm from tb1declare @t1 datetime,@t2 datetime,@zm char(8)declare @tb table(zm char(8),d datetime)open zm_cursorfetch next from zm_cursor into @zmwhile @@fetch_status=0beginselect @t1=sd,@t2=ed from tb1 where zm=@zmwhile @t1<=@t2begininsert into @tb values(@zm,@t1)set @t1=dateadd(day,1,@t1)endfetch next from zm_cursor into @zmendselect * from @tbclose zm_cursordeallocate zm_cursorGODROP TABLE TB1有问题HI我这个问题用标致函数比较难,1楼调用标致函数的方法也是错误的,正确的方法应该是:SELECT * FROM DBO.CaculateDailyTM()我用游标实现 ,比较好理解:create table tb1(zm char(8),sd datetime,ed datetime)GOinsert into tb1 values('s1','1980-1-1','1980-2-1')insert into tb1 values('s2','1992-2-1','1993-3-1')GO--建立测试数据declare zm_cursor cursor fast_forward for select zm from tb1declare @t1 datetime,@t2 datetime,@zm char(8)declare @tb table(zm char(8),d datetime)open zm_cursorfetch next from zm_cursor into @zmwhile @@fetch_status=0beginselect @t1=sd,@t2=ed from tb1 where zm=@zmwhile @t1<=@t2begininsert into @tb values(@zm,@t1)set @t1=dateadd(day,1,@t1)endfetch next from zm_cursor into @zmendselect * from @tbclose zm_cursordeallocate zm_cursorGODROP TABLE TB1有问题HI我这个问题用标致函数比较难,1楼调用标致函数的方法也是错误的,正确的方法应该是:SELECT * FROM DBO.CaculateDailyTM()我用游标实现 ,比较好理解:create table tb1(zm char(8),sd datetime,ed datetime)GOinsert into tb1 values('s1','1980-1-1','1980-2-1')insert into tb1 values('s2','1992-2-1','1993-3-1')GO--建立测试数据declare zm_cursor cursor fast_forward for select zm from tb1declare @t1 datetime,@t2 datetime,@zm char(8)declare @tb table(zm char(8),d datetime)open zm_cursorfetch next from zm_cursor into @zmwhile @@fetch_status=0beginselect @t1=sd,@t2=ed from tb1 where zm=@zmwhile @t1<=@t2begininsert into @tb values(@zm,@t1)set @t1=dateadd(day,1,@t1)endfetch next from zm_cursor into @zmendselect * from @tbclose zm_cursordeallocate zm_cursorGODROP TABLE TB1有问题HI我这个问题用标致函数比较难,1楼调用标致函数的方法也是错误的,正确的方法应该是:SELECT * FROM DBO.CaculateDailyTM()我用游标实现 ,比较好理解:create table tb1(zm char(8),sd datetime,ed datetime)GOinsert into tb1 values('s1','1980-1-1','1980-2-1')insert into tb1 values('s2','1992-2-1','1993-3-1')GO--建立测试数据declare zm_cursor cursor fast_forward for select zm from tb1declare @t1 datetime,@t2 datetime,@zm char(8)declare @tb table(zm char(8),d datetime)open zm_cursorfetch next from zm_cursor into @zmwhile @@fetch_status=0beginselect @t1=sd,@t2=ed from tb1 where zm=@zmwhile @t1<=@t2begininsert into @tb values(@zm,@t1)set @t1=dateadd(day,1,@t1)endfetch next from zm_cursor into @zmendselect * from @tbclose zm_cursordeallocate zm_cursorGODROP TABLE TB1有问题HI我这个问题用标致函数比较难,1楼调用标致函数的方法也是错误的,正确的方法应该是:SELECT * FROM DBO.CaculateDailyTM()我用游标实现 ,比较好理解:create table tb1(zm char(8),sd datetime,ed datetime)GOinsert into tb1 values('s1','1980-1-1','1980-2-1')insert into tb1 values('s2','1992-2-1','1993-3-1')GO--建立测试数据declare zm_cursor cursor fast_forward for select zm from tb1declare @t1 datetime,@t2 datetime,@zm char(8)declare @tb table(zm char(8),d datetime)open zm_cursorfetch next from zm_cursor into @zmwhile @@fetch_status=0beginselect @t1=sd,@t2=ed from tb1 where zm=@zmwhile @t1<=@t2begininsert into @tb values(@zm,@t1)set @t1=dateadd(day,1,@t1)endfetch next from zm_cursor into @zmendselect * from @tbclose zm_cursordeallocate zm_cursorGODROP TABLE TB1有问题HI我这个问题用标致函数比较难,1楼调用标致函数的方法也是错误的,正确的方法应该是:SELECT * FROM DBO.CaculateDailyTM()我用游标实现 ,比较好理解:create table tb1(zm char(8),sd datetime,ed datetime)GOinsert into tb1 values('s1','1980-1-1','1980-2-1')insert into tb1 values('s2','1992-2-1','1993-3-1')GO--建立测试数据declare zm_cursor cursor fast_forward for select zm from tb1declare @t1 datetime,@t2 datetime,@zm char(8)declare @tb table(zm char(8),d datetime)open zm_cursorfetch next from zm_cursor into @zmwhile @@fetch_status=0beginselect @t1=sd,@t2=ed from tb1 where zm=@zmwhile @t1<=@t2begininsert into @tb values(@zm,@t1)set @t1=dateadd(day,1,@t1)endfetch next from zm_cursor into @zmendselect * from @tbclose zm_cursordeallocate zm_cursorGODROP TABLE TB1有问题HI我这个问题用标致函数比较难,1楼调用标致函数的方法也是错误的,正确的方法应该是:SELECT * FROM DBO.CaculateDailyTM()我用游标实现 ,比较好理解:create table tb1(zm char(8),sd datetime,ed datetime)GOinsert into tb1 values('s1','1980-1-1','1980-2-1')insert into tb1 values('s2','1992-2-1','1993-3-1')GO--建立测试数据declare zm_cursor cursor fast_forward for select zm from tb1declare @t1 datetime,@t2 datetime,@zm char(8)declare @tb table(zm char(8),d datetime)open zm_cursorfetch next from zm_cursor into @zmwhile @@fetch_status=0beginselect @t1=sd,@t2=ed from tb1 where zm=@zmwhile @t1<=@t2begininsert into @tb values(@zm,@t1)set @t1=dateadd(day,1,@t1)endfetch next from zm_cursor into @zmendselect * from @tbclose zm_cursordeallocate zm_cursorGODROP TABLE TB1有问题HI我这个问题用标致函数比较难,1楼调用标致函数的方法也是错误的,正确的方法应该是:SELECT * FROM DBO.CaculateDailyTM()我用游标实现 ,比较好理解:create table tb1(zm char(8),sd datetime,ed datetime)GOinsert into tb1 values('s1','1980-1-1','1980-2-1')insert into tb1 values('s2','1992-2-1','1993-3-1')GO--建立测试数据declare zm_cursor cursor fast_forward for select zm from tb1declare @t1 datetime,@t2 datetime,@zm char(8)declare @tb table(zm char(8),d datetime)open zm_cursorfetch next from zm_cursor into @zmwhile @@fetch_status=0beginselect @t1=sd,@t2=ed from tb1 where zm=@zmwhile @t1<=@t2begininsert into @tb values(@zm,@t1)set @t1=dateadd(day,1,@t1)endfetch next from zm_cursor into @zmendselect * from @tbclose zm_cursordeallocate zm_cursorGODROP TABLE TB1

红颜莎娜

select dbo.CaculateDailyTM(站名, 开始时间,结束时间) from 表1
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

SQL Server