Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Getting Last Record and First Record from a Record Set (SQL server DB)

Getting Last Record and First Record from a Record Set (SQL server DB)

Scheduled Pinned Locked Moved Database
databasesql-serversysadmindata-structurestutorial
4 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 11809903
    wrote on last edited by
    #1

    I am pretty new using MS SQL. I perform a query over some SQL server table; it returns a record set; but I need to read the last record just to get the value of a certain column. This is my procedure:

    $connectionInfo = array( "Database"=>$myDB, "UID"=>$myUser , "PWD"=>$myPass);
    $conn = sqlsrv_connect( $myServer, $connectionInfo);

    $query = "SELECT * FROM( SELECT col0, col1, col2, col3 FROM t1 LEFT JOIN t2 ON t1.col1 = t2.col1 LEFT JOIN t3 ON t1.col2 = t3.col2) AS t ORDER BY t.col0; ";
    $rec_set = sqlsrv_query($connection, $query, array(), array("Scrollable" => 'buffered'));

    if( $rec_set === false ) die( print_r( sqlsrv_errors(), true));

    $connectionInfo = array( "Database"=>$myDB, UID"=>$myUser , "PWD"=>$myPass);
    $conn = sqlsrv_connect( $myServer, connectionInfo);

    $query = "SELECT * FROM( SELECT col0, col1, col2, col3 FROM t1 LEFT JOIN t2 ON t1.col1 = t2.col1 LEFT JOIN t3 ON t1.col2 = t3.col2) AS t ORDER BY t.col0; ";

    $rec_set = sqlsrv_query($connection, $query, array(), array("Scrollable" => 'buffered'));
    if( $rec_set === false ) die( print_r( sqlsrv_errors(), true));

    $row_count = sqlsrv_num_rows($rec_set);
    if ($row_count === false) die( print_r( sqlsrv_errors(), true));

    $reg = sqlsrv_fetch_array( $rec_set, SQLSRV_FETCH_ASSOC, 0);
    $first = $reg['column_name'];

    $reg = sqlsrv_fetch_array( $rec_set, SQLSRV_FETCH_ASSOC, $row_count - 1);
    $last = $reg['column_name'];

    It is clear that the sqlsrv_fetch_array DOES NOT work like that. Does anyone know how to achieve my goal, that is, getting last and first records from that record set without generating another SELECT? Thank you in advance.

    Richard Andrew x64R 1 Reply Last reply
    0
    • U User 11809903

      I am pretty new using MS SQL. I perform a query over some SQL server table; it returns a record set; but I need to read the last record just to get the value of a certain column. This is my procedure:

      $connectionInfo = array( "Database"=>$myDB, "UID"=>$myUser , "PWD"=>$myPass);
      $conn = sqlsrv_connect( $myServer, $connectionInfo);

      $query = "SELECT * FROM( SELECT col0, col1, col2, col3 FROM t1 LEFT JOIN t2 ON t1.col1 = t2.col1 LEFT JOIN t3 ON t1.col2 = t3.col2) AS t ORDER BY t.col0; ";
      $rec_set = sqlsrv_query($connection, $query, array(), array("Scrollable" => 'buffered'));

      if( $rec_set === false ) die( print_r( sqlsrv_errors(), true));

      $connectionInfo = array( "Database"=>$myDB, UID"=>$myUser , "PWD"=>$myPass);
      $conn = sqlsrv_connect( $myServer, connectionInfo);

      $query = "SELECT * FROM( SELECT col0, col1, col2, col3 FROM t1 LEFT JOIN t2 ON t1.col1 = t2.col1 LEFT JOIN t3 ON t1.col2 = t3.col2) AS t ORDER BY t.col0; ";

      $rec_set = sqlsrv_query($connection, $query, array(), array("Scrollable" => 'buffered'));
      if( $rec_set === false ) die( print_r( sqlsrv_errors(), true));

      $row_count = sqlsrv_num_rows($rec_set);
      if ($row_count === false) die( print_r( sqlsrv_errors(), true));

      $reg = sqlsrv_fetch_array( $rec_set, SQLSRV_FETCH_ASSOC, 0);
      $first = $reg['column_name'];

      $reg = sqlsrv_fetch_array( $rec_set, SQLSRV_FETCH_ASSOC, $row_count - 1);
      $last = $reg['column_name'];

      It is clear that the sqlsrv_fetch_array DOES NOT work like that. Does anyone know how to achieve my goal, that is, getting last and first records from that record set without generating another SELECT? Thank you in advance.

      Richard Andrew x64R Offline
      Richard Andrew x64R Offline
      Richard Andrew x64
      wrote on last edited by
      #2

      Have you tried:

      SELECT TOP 1 COL1 FROM TABLE ORDER BY COL1

      and then

      SELECT TOP 1 COL1 FROM TABLE ORDER BY COL1 DESC

      The difficult we do right away... ...the impossible takes slightly longer.

      U 1 Reply Last reply
      0
      • Richard Andrew x64R Richard Andrew x64

        Have you tried:

        SELECT TOP 1 COL1 FROM TABLE ORDER BY COL1

        and then

        SELECT TOP 1 COL1 FROM TABLE ORDER BY COL1 DESC

        The difficult we do right away... ...the impossible takes slightly longer.

        U Offline
        U Offline
        User 11809903
        wrote on last edited by
        #3

        No. The problem here is that I have worked with mysql/postresql and I can retrieve from a recordset any record I want depending on its position in the recordset. So when you have: $var = pg_fetch_result($recordset, $k, 'col'); you are retrieving the 'col' value in the k-th record from that recordset (in PostgreSQL). You can do the same in MySQL. I want to achieve the same in sql server... I have noticed this parameter en the fetch of sql: SQLSRV_SCROLL_LAST in the row parameter of the sqlsrv_fetch_array(resource $stmt, int $fetchType = ?, int $row = ?, int $offset = ?). But for some reason I can not make it run.

        L 1 Reply Last reply
        0
        • U User 11809903

          No. The problem here is that I have worked with mysql/postresql and I can retrieve from a recordset any record I want depending on its position in the recordset. So when you have: $var = pg_fetch_result($recordset, $k, 'col'); you are retrieving the 'col' value in the k-th record from that recordset (in PostgreSQL). You can do the same in MySQL. I want to achieve the same in sql server... I have noticed this parameter en the fetch of sql: SQLSRV_SCROLL_LAST in the row parameter of the sqlsrv_fetch_array(resource $stmt, int $fetchType = ?, int $row = ?, int $offset = ?). But for some reason I can not make it run.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Add a "row number"? [ROW_NUMBER (Transact-SQL) - SQL Server | Microsoft Learn](https://learn.microsoft.com/en-us/sql/t-sql/functions/row-number-transact-sql?view=sql-server-ver16#general-remarks)

          "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups