MS SQL Server
  Home arrow MS SQL Server arrow Page 2 - How to Receive Data from a Single Table
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Moblin 
JMSL Numerical Library 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MS SQL SERVER

How to Receive Data from a Single Table
By: Murach Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 9
    2006-02-23

    Table of Contents:
  • How to Receive Data from a Single Table
  • SELECT statement examples
  • How to code the SELECT clause
  • How to name the columns in a result set
  • How to code string expressions
  • How to code arithmetic expressions

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    How to Receive Data from a Single Table - SELECT statement examples


    (Page 2 of 6 )


     
    Figure 3-2 presents five SELECT statement examples. All of these statements retrieve data from the Invoices table. If you aren’t already familiar with this table, you should use the Enterprise Manager as described in the last chapter to review its definition.

    The first statement in this figure retrieves all of the rows and columns from the Invoices table. Here, an asterisk (*) is used as a shorthand to indicate that all of the columns should be retrieved, and the WHERE clause is omitted so that there are no conditions on the rows that are retrieved. Notice that this statement doesn’t include an ORDER BY clause, so the rows are in primary key sequence. You can see the results following this statement as they’re displayed by the Query Analyzer. Notice that both horizontal and vertical scroll bars are displayed, indicating that the result set contains more rows and columns than can be displayed on the screen at one time.

    The second statement retrieves selected columns from the Invoices table. As you can see, the columns to be retrieved are listed in the SELECT clause. Like the first statement, this statement doesn’t include a WHERE clause, so all the rows are retrieved. Then, the ORDER BY clause causes the rows to be sorted by the InvoiceTotal column in descending sequence.

    The third statement also lists the columns to be retrieved. In this case, though, the last column is calculated from two columns in the base table, CreditTotal and PaymentTotal, and the resulting column is given the name TotalCredits. In addition, the WHERE clause specifies that only the invoice whose InvoiceID column has a value of 17 should be retrieved.

    The fourth SELECT statement includes a WHERE clause whose condition specifies a range of values. In this case, only invoices with invoice dates between 05/01/2002 and 05/31/2002 will be retrieved. In addition, the rows in the result set will be sorted by invoice date.

    The last statement in this figure shows another variation of the WHERE clause. In this case, only those rows with invoice totals greater than 50,000 are retrieved. Notice that since none of the rows in the Invoices table satisfy this condition, the result set is empty.

    Figure 3-2.   SELECT statement examples

    A SELECT statement that retrieves all the data from the Invoices table

      SELECT *
     FROM Invoices
     


      (114 rows)

    A SELECT statement that retrieves three columns from each row, sorted in descending sequence by invoice total

      SELECT InvoiceNumber, InvoiceDate, InvoiceTotal
      FROM Invoices
      ORDER BY InvoiceTotal DESC


      (114 rows)

    A SELECT statement that retrieves two columns and a calculated value for a specific invoice

      SELECT InvoiceID, InvoiceTotal, CreditTotal + PaymentTotal AS TotalCredits
      FROM Invoices


      WHERE InvoiceID = 17

    A SELECT statement that retrieves all invoices between given dates

      SELECT InvoiceNumber, InvoiceDate, InvoiceTotal
      FROM Invoices
      WHERE InvoiceDate BETWEEN '2002-05-01' AND '2002-05-31'
      ORDER BY InvoiceDate


      (70 rows)

    A SELECT statement that returns an empty result set

      SELECT InvoiceNumber, InvoiceDate, InvoiceTotal
      FROM Invoices
      WHERE InvoiceTotal > 50000

    More MS SQL Server Articles
    More By Murach Publishing


       · This article is an excerpt from the book "Murach's SQL for SQL Server," published by...
     

    Buy this book now. This article is excerpted from chapter three of the book Murach's SQL for SQL Server, written by Bryan Sylverson (Murach; ISBN: 1890774162). Check it out today at your favorite bookstore. Buy this book now.

    MS SQL SERVER ARTICLES

    - Completing the Introduction to Transact-SQL
    - A Brief Introduction to Transact-SQL
    - Lookups and Blocking Bad Data
    - Field Validation Rules for Blocking Bad Data
    - Using Masks to Block Bad Data
    - Blocking Bad Data
    - Using @@ROWCOUNT and TABLE Variables for Dat...
    - How to Use Variables, IF and CASE in Databas...
    - Creating Important Aspects of Notification S...
    - Working wth Variables in Database Interactio...
    - Delving Deeper into Notification Services
    - Notification Services
    - Building a Multi-table Report with SQL 2005 ...
    - A Secure Way of Building Connection Strings
    - Transferring a Database Using the SSIS Desig...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway