MS SQL Server
  Home arrow MS SQL Server arrow Page 3 - 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 
Mobile Linux 
App Generation ROI 
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 - How to code the SELECT clause


    (Page 3 of 6 )


     
    Figure 3-3 presents an expanded syntax for the SELECT clause. The keywords shown in the first line allow you to restrict the rows that are returned by a query. You’ll learn how to code them in a few minutes. First, though, you’ll learn various techniques for identifying which columns are to be included in a result set.

    How to code column specifications
     
    Figure 3-3 summarizes the techniques you can use to code column specifications. You saw how to use some of these techniques in the previous figure. For example, you can code an asterisk in the SELECT clause to retrieve all of the columns in the base table, and you can code a list of column names separated by commas. Note that when you code an asterisk, the columns are returned in the order that they occur in the base table.

    You can also code a column specification as an expression. For example, you can use an arithmetic expression to perform a calculation on two or more columns in the base table, and you can use a string expression to combine two or more string values. An expression can also include one or more functions. You’ll learn more about each of these techniques in the topics that follow.

    But first, you should know that when you code the SELECT clause, you should include only the columns you need. For example, you shouldn’t code an asterisk to retrieve all the columns unless you need all the columns. That’s because the amount of data that’s retrieved can affect system performance. This is particularly important if you’re developing SQL statements that will be used by application programs. 

    Figure 3-3.   How to code column specifications

    The expanded syntax of the SELECT clause

      SELECT [ALL|DISTINCT] [TOP n [PERCENT] [WITH TIES]]
        column_specification [[AS] result_column]
        [, column_specification [[AS] result_column]] ...

    Five ways to code column specifications  

    Column specifications that use base table values

    The * is used to retrieve all columns

      SELECT *

    Column names are used to retrieve specific columns

      SELECT VendorName, VendorCity, VendorState

    Column specifications that use calculated values

    An arithmetic expression is used to calculate BalanceDue

      SELECT InvoiceNumber,
          InvoiceTotal - PaymentTotal – CreditTotal AS BalanceDue

    A string expression is used to calculate FullName

      SELECT VendorContactFName + ' ' + VendorContactLName AS FullName

    A function is used to calculate CurrentDate

      SELECT InvoiceNumber, InvoiceDate,  
          GETDATE() AS CurrentDate

    Description

    1. Use SELECT * only when you need to retrieve all of the columns from a table. Otherwise, list the names of the columns you need.
    2. An expression is a combination of column names and operators that evaluate to a single value. In the SELECT clause, you can code arithmetic expressions, string expressions, and expressions that include one or more functions.
    3. After each column specification, you can code an AS clause to specify the name for the column in the result set. See figure 3-4 for details.

    Note

    • The other elements shown in the syntax summary above let you control the number of rows that are returned by a query. You can use the ALL and DISTINCT keywords to determine whether or not duplicate rows are returned. And you can use the TOP clause to retrieve a specific number or percent of rows. See figures 3-8 and 3-9 for details.

    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 5 hosted by Hostway
    Stay green...Green IT