Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

usp_TransposeNRows v2.0

By j.d.brooks,

This is a stored procedure to transpose N rows from a specified table.

Version 2.0 allows the user to specify the schema in @TableName (the schema defaults to [dbo]).

I have found copying a transposed table to Excel useful for reviewing data from a table with many columns. A transposed table derived from a limited number of rows will not exceed Excel's maximum number of columns, and the transposed data is often easier to navigate and read.

Other transpose procedures I have found on the web are somewhat limited: they assume the columns in the original table are of the same numeric data type. This procedure handles tables with columns of varying data types; the workaround is to cast all data in the output table as varchar(max).

Also, please note that this procedure does NOT return a crosstab table. Crosstabs return summary data (totals or counts), not the original data. In certain specific circumstances (when the cases in a base table are unique) a crosstab procedure can return a transpose of the original data. Again, however, this use is limited to numeric data.

A portion of the procedure was adapted from MSDN community content. Thanks to the original posting member (Brindha Raji) and additional revising members. Please see the procedure code for the reference URL.

SYNTAX:
EXEC usp_TransposeNRows @TableName [, @NRows]

PARAMETERS:

@TableName Name of table to be transposed (REQUIRED)
Format: [<SchemaName>.]<TableName> (<SchemaName> defaults to [dbo])
@NRows Number of rows to transpose (OPTIONAL; Default = 0, max = 1022)
When @NRows = 0, just the column order and column names of the table are returned.

Total article views: 1935 | Views in the last 30 days: 16
 
Related Articles
FORUM

Transposing rows into columns

Transposing rows into columns

SCRIPT

Transpose Query Output

script to return query result transposed.

FORUM

How to transpose rows to columns

Transpose rows to columns.

FORUM

Transpose Columns to Rows

SQL Transpose Data

FORUM

Procedure return value problem

Procedure return value problem

Tags
t-sql    
 
Contribute

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones