Call now: (800) 766-1884  



 Home


 SQL Server Tips
 SQL Server Training

 SQL Server Consulting
 SQL Server Support
 SQL Server Remote DBA



 Articles
 Services
 SQL Server Scripts
 Scripts Menu



 

 

 

 
  SQL Server Tips by Gama and Naughter

 

Return rowsets with a variable number of columns
 

When creating queries for reports, sometimes the specifications ask for plenty of flexibility. Queries must always return a fixed number of columns and dynamic SQL is one way to work around that.

 

Example: Create a SP that will return the N first columns from a table. The input will be the table name and the number of columns to be returned.

 

CREATE PROCEDURE spTableNfirstColumns @table  NVARCHAR(128), @Ncols int

--returns the N first columns from a table

AS

DECLARE @ColNames NVARCHAR(500), @SQL NVARCHAR(4000)

SET @ColNames=''

SELECT @ColNames=@ColNames+N','+COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS

WHERE TABLE_NAME=@table AND ORDINAL_POSITION<=@Ncols

SET @ColNames=RIGHT(@ColNames, LEN(@ColNames)-1)

SET @SQL=N'SELECT '+@ColNames+N' FROM '+@table

EXEC (@SQL)

 

Example: Create a SP that will return the product names from table Categories and a variable number of columns, each one corresponding to a category. Under each category column, there would be the number of products in stock.

 

CREATE PROCEDURE spProductsByCategory

--returns the product names and their number in stock, with the number under a category column

AS

DECLARE @SQL NVARCHAR(4000)

SET @SQL='SELECT Products.ProductName, '

DECLARE @CategoryID int, @CategoryName NVARCHAR(15)

DECLARE cur_ds  CURSOR

FOR SELECT CategoryID, CategoryName FROM  Categories

OPEN cur_ds

FETCH NEXT FROM cur_ds INTO @CategoryID, @CategoryName

WHILE @@FETCH_STATUS = 0

      BEGIN

      SET @SQL=@SQL+'CASE Products.categoryid WHEN '+CAST(@CategoryID as CHAR)

      +' THEN Products.UnitsInStock ELSE 0 END AS ['+CAST(@CategoryName as CHAR)+'],'

      FETCH NEXT FROM cur_ds INTO @CategoryID, @CategoryName

      END

CLOSE cur_ds

DEALLOCATE cur_ds

SET @SQL=LEFT(@SQL,LEN(@SQL)-1)--remove the last comma

SET @SQL=@SQL+'FROM  Products INNER JOIN Categories ON Products.CategoryID = Categories.CategoryID'

EXEC (@SQL)

 

Allow parameterized filtering with the IN clause
 

Example: Create a SP that will return rows from table Shippers filtering by ShipperID. Any combination of ShipperID should be possible by means of using the IN clause with its filter data as an input parameter of the SP.

 

CREATE PROCEDURE spOrdersByShipperID @Shippers varchar(20)

--returns the orders by any combination of shipper ID's

AS

EXEC('SELECT dbo.Orders.OrderID, dbo.Orders.ShipName, dbo.Orders.ShipCity, dbo.Orders.ShipRegion, dbo.Orders.ShipPostalCode, dbo.Orders.ShipCountry,

               dbo.Shippers.CompanyName

FROM  dbo.Orders INNER JOIN

               dbo.Shippers ON dbo.Orders.ShipVia = dbo.Shippers.ShipperID

WHERE (dbo.Shippers.ShipperID IN ('+@Shippers+'))')
 


The above book excerpt is from:

Super SQL Server Systems
Turbocharge Database Performance with C++ External Procedures

ISBN: 0-9761573-2-2
Joseph Gama, P. J. Naughter

 http://www.rampant-books.com/book_2005_2_sql_server_external_procedures.htm
 

 

Burleson Consulting Remote DB Administration


 

 


 

 

 

 

 
Burleson is the America's Team

Note: The pages on this site were created as a support and training reference for use by our staff of DBA consultants.  If you find it confusing, please exit this page.

Errata?  SQL Server technology is changing and we strive to update our SQL Server support information.  If you find an error or have a suggestion for improving our content, we would appreciate your feedback.  Just  e-mail:and include the URL for the page.
 


Burleson Consulting
SQL Server database support

 

Copyright 1996 -  2013 by Vaaltech Web Services. All rights reserved.

Hit Counter