A SQL Statement with the Resultset Flopped

A SQL Statement with the Resultset Flopped

I got a request from a developer the other day to produce a SQL statement with the resultset flopped. In other words, he wanted the rows to appear as columns and the columns to appear as rows. What’s more, he wanted the names of the rows to appear in the first column.

Say you had a resultset like this:

Col1,Col2,Col3 A,B,C D,E,F

He wanted to see the information like this:

 ColName, Value1, Value2,... Col1,A,D Col2,B,E Col3,C,F 

The following code shows how I did this. I wouldn’t use this to return an extremely large recordset, but it is interesting to work with anyway.

declare @colName varchar(50), @colId smallint, @tblName varchar(50)declare	@sql varchar(1000), @cond varchar(6000)--user selection criteria These are the only variables you need to change		--Name OF Table		--Limiting Condition on rowsetselect @tblName='some table', @cond = ' some limiting condition for the table ie (ID=75) or (ID between 1 and 7)'--I would recommend that you not return more than 100 rows--Begin selectionselect @colName='',@colId=1 create table #tmp (cName varchar(50))--add value columns for number of records returnedSelect @sql = 'Declare @cols smallint, @sql varchar(7000);Select @cols=count(*) From ' + @tblNameIf Len(@cond)>0	select @sql = @sql + ' Where ' + @condselect @sql = @sql + ';	while @cols > 0	begin		select @sql = ''alter table #tmp add [Val'' + cast(@cols as varchar) + ''] varchar(50)''		Exec(@sql)		select @cols = @cols - 1	End'exec( @sql)set nocount onwhile @colname is not nullbegin	select @colname = col_name(object_id(@tblName),@colID)	Select @colID = @colid + 1	select @sql = 'declare @retVal varchar(6000);set @retVal = '''';		update ' + @tblName + ' set @[email protected] + '','' + substring(cast (' + @colname + ' as varchar),1,50)'	if len(@cond) > 0 		select @sql = @sql + ' Where ' + @cond	select @sql = @sql + ';		select @retval = replace(substring(@retVal,2,len(@retVal)),'','','''''','''''')		Select @retVal = '''''''' + @retVal + ''''''''		declare @sql varchar(7000);		select @sql = ''Insert Into #tmp Values('''''+ @colName + ''''','' + @retVal + '')''	if len(@sql) > 0 Exec (@sql)'	execute (@sql)endSelect * from #tmpdrop table #tmp
Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of