要求我输入产品ID和一个数字,
比如产品ID为5,数字为3我想查出5后面第3位的产品ID,
和5前面第3位的产品ID,
不要存储过程,SQL语句就可以了,

解决方案 »

  1.   

    declare @num int
    select * from a where sno=@num-3 or SNO=@num+3
      

  2.   

    declare @num int
    select * from a where sno between @num-3 and SNO=@num+3
      

  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  
    */
      

  4.   

    '这样看起来舒服些...'
    --------------------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   
    */
      

  5.   

    2005利用row_number 生成的CTE 进行筛选是效率最高的
    2000导入临时表
      

  6.   

    不用2005,要能同时兼容2000和ACC的代码
      

  7.   

    declare @num=5 int 
    select * from table where id=@num+3
    select * from table where id=@num-3就这样