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了