sql預存程序迴圈while

來源:互聯網
上載者:User

create procedure [擁有者.]預存程序名[;程式編號]   [(參數#1,…參數#1024)]   [with   {recompile | encryption | recompile, encryption}   ]   [for replication]   as 程式行   其中預存程序名不能超過128個字。每個預存程序中最多設定1024個參數   (sql server 7.0以上版本),參數的使用方法如下:   @參數名 資料類型 [varying] [=內定值] [output]   每個參數名前要有一個“@”符號,每一個預存程序的參數僅為該程式內部使用,參數的類型除了image外,其他sql server所支援的資料類型都可使用。

 

declare @mycounter int
set @mycounter = 0 /*設定變數*/
while (@mycounter < 2) /*設定迴圈次數*/
begin
waitfor delay '000:00:10' /*延遲時間10秒*/
insert into time_by_day
(time_id, the_date, the_year, month_of_year, quarter, day_of_month)
select top 1 time_id + 1 as time_id, the_date + 1 as the_date, year(the_date + 1)
as the_year, month(the_date + 1) as month_of_year, { fn quarter(the_date + 1)
} as quarter, day(the_date + 1) as day_of_month
from time_by_day
order by time_id desc

set @mycounter = @mycounter + 1
end

 

用遊標吧

declare @a int,@b int
declare ccc cursor for select * from test where id>@id
open ccc
fetch next from ccc into @a,@b
while (@@fetch_status=0)
begin
.......
end
close ccc
declare ccc(這個操作看看別的系統預存程序怎麼寫的 記不太清了 每次都是copy的)

大概就是這樣 不知道文法有沒有錯誤 最近一段時間沒用sqlserver了 看看系統預存程序就能明白怎麼用cursor了

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.