---定義開始和結束時間
declare
@st_dt
datetime
declare
@en_dt
datetime
---時間賦值
select
@st_dt='
20150601
'select
@en_dt='
20150930'--
-定義中間變數
declare
@dtdatetime
select
@dt=
@st_dt
---開始按照時間迴圈執行
while
@dt<=
@en_dt
begin
---這個是個演示,可以替換為需要執行的儲存過程
declare
@tel
varchar(50
),
@idvarchar(50
)declare subscribe_cursor cursor
--定義游標
for(
select
top360
convert(varchar(20),convert(decimal(20,0),f1)),id from sdata where tag=0)
open subscribe_cursor; --
開啟游標
fetch
next
from subscribe_cursor into
@tel,@id
while
@@fetch_status=0
begin
insert
into ls_recall(tel,recallplandate)values(@tel,@dt
)
update sdata set tag=
1where id=
@idfetch
next
from subscribe_cursor into
@tel,@id
;
endclose subscribe_cursor; --
關閉游標
deallocate subscribe_cursor; --
釋放游標
---中間變數加1
select
@dt=
dateadd(day,1,@dt
)
end--
-定義開始和結束時間
declare
@st_dt
datetime
declare
@en_dt
datetime
---時間賦值
select
@st_dt='
20150602
'select
@en_dt='
20150930'--
-定義中間變數
declare
@dtdatetime
select
@dt=
@st_dt
---開始按照時間迴圈執行
while
@dt<=
@en_dt
begin
---定義開始和結束工號
declare
@st_code
intdeclare
@en_code
int--
-工號賦值
select
@st_code=1
select
@en_code=18
---定義中間變數
declare
@code
intselect
@code
=@st_code
while
@code
<=
@en_code
begin
declare
@idvarchar(50
)
declare subscribe_cursor cursor
--定義游標
for(
select
top20 id from ls_recall where recallplandate=
@dtand empcode is
null
)
open subscribe_cursor; --
開啟游標
fetch
next
from subscribe_cursor into
@idwhile
@@fetch_status=0
begin
update ls_recall set empcode=
@code
where id=
@idfetch
next
from subscribe_cursor into
@id;
endclose subscribe_cursor; --
關閉游標
deallocate subscribe_cursor; --
釋放游標
---中間變數加1
select
@code
=@code+1
end--
-中間變數加1
select
@dt=
dateadd(day,1,@dt
)
end
sql 迴圈,游標
declare userid varchar 50 declare my cursor cursor 定義游標 for select userid from dbo.memberaccount 查出需要的集合放到游標中 open my cursor 開啟游標 fetch next from my c...
sql游標迴圈
declare begindate datetime convert datetime,2019.05.06 09 40 50 enddate datetime getdate idint declare my cursor cursor 定義游標 for select id from dbo.op...
SQL游標迴圈
begin 定義的這些變數就是以下查出的結果的字段數,一一對應 declare cid int,cno nvarchar 64 cname nvarchar 256 debtamount decimal begin 定義游標.declare vernier main cursor fast forw...