SQL Queries

  • Can someone tell me the T-SQL code that I can use to retrieve rows in order that they are entered into a table? Many thanks in advance.

  • A table, by definition, has no order - so, there is no way to return rows in the order they were entered.

    The only way you can write a query in the order of insertion is if you have a column that records when the row was inserted. Ideally, that would be a date column or an identity - although either will not necessarily give you the exact order of insertion. SQL Server can choose to re-order the rows inserted in a batch - if needed.

    Jeffrey Williams
    “We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”

    ― Charles R. Swindoll

    How to post questions to get better answers faster
    Managing Transaction Logs

  • Thanks so much Jeff for your prompt and valuable response. Another question arise,

    In a table with an identity column (CustID for instance), what query can I use to select the CustID for a newly entered Customer?

    Once again, thanks.

  • sahoong (7/19/2008)


    In a table with an identity column (CustID for instance), what query can I use to select the CustID for a newly entered Customer?

    Hi,

    If you have an identity column on a table, using the function SCOPE_IDENTITY() will return the value of the most recently inserted record (within the current scope)

    SELECT SCOPE_IDENTITY() AS [SCOPE_IDENTITY]

    Refer to the following linke for further details:

    http://msdn.microsoft.com/en-us/library/ms190315.aspx

    Hope this helps but of course please feel free to pose any further questions you may have.

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply