比方有一商品表commodity
編號 名稱 其他列...
1 a
2 b
3 c
4 x
5 t
6 a
7 b
如果給定 編號(id) 為 3 需要取 4條記錄 ,那麼應該返回 id:1,2,4,5 四條記錄
如果給定 id=2 那麼返回 id: 1,3,4,5
如果給定 id=7 那麼返回 id: 3,4,5,7
**如下(sql2000)
set quoted_identifier on
goset ansi_nulls on go
alter proc commodity_queryrelative
@id int,
@num int =4
asdeclare @sql nvarchar(2000)
declare @highnum int
declare @lownum int
declare @username nvarchar(50)
set @sql=''
set @highnum=0
set @lownum=''
set @username=''
select @username=username from [commodity] where [id]=@id
select @highnum=count(id) from [commodity] where [id]> @id and username=@username
select @lownum=count(id) from [commodity] where [id]< @id and username=@username
declare @margin int
declare @halfnum int
set @halfnum=@num/2
set @margin =0
if @highnum < @halfnum and @lownum <@halfnum
begin
set @sql='select top '+ cast(@halfnum as nvarchar) +' [id], [title], [keys], [classid], [classidpath], [classname], [classnamepath], [userclassid], [userclassname],[img], [thumimg], [areaid], [areaname], [address], [linkman], [updatetime],[addtime]
from commodity
where username='''+ @username + ''' and id !=' + cast(@id as nvarchar)
endelse
begin
declare @lessnum int
declare @largenum int
set @lessnum=0
set @largenum=0
if @highnum >=@halfnum and @lownum >=@halfnum
begin
set @lessnum=@halfnum
set @largenum=@halfnum
endif @highnum >= @halfnum and @lownum< @halfnum
begin
set @lessnum=@halfnum
set @largenum=@num-@lownum
endif @highnum < @halfnum and @lownum >= @halfnum
begin
set @lessnum=@num-@highnum
set @largenum=@halfnum
endset @sql='select * from
(select top '+ cast(@lessnum as nvarchar) +' [id], [title], [keys], [classid], [classidpath], [classname], [classnamepath], [userclassid], [userclassname],[img], [thumimg], [areaid], [areaname], [address], [linkman], [updatetime],[addtime]
from commodity
where username='''+ @username + ''' and id <' + cast(@id as nvarchar) +' order by id desc
) as t1
union
select * from
( select top '+ cast(@largenum as nvarchar) +' [id], [title], [keys], [classid], [classidpath], [classname], [classnamepath], [userclassid], [userclassname],[img], [thumimg], [areaid], [areaname], [address], [linkman], [updatetime],[addtime]
from commodity
where username='''+ @username + ''' and id >' + cast(@id as nvarchar) +'
) as t2 '
endexec(@sql)
goset quoted_identifier off
goset ansi_nulls on
go
TSQL查詢給定記錄的相關資料
具體環境為sql 2000 在一內容表中給定一條記錄,查尋編號大於這裡記錄的的資料,具體 如下 create proc cmst queryinterrelated id int,給定的記錄的主鍵 num int 需要條數 asdeclare rnum int declare sql nvarcha...
每日一記,養成記錄的習慣
每個事物都有她在時間維度上的軌跡,當我們在適當的節點上留下一些痕跡,時間的軌跡就變得有形。養成作些記錄的習慣,是有必要而且非常有意義的。記錄的方式有很多,幾乎有日曆功能的地方就有記事備忘的功能,我們的辦公系統也必然有日誌記錄功能。這些功能為我們作筆記或者說留下痕跡提供了很大的便利,可以好好加以利用。...
與敏捷相關的概念(第一記)
1.什麼是敏捷?敏捷 是描述一些軟體開發方法學的新的標誌性詞彙。敏捷這一概念的出現,其實是源於瀑布開發方法對我們在實際開發中帶來的一系列不好的影響。使我們反思是否有一種更適合專案開發的方法,而不需要從一開始就將需求,設計都定死 而是採用一種更加靈活,迭代的方式去進行我們的專案開發。敏捷過程,是一種十...