datetime century date into pad dynamic cursor money percent sp job isnumeric isdate over update
SQLUSA.com
SQL 2008 GRAND SLAM ON 49 CD
FREE TRIAL  CLICK HERE TO ORDER  SEARCH
SQL Server Training SQL 2005 Scripts SQL 2008 Articles
SQL JOBS News Format Developer
How to put the query results into a variable?

Execute the following Microsoft T-SQL example scripts in SQL Server Management Studio Query Editor to demonstrate assigning query results to local variables.

------------
-- Microsoft SQL Server T-SQL assign query result to variable
------------
USE AdventureWorks;
DECLARE  @FirstName VARCHAR(30),
         @LastName  VARCHAR(30)
SELECT @FirstName = c.FirstName,
       @LastName = c.LastName
FROM   HumanResources.Employee e
       INNER JOIN Person.Contact c
         ON e.ContactID = c.ContactID
WHERE  EmployeeID = 100
SELECT EmployeeFullName = @LastName + ', ' + @FirstName
PRINT @FirstName + ' ' + @LastName -- OUTPUT to Messages: Lane Sacksteder
GO
/* Results
 
EmployeeFullName
Sacksteder, Lane
*/
 
-- T-SQL literal query assignment to variables
DECLARE  @FirstName VARCHAR(30),
         @LastName  VARCHAR(30)
SELECT @FirstName = 'Marlon',
       @LastName = 'Brando'
SELECT Actor = @LastName + ', ' + @FirstName
PRINT @FirstName + ' ' + @LastName -- OUTPUT to Messages: Marlon Brando
GO
/* Results
 
Actor
Brando, Marlon
*/
 
-- SQL assignment of query results to numeric variable
DECLARE @POCount int
SELECT @POCount = COUNT(*)
FROM Purchasing.PurchaseOrderHeader
WHERE YEAR(OrderDate) = 2004
SELECT [Purchase Orders in 2004]=@POCount
GO
/*
Purchase Orders in 2004
2697
*/
 
-- SQL place query results into a variable using comma as delimiter
-- MSSQL convert rows into comma-limited string
DECLARE  @Departments VARCHAR(8000)
SET @Departments = ''
SELECT   @Departments = @Departments +  Name + ', '
FROM     AdventureWorks.HumanResources.Department
ORDER BY GroupName,
         Name
-- Remove last comma with LEFT string function
SELECT DepartmentsString=left(@Departments, LEN(@Departments)-1)
GO
/* Results
 
DepartmentsString
Executive, Facilities and Maintenance, Finance, Human Resources,
Information Services, Purchasing, Shipping and Receiving, Production,
Production Control, Document Control, Quality Assurance, Engineering,
Research and Development, Tool Design, Marketing, Sales
*/
 
-- MSSQL assign table count in database to variable - quotename function
-- T-SQL dynamic sql stored procedure with input / output parameters
CREATE PROCEDURE uspTableCount
                @DatabaseName SYSNAME,
                @Tables       INT  OUTPUT
AS
  BEGIN
    DECLARE  @DynamicSQL NVARCHAR(256), @Count INT
    SET @DynamicSQL = N'SELECT @Count = COUNT(*) FROM ' +
                              QUOTENAME(@DatabaseName) +
                     '.sys.tables'
    PRINT @DynamicSQL  -- Debug & test
    -- SELECT @Count = COUNT(*) FROM [AdventureWorks2008].sys.tables
    -- Dynamic SQL execution with output parameters
    EXEC sp_executesql
      @Query = @DynamicSQL ,
      @Params = N'@Count INT OUTPUT' ,
      @Count = @Count OUTPUT
    SET @Tables = @Count
  END
GO
 
-- Microsoft SQL Server T-SQL execute stored procedure
-- SQL Assign sproc result to variable
DECLARE  @AWtables INT
EXEC uspTableCount  'AdventureWorks2008' ,   @AWtables OUTPUT
SELECT 'AdventureWorks2008 table count' = @AWtables
GO
/* Results
 
AdventureWorks2008 table count
76

*/

------------

Order SQL 2008 GRAND SLAM Today!
SQLUSA.com Home Page
SQL Server Training at www.sqlusa.com.
Microsoft SQL Server 2012 Training Videos at www.sqlusa.com.
SQL Server 2008 Video Training at www.sqlusa.com.
SQL Server 2005 Training Videos at www.sqlusa.com.
Accounting
Administrative
Advertising
Arts
Architecture
Banking
Business Intelligence
Career Jobs
Celebrity
Computer
Consulting
Customer Service
Education
Engineering
Entertainment
Entry Level
Executive
Federal
Finance
Government
Hardware
Healthcare
Hospital
Human Resources
Information Technology
Insurance
Internet
Job Openings
Laboratory
Law Enforcement
Legal
Logistics
Manufacturing
Marketing
Medical
Military
Nursing
Pharmaceutical
Physician
Public Relations
Publishing
Real Estate
Restaurant
Retail
Sales
Social Media
Software
SQL Database
Telecomm
Therapist
Training
Transportation
Truck Driver
Travel
Web
Work from Home

FREE SS SQL / BI OLAP Short Videos on YOUTUBE.com

Microsoft Community Contributor 2011 Microsoft Community Contributor 2012

Search SQLUSA FREE SQL Server Articles & FREE T-SQL Scripts

JOIN US ON TWITTER

Copyright 2005-2012, SMI Corp. All Rights Reserved.

SQL Server 2012 is a program product of Microsoft Corporation.
SQL Server 2008 is a program product of Microsoft Corporation.
SQL Server 2005 is a program product of Microsoft Corporation.
SQL Server 2000 is a program product of Microsoft Corporation.