Search code examples
sql-server-2008-express

Retrieving only primary key column numbers from article_tbl and checking against?


Would like to figure out how to better retrieve data from database without performance cost.

Plan as follows:

  1. Select id from article table;
  2. store ids in List<int> arr;
  3. find out last article id. int x = arr.Count()
  4. Select * from article_tbl where id = x; Run query.
  5. Post it on you page.

Am I planning right? What is better way of retrieving data from database?

Thanks a lot


Solution

  • Try something like this - you can call it "ad-hoc" or wrap it up in a stored procedure:

    -- get the "latest" ID from the "Article" latest
    -- but you need to define *latest* by WHAT criteria?? A date?? The ID itself??
    DECLARE @LastID INT
    
    SELECT TOP 1 @LastID = ID
    FROM dbo.Article
    ORDER BY ..........  -- order by date? id? what??
    
    -- get the detail data for that ID from the "Article_tbl"
    SELECT (list of columns)
    FROM dbo.Article_tbl 
    WHERE ID = @LastID