以前曾經在asp.net實現記錄排序功能,參考:前端實現,寫得很複雜。
完整記錄排序觸發器**(更新記錄),
setansi_nulls
ongo
setquoted_identifier
ongo
ifexists
(select
[name
]from
sysobjects
where
[name]=
'tri_orderdemo_update
'and
type ='
tr')drop
trigger
[dbo].
[tri_orderdemo_update]go
create
trigger
[dbo].
[tri_orderdemo_update]on
[dbo].
[orderdemo
]for
update
as--
宣告變數,儲存更新記錄的主鍵,[num]欄位的舊值,新值,以及所有記錄的[num]欄位的最大值與最小值,最小值設為1
declare
@pkint
,@oldvalue
int,
@newvalue
int,
@maxvalue
int,
@minvalue
int=1--
從deleted虛擬表找主鍵和[num]欄位到舊值
select
@pk=
[orderdemoid],
@oldvalue=[
num]
from
deleted
--從inserted虛擬表找[num]欄位到新值
select
@newvalue=[
num]
from
inserted
--從表[ordedemo]找到最大值
select
@maxvalue
=max([
num]
) from
[dbo].
[orderdemo
]begin
--由大的排序號,排至小的排序號if(
@newvalue
<
@oldvalue
)update
[dbo].
[orderdemo
]set
[num]=
[num]+
1where
[orderdemoid]in
(select
[orderdemoid
]from
[dbo].
[orderdemo
]where
[num
]between
@newvalue
and(
@oldvalue-1
)) and
[orderdemoid
]<>
@pk--
由小的排序號,排至大的排序號
elseif(
@newvalue
>
@oldvalue
)update
[dbo].
[orderdemo
]set
[num]=
[num]-
1where
[orderdemoid]in
(select
[orderdemoid
]from
[dbo].
[orderdemo
]where
[num
]between
(@oldvalue+1
) and
@newvalue
) and
[orderdemoid
]<>
@pkelse
--新值與舊值一樣,不作更新@newvalue = @oldvalue
return
end
刪除記錄觸發器,
setansi_nulls
ongo
setquoted_identifier
ongo
ifexists
(select
[name
]from
sysobjects
where
[name]=
'tri_orderdemo_delete
'and
type ='
tr')drop
trigger
[dbo].
[tri_orderdemo_delete]go
create
trigger
[dbo].
[tri_orderdemo_delete]on
[dbo].
[orderdemo
]for
delete
as--
宣告變數,儲存刪除記錄的[num]欄位的值
declare
@value
int,
@maxvalue
int--
從deleted虛擬表找主鍵和[num]字段值
select
@value=[
num]
from
deleted
select
@maxvalue
=max([
num]
) from
[dbo].
[orderdemo
]beginif(
@value
<
@maxvalue
)update
[dbo].
[orderdemo
]set
[num]=
[num]-
1where
[orderdemoid]in
(select
[orderdemoid
]from
[dbo].
[orderdemo
]where
[num
]between
@value+1
and@maxvalue
)end
實現記錄排序
以前曾經在asp.net實現記錄排序功能,參考 前端實現,寫得很複雜。完整記錄排序觸發器 更新記錄 setansi nulls ongo setquoted identifier ongo ifexists select name from sysobjects where name tri ord...
演算法實現記錄 希爾排序
簡而言之,這是基於插入排序的一種改進版本。原理就是通過選取步長 間隔 為乙個變常數的值得插入排序,來將陣列變得有序 而我們知道,插入排序對於有序資料的處理效果是很好的。按照這個思路,實現如下 include include include include using namespace std vo...
檔案記錄排序
你現在有乙個檔案,檔案中順序存有 n個記錄,r1,r2,rn,這些記錄不是有序的,但是你知道乙個整數 m,這些記錄滿足 r1以及 rm 1 1,設計乙個演算法或編寫乙個程式,將檔案中的記錄排序為 r1 演算法或程式讀取檔案的次數為 o n 不限記憶體使用,2,設計乙個演算法或編寫乙個程式,將檔案中的...