永发信息网

关于sql server中的表函数

答案:3  悬赏:40  手机版
解决时间 2021-01-26 00:14
大家好,我有一个数据表(如表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
最佳答案
这个问题用标致函数比较难,1楼调用标致函数的方法也是错误的,正确的方法应该是:
SELECt * FROM DBO.CaculateDailyTM()

我用游标实现 ,比较好理解:

create table tb1(zm char(8),sd datetime,ed datetime)
GO
insert 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 tb1
declare @t1 datetime,@t2 datetime,@zm char(8)
declare @tb table(zm char(8),d datetime)
open zm_cursor
fetch next from zm_cursor into @zm
while @@fetch_status=0
begin
select @t1=sd,@t2=ed from tb1 where zm=@zm
while @t1<=@t2
begin
insert into @tb values(@zm,@t1)
set @t1=dateadd(day,1,@t1)
end
fetch next from zm_cursor into @zm
end
select * from @tb
close zm_cursor
deallocate zm_cursor

GO

DROp TABLE TB1

有问题HI我

这个问题用标致函数比较难,1楼调用标致函数的方法也是错误的,正确的方法应该是:
SELECT * FROM DBO.CaculateDailyTM()

我用游标实现 ,比较好理解:

create table tb1(zm char(8),sd datetime,ed datetime)
GO
insert 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 tb1
declare @t1 datetime,@t2 datetime,@zm char(8)
declare @tb table(zm char(8),d datetime)
open zm_cursor
fetch next from zm_cursor into @zm
while @@fetch_status=0
begin
select @t1=sd,@t2=ed from tb1 where zm=@zm
while @t1<=@t2
begin
insert into @tb values(@zm,@t1)
set @t1=dateadd(day,1,@t1)
end
fetch next from zm_cursor into @zm
end
select * from @tb
close zm_cursor
deallocate zm_cursor

GO

DROp TABLE TB1

有问题HI我

这个问题用标致函数比较难,1楼调用标致函数的方法也是错误的,正确的方法应该是:
SELECT * FROM DBO.CaculateDailyTM()

我用游标实现 ,比较好理解:

create table tb1(zm char(8),sd datetime,ed datetime)
GO
insert 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 tb1
declare @t1 datetime,@t2 datetime,@zm char(8)
declare @tb table(zm char(8),d datetime)
open zm_cursor
fetch next from zm_cursor into @zm
while @@fetch_status=0
begin
select @t1=sd,@t2=ed from tb1 where zm=@zm
while @t1<=@t2
begin
insert into @tb values(@zm,@t1)
set @t1=dateadd(day,1,@t1)
end
fetch next from zm_cursor into @zm
end
select * from @tb
close zm_cursor
deallocate zm_cursor

GO

DROp TABLE TB1

有问题HI我

这个问题用标致函数比较难,1楼调用标致函数的方法也是错误的,正确的方法应该是:
SELECT * FROM DBO.CaculateDailyTM()

我用游标实现 ,比较好理解:

create table tb1(zm char(8),sd datetime,ed datetime)
GO
insert 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 tb1
declare @t1 datetime,@t2 datetime,@zm char(8)
declare @tb table(zm char(8),d datetime)
open zm_cursor
fetch next from zm_cursor into @zm
while @@fetch_status=0
begin
select @t1=sd,@t2=ed from tb1 where zm=@zm
while @t1<=@t2
begin
insert into @tb values(@zm,@t1)
set @t1=dateadd(day,1,@t1)
end
fetch next from zm_cursor into @zm
end
select * from @tb
close zm_cursor
deallocate zm_cursor

GO

DROp TABLE TB1

有问题HI我

这个问题用标致函数比较难,1楼调用标致函数的方法也是错误的,正确的方法应该是:
SELECT * FROM DBO.CaculateDailyTM()

我用游标实现 ,比较好理解:

create table tb1(zm char(8),sd datetime,ed datetime)
GO
insert 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 tb1
declare @t1 datetime,@t2 datetime,@zm char(8)
declare @tb table(zm char(8),d datetime)
open zm_cursor
fetch next from zm_cursor into @zm
while @@fetch_status=0
begin
select @t1=sd,@t2=ed from tb1 where zm=@zm
while @t1<=@t2
begin
insert into @tb values(@zm,@t1)
set @t1=dateadd(day,1,@t1)
end
fetch next from zm_cursor into @zm
end
select * from @tb
close zm_cursor
deallocate zm_cursor

GO

DROp TABLE TB1

有问题HI我

这个问题用标致函数比较难,1楼调用标致函数的方法也是错误的,正确的方法应该是:
SELECT * FROM DBO.CaculateDailyTM()

我用游标实现 ,比较好理解:

create table tb1(zm char(8),sd datetime,ed datetime)
GO
insert 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 tb1
declare @t1 datetime,@t2 datetime,@zm char(8)
declare @tb table(zm char(8),d datetime)
open zm_cursor
fetch next from zm_cursor into @zm
while @@fetch_status=0
begin
select @t1=sd,@t2=ed from tb1 where zm=@zm
while @t1<=@t2
begin
insert into @tb values(@zm,@t1)
set @t1=dateadd(day,1,@t1)
end
fetch next from zm_cursor into @zm
end
select * from @tb
close zm_cursor
deallocate zm_cursor

GO

DROp TABLE TB1

有问题HI我

这个问题用标致函数比较难,1楼调用标致函数的方法也是错误的,正确的方法应该是:
SELECT * FROM DBO.CaculateDailyTM()

我用游标实现 ,比较好理解:

create table tb1(zm char(8),sd datetime,ed datetime)
GO
insert 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 tb1
declare @t1 datetime,@t2 datetime,@zm char(8)
declare @tb table(zm char(8),d datetime)
open zm_cursor
fetch next from zm_cursor into @zm
while @@fetch_status=0
begin
select @t1=sd,@t2=ed from tb1 where zm=@zm
while @t1<=@t2
begin
insert into @tb values(@zm,@t1)
set @t1=dateadd(day,1,@t1)
end
fetch next from zm_cursor into @zm
end
select * from @tb
close zm_cursor
deallocate zm_cursor

GO

DROp TABLE TB1

有问题HI我

这个问题用标致函数比较难,1楼调用标致函数的方法也是错误的,正确的方法应该是:
SELECT * FROM DBO.CaculateDailyTM()

我用游标实现 ,比较好理解:

create table tb1(zm char(8),sd datetime,ed datetime)
GO
insert 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 tb1
declare @t1 datetime,@t2 datetime,@zm char(8)
declare @tb table(zm char(8),d datetime)
open zm_cursor
fetch next from zm_cursor into @zm
while @@fetch_status=0
begin
select @t1=sd,@t2=ed from tb1 where zm=@zm
while @t1<=@t2
begin
insert into @tb values(@zm,@t1)
set @t1=dateadd(day,1,@t1)
end
fetch next from zm_cursor into @zm
end
select * from @tb
close zm_cursor
deallocate zm_cursor

GO

DROp TABLE TB1

有问题HI我

这个问题用标致函数比较难,1楼调用标致函数的方法也是错误的,正确的方法应该是:
SELECT * FROM DBO.CaculateDailyTM()

我用游标实现 ,比较好理解:

create table tb1(zm char(8),sd datetime,ed datetime)
GO
insert 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 tb1
declare @t1 datetime,@t2 datetime,@zm char(8)
declare @tb table(zm char(8),d datetime)
open zm_cursor
fetch next from zm_cursor into @zm
while @@fetch_status=0
begin
select @t1=sd,@t2=ed from tb1 where zm=@zm
while @t1<=@t2
begin
insert into @tb values(@zm,@t1)
set @t1=dateadd(day,1,@t1)
end
fetch next from zm_cursor into @zm
end
select * from @tb
close zm_cursor
deallocate zm_cursor

GO

DROp TABLE TB1

有问题HI我

这个问题用标致函数比较难,1楼调用标致函数的方法也是错误的,正确的方法应该是:
SELECT * FROM DBO.CaculateDailyTM()

我用游标实现 ,比较好理解:

create table tb1(zm char(8),sd datetime,ed datetime)
GO
insert 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 tb1
declare @t1 datetime,@t2 datetime,@zm char(8)
declare @tb table(zm char(8),d datetime)
open zm_cursor
fetch next from zm_cursor into @zm
while @@fetch_status=0
begin
select @t1=sd,@t2=ed from tb1 where zm=@zm
while @t1<=@t2
begin
insert into @tb values(@zm,@t1)
set @t1=dateadd(day,1,@t1)
end
fetch next from zm_cursor into @zm
end
select * from @tb
close zm_cursor
deallocate zm_cursor

GO

DROp TABLE TB1

有问题HI我

这个问题用标致函数比较难,1楼调用标致函数的方法也是错误的,正确的方法应该是:
SELECT * FROM DBO.CaculateDailyTM()

我用游标实现 ,比较好理解:

create table tb1(zm char(8),sd datetime,ed datetime)
GO
insert 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 tb1
declare @t1 datetime,@t2 datetime,@zm char(8)
declare @tb table(zm char(8),d datetime)
open zm_cursor
fetch next from zm_cursor into @zm
while @@fetch_status=0
begin
select @t1=sd,@t2=ed from tb1 where zm=@zm
while @t1<=@t2
begin
insert into @tb values(@zm,@t1)
set @t1=dateadd(day,1,@t1)
end
fetch next from zm_cursor into @zm
end
select * from @tb
close zm_cursor
deallocate zm_cursor

GO

DROP TABLE TB1

有问题HI我
全部回答
select dbo.CaculateDailyTM(站名, 开始时间,结束时间) from 表1
这个问题用标致函数比较难,1楼调用标致函数的方法也是错误的,正确的方法应该是: SELECt * FROM DBO.CaculateDailyTM() 我用游标实现 ,比较好理解: create table tb1(zm char(8),sd datetime,ed datetime) GO insert 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 tb1 declare @t1 datetime,@t2 datetime,@zm char(8) declare @tb table(zm char(8),d datetime) open zm_cursor fetch next from zm_cursor into @zm while @@fetch_status=0 begin select @t1=sd,@t2=ed from tb1 where zm=@zm while @t1<=@t2 begin insert into @tb values(@zm,@t1) set @t1=dateadd(day,1,@t1) end fetch next from zm_cursor into @zm end select * from @tb close zm_cursor deallocate zm_cursor GO DROp TABLE TB1 有问题HI我
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
武器太难锻造了,请问300独立 能提升多少
兔子为什么不会摇尾巴
甲乙丙共种60棵树.甲树棵数是2人的二分之一,
成语己饥己溺的意思是什么啊?有知道释义的请
梦幻里活动是按平均等级吗?
权力的游戏游戏操作键位一览 游戏怎么操作
运行安装程序时发生(-5006 :0x800700
国家拍卖粮食如何升贴水
一箭双雕的意思和造句
椎体骨破坏与椎间隙变窄并存时,应考虑哪种疾
幼儿园中班社会民间艺术了解剪纸脸谱的教学备
古代“六艺”中的“御”是指什么?
怎么注销陌陌账号
有个人拿着四万和七万麻将去买车那个笑话怎么
天露水洗浴会所在五家渠的什么位置
推荐资讯
《海商王2》进入时黑屏怎么回事?
车跑3600顶气门了,修过后会有影响吗?
成语议论纷纷的意思是什么啊?有知道释义的请
成语身在林泉,心怀魏阙的意思是什么啊?有知
已知在三角形ABC中,角A=60度,角B=45度,AC=2,
厦门大学基金楼怎么走?
电脑玩游戏正常!看电影花屏?
成都社保编码02开头是属于哪个区的? 想补交3,
长得像羊又像狼的动物是什么
请问贴吧里面总说的光谱,hb1,pb,ib1~~~都
成语除恶务尽的意思是什么啊?有知道释义的请
营口天翔汽车贸易有限公司地址在什么地方,我
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?