SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

usp_TransposeNRows v2.0

By j.d.brooks, 2010/02/17

Total article views: 793 | Views in the last 30 days: 661

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.

 

By j.d.brooks, 2010/02/17

Total article views: 793 | Views in the last 30 days: 661
Your response
 
 
Related Articles
FORUM

Transposing rows into columns

Transposing rows into columns

FORUM

Procedure return value problem

Procedure return value problem

SCRIPT

usp_TransposeNRows

Stored procedure to transpose the first N rows of a specified table

FORUM

Columns to multiple rows - transpose based on pattern in the column

Columns to multiple rows - transpose based on pattern in the column

FORUM

Transpose cloumns into rows and rows into columns

hi everyone i want to show the transpose the tble into sql server.iam providing the sample data.

Tags
t-sql    
 
Contribute

Free registration required...

To read the rest of this article, and access thousands of other articles, we ask you to register on the site and subscribe to our newsletters.

Login (existing users)

Login

Email:   Password:   Remember me: Forgotten your password?

Register (new users)

Register

Email:   Password:
Confirm:

Subscribing to our newsletters gets you:

  • ALL of our content (thousands of articles, scripts, and forum postings)
  • A daily newsletter (example)
  • A weekly news round up (example)
  • The opportunity to ask and answer questions in our forums
  • A daily Question of the Day to test and help you increase your knowledge of SQL Server.

Steve Jones
Editor, SQLServerCentral.com