SQL Select Question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Boxwar
    New Member
    • Mar 2007
    • 7

    SQL Select Question

    Hello,

    Another question, which probably would be very simple.
    How can I Select the record which is one before the last record ?
  • Doranj00
    New Member
    • Apr 2007
    • 5

    #2
    You need to be more explicit with what you are trying to accomplish. Tables have a natural record order, Oracle has rownum and rowid but MS SQL does not.

    Look at this article:


    Q186133 INF: How to Dynamically Number Rows in a Select Statement

    http://support.microso ft.com/default.aspx?sc id=KB;EN-US;q186133

    Comment

    • Boxwar
      New Member
      • Mar 2007
      • 7

      #3
      That article is interesting, thank you.
      I am trying to select the last 20 records of my table, but each of them has to be selected separately. How can i do this ?
      I am using SQL Server 2000. My table consists of a primary key and the rest are normal columns. The primary key is numeric and the table is sorted on that column.
      I am not sure what to add else, if there is anything you need to know, please ask.

      Comment

      • Paru
        New Member
        • Jul 2007
        • 2

        #4
        Firstly create a Temporary table & insert all values from Existing table into Temporary table.Then create a cursor & Fetch the second last record from table.



        create table #temp(sal int,name varchar(10))

        declare cursor_sample cursor scroll for
        select Emp_Salary from Employee_tbl where Emp_Salary>6000

        open cursor_sample

        declare @sal int
        declare @name varchar(50)

        Fetch Absolute -2 from cursor_sample into @sal
        print @sal

        begin
        select @sal=Emp_Salary ,@name=Emp_Name from Employee_tbl where Emp_Salary=@sal
        insert into #temp values(@sal,@na me)

        end

        deallocate cursor_sample

        select * from #temp

        truncate table #temp

        Comment

        Working...