declare @num int select * from a where sno=@num-3 or SNO=@num+3
declare @num int select * from a where sno between @num-3 and SNO=@num+3
--------------------SQL Server数据格式化工具------------------- --------------------------------------------------------------- -- DESIGNER :happycell188(喜喜) -- QQ :584738179 -- Development Tool :Microsoft Visual C++ 6.0 C Language -- FUNCTION :CONVERT DATA TO T-SQL --------------------------------------------------------------- -- Microsoft SQL Server 2005 -- Developer Edition on Microsoft Windows XP [版本 5.1.2600] --------------------------------------------------------------- ---------------------------------------------------------------use test go if object_id('test.dbo.tb') is not null drop table tb -- 创建数据表 create table tb ( ID char(3), jno char(3) ) go --插入测试数据 insert into tb select '1','j1' union all select '4','j3' union all select '3','j4' union all select '7','j3' union all select '5','j1' union all select '9','j2' union all select '8','j1' go --代码实现 if object_id('test.dbo.proc_test') is not null drop proc proc_test go create proc proc_test @idinput varchar(10), @i int as begin declare @idd varchar(10) if object_id('test.dbo.#t') is not null drop table #t select idd=row_number()over(order by getdate()),* into #t from tb select @idd=idd from #t where ID=@idinput select ID from #t where idd=@idd+3 or idd=@idd-3 end go--> 测试存储过程 exec proc_test 5,3/* ID ------- 4 */exec proc_test 7,3/* ID ------- 1 8 */
'这样看起来舒服些...' --------------------SQL Server数据格式化工具------------------- --------------------------------------------------------------- -- DESIGNER :happycell188(喜喜) -- QQ :584738179 -- Development Tool :Microsoft Visual C++ 6.0 C Language -- FUNCTION :CONVERT DATA TO T-SQL --------------------------------------------------------------- -- Microsoft SQL Server 2005 -- Developer Edition on Microsoft Windows XP [版本 5.1.2600] --------------------------------------------------------------- ---------------------------------------------------------------use test go if object_id('test.dbo.tb') is not null drop table tb -- 创建数据表 create table tb ( ID char(3), jno char(3) ) go --插入测试数据 insert into tb select '1','j1' union all select '4','j3' union all select '3','j4' union all select '7','j3' union all select '5','j1' union all select '9','j2' union all select '8','j1' go --代码实现 if object_id('test.dbo.proc_test') is not null drop proc proc_test go create proc proc_test @idinput varchar(10), @i int as begin declare @idd varchar(10) if object_id('test.dbo.#t') is not null drop table #t select idd=row_number()over(order by getdate()),* into #t from tb select @idd=idd from #t where ID=@idinput select ID from #t where idd=@idd+3 or idd=@idd-3 end go--> 测试存储过程 exec proc_test 5,3/* ID ------- 4 */exec proc_test 7,3/* ID ------- 1 8 */
2005利用row_number 生成的CTE 进行筛选是效率最高的 2000导入临时表
不用2005,要能同时兼容2000和ACC的代码
declare @num=5 int select * from table where id=@num+3 select * from table where id=@num-3就这样
select * from a where sno=@num-3 or SNO=@num+3
select * from a where sno between @num-3 and SNO=@num+3
---------------------------------------------------------------
-- DESIGNER :happycell188(喜喜)
-- QQ :584738179
-- Development Tool :Microsoft Visual C++ 6.0 C Language
-- FUNCTION :CONVERT DATA TO T-SQL
---------------------------------------------------------------
-- Microsoft SQL Server 2005
-- Developer Edition on Microsoft Windows XP [版本 5.1.2600]
---------------------------------------------------------------
---------------------------------------------------------------use test
go
if object_id('test.dbo.tb') is not null drop table tb
-- 创建数据表
create table tb
(
ID char(3),
jno char(3)
)
go
--插入测试数据
insert into tb select '1','j1'
union all select '4','j3'
union all select '3','j4'
union all select '7','j3'
union all select '5','j1'
union all select '9','j2'
union all select '8','j1'
go
--代码实现
if object_id('test.dbo.proc_test') is not null drop proc proc_test
go
create proc proc_test
@idinput varchar(10),
@i int
as
begin
declare @idd varchar(10)
if object_id('test.dbo.#t') is not null drop table #t
select idd=row_number()over(order by getdate()),* into #t from tb
select @idd=idd from #t where ID=@idinput
select ID from #t where idd=@idd+3 or idd=@idd-3
end
go--> 测试存储过程
exec proc_test 5,3/*
ID
-------
4
*/exec proc_test 7,3/*
ID
-------
1
8
*/
--------------------SQL Server数据格式化工具-------------------
---------------------------------------------------------------
-- DESIGNER :happycell188(喜喜)
-- QQ :584738179
-- Development Tool :Microsoft Visual C++ 6.0 C Language
-- FUNCTION :CONVERT DATA TO T-SQL
---------------------------------------------------------------
-- Microsoft SQL Server 2005
-- Developer Edition on Microsoft Windows XP [版本 5.1.2600]
---------------------------------------------------------------
---------------------------------------------------------------use test
go
if object_id('test.dbo.tb') is not null drop table tb
-- 创建数据表
create table tb
(
ID char(3),
jno char(3)
)
go
--插入测试数据
insert into tb select '1','j1'
union all select '4','j3'
union all select '3','j4'
union all select '7','j3'
union all select '5','j1'
union all select '9','j2'
union all select '8','j1'
go
--代码实现
if object_id('test.dbo.proc_test') is not null drop proc proc_test
go
create proc proc_test
@idinput varchar(10),
@i int
as
begin
declare @idd varchar(10)
if object_id('test.dbo.#t') is not null drop table #t
select idd=row_number()over(order by getdate()),* into #t from tb
select @idd=idd from #t where ID=@idinput
select ID from #t where idd=@idd+3 or idd=@idd-3
end
go--> 测试存储过程
exec proc_test 5,3/*
ID
-------
4
*/exec proc_test 7,3/*
ID
-------
1
8
*/
2000导入临时表
select * from table where id=@num+3
select * from table where id=@num-3就这样