SunQuest
 
       MS SQL Server
  Home arrow MS SQL Server arrow Page 2 - Working wth Variables in Database Interact...
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 
Actuate Whitepapers 
VeriSign Whitepapers 
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

Working wth Variables in Database Interactions with Transact-SQL
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 6
    2007-03-19

    Table of Contents:
  • Working wth Variables in Database Interactions with Transact-SQL
  • Retrieving data from a database using a T-SQL script
  • Common scenarios while retrieving data from a database into variables
  • Using T-SQL variables efficiently: continued

  • 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Working wth Variables in Database Interactions with Transact-SQL - Retrieving data from a database using a T-SQL script


    (Page 2 of 4 )

    Let us consider the following example:

    use Northwind
    go

    declare @EmpID int
    set @EmpID = 4

    select LastName + ',' + FirstName
    from dbo.Employees
    where EmployeeID = @EmpID

    go

    In the above script, I am asking the script to work within the context of the  "Northwind" database. If you are already connected to the same database, this may not be necessary. But it is always a good practice to include the "use" statement to switch to the respective database context you require.

    The word "go," in simple terms, executes all the previous statement(s) immediately as a "batch." Going further down, we have the following:

    declare @EmpID int
    set @EmpID = 4

    The above two statements simply declare and initialize a new variable named "@EmpID" (of type "int") with a value of 4. Every variable you use in the script must be declared prior to its usage and every variable must be preceded with the "@" symbol. Going further down, we have the following:

    select LastName + ',' + FirstName
    from dbo.Employees
    where EmployeeID = @EmpID

    The above SELECT statement simply retrieves the name (combined value of LastName and FirstName separated with comma) from the Employees table based on the EmployeeID available in the variable @EmpID.

    Retrieving data from a database into variables using a T-SQL script

    In the previous example, we directly used the SELECT statement to retrieve and display the information to the user. In general, when we are working with scripts, we may need to retrieve the values of the SELECT statement into variables and use them extensively as part of the entire script.

    Let us consider the following example:

    use Northwind
    go
    declare @ename varchar(20)
    declare @EmpID int
    set @EmpID = 4

    select @ename = LastName + ',' + FirstName
    from dbo.Employees
    where EmployeeID = @EmpID

    print 'Name of the Employee:' + @ename
    go

    In the above example, I declared one more variable, "@ename" of type string. You can also observe that it is not initialized directly as we did for "@EmpID." The following is the statement which does it.

    select @ename = LastName + ',' + FirstName
    from dbo.Employees
    where EmployeeID = @EmpID

    The above SELECT statement retrieves the name (combined LastName and FirstName) of the employee and assigns the value to the variable "@ename." When the SELECT statement assigns its values to the variables, it will not display any output to the user. So, we need another statement to display the value back to the user as follows:

    print 'Name of the Employee:' + @ename

    The above statement displays a message concatenated by the employee name retrieved. You can also work with more than one column as follows:

    use Northwind
    go
    declare @Lname varchar(20)
    declare @Fname varchar(20)
    declare @EmpID int

    set @EmpID = 4
    select @Lname = LastName, @Fname = FirstName
    from dbo.Employees
    where EmployeeID = @EmpID

    print 'Name of the Employee:' + @Lname + ',' + @Fname
    go

    More MS SQL Server Articles
    More By Jagadish Chaterjee


       · Hello guys,This is the first in a series of articles focusing on Transact-SQL...
       · Storing select query result to an array! how's that possible ?
     

    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 3 hosted by Hostway