SQLUSA.com
SQL SERVER 2008 GRAND SLAM
FREE TRIAL  CLICK HERE TO ORDER  SEARCH
SQL JOBS SQL Server Training Scripts JOB BANK
  SQL Server 2008 Training Scripts  
  SQL Server 2005 Training Scripts  
News SQL Server Articles SQL Format

How to architect a WHILE loop using table variable?

Execute the following T-SQL example scripts in Microsoft SQL Server Management Studio Query Editor to demonstrate cursor-like WHILE loop construction.

------------
-- WARNING: WHILE (and cursor) loop does not scale well for large datasets
-- Use set-based operations for large datasets
------------
-- T-SQL create stored procedure - no parameter
-- MSSQL while loop - insert select - table variable
USE Northwind
GO
CREATE PROC SupplierStats
-- ALTER PROC SupplierStats
AS
  BEGIN
  SET nocount  ON
    DECLARE  @imax INT,
             @i    INT
    DECLARE  @Contact VARCHAR(100),
             @Company VARCHAR(50)
-- The RowID identity column will be used for loop control
-- MSSQL declare table variable
    DECLARE  @CompanyInfo  TABLE(
                                 RowID       INT    IDENTITY ( 1 , 1 ),
                                 CompanyName VARCHAR(100),
                                 ContactName VARCHAR(50)
                                 )
    INSERT @CompanyInfo
    SELECT   CompanyName,
             ContactName
    FROM     Suppliers
    WHERE    ContactName LIKE '[a-k]%'
    ORDER BY CompanyName
    
    SET @imax = @@ROWCOUNT
    SET @i = 1
    
    WHILE (@i <= @imax)
      BEGIN
        SELECT @Contact = ContactName,
               @Company = CompanyName
        FROM   @CompanyInfo
        WHERE  RowID = @i
        
        ------------------------------------------------------

        -- INSERT PROCESSING HERE

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

 
        PRINT CONVERT(varchar,@i)+' Contact: ' + @Contact + ' at ' + @Company
        
        SET @i = @i + 1
      END -- WHILE
  END -- SPROC
GO
-- Stored procedure execution - test stored procedure
EXEC SupplierStats
GO
 
/* Partial results
 
1 Contact: Guylène Nodier at Aux joyeux ecclésiastiques
2 Contact: Cheryl Saylor at Bigfoot Breweries
3 Contact: Antonio del Valle Saavedra at Cooperativa de Quesos 'Las Cabras'
4 Contact: Charlotte Cooper at Exotic Liquids
5 Contact: Chantal Goulet at Forêts d'érables
6 Contact: Elio Rossi at Formaggi Fortini s.r.l.
7 Contact: Eliane Noz at Gai pâturage
8 Contact: Anne Heikkonen at Karkki Oy

*/
 

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

Related articles:

http://sqlusa.com/bestpractices2005/doublecursor/

WHILE (Transact-SQL)

SQL Server Table Variables To Eliminate Cursors

 

Order SQL 2008 GRAND SLAM Today!
SQLUSA.com Home Page
SQL Server Training at www.sqlusa.com.
SQL Server 2008 Video Training at www.sqlusa.com.
SQL Server 2005 Training Videos at www.sqlusa.com.
Microsoft SQL Server 2000 Training Videos at www.sqlusa.com.

FREE SQL & Business Intelligence / OLAP Short Videos on YOUTUBE.com

Microsoft Community Contributor 2011
Invest in Your SUCCESS!

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


Copyright 2005-2011, 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.