亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

不知道各位大俠還有其他的什么好方法能實現嗎?

不知道各位大俠還有其他的什么好方法能實現嗎?

慕妹3146593 2022-10-20 15:15:58
大家好,我有一個數據表(如表1所示),我想對每個站在開始時間和結束時間之間插入時間段,步長為1天,得到的結果應該為表2所示,表1結構如下站名 開始時間 結束時間s1 1980-1-1 1980-2-1s2 1992-2-1 1993-3-1表2站名 時間s1 1980-1-1s1 1980-1-2s1 1980-1-3................s1 1980-2-1s2 1992-2-1s2 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)asbegindeclare @temptm datetimeset @temptm=@tminitwhile @temptm<=@tmendbegininsert into @mytbl select STCD,@temptmset @temptm=@temptm+1endreturnRETURNendGO
查看完整描述

2 回答

?
一只斗牛犬

TA貢獻1784條經驗 獲得超2個贊

這個問題用標致函數比較難,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


查看完整回答
反對 回復 2022-10-24
?
紅顏莎娜

TA貢獻1842條經驗 獲得超13個贊

select dbo.CaculateDailyTM(站名, 開始時間,結束時間) from 表1

查看完整回答
反對 回復 2022-10-24
  • 2 回答
  • 0 關注
  • 107 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號