Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hi all....
I have got the resultset while executing a stored procedure, now how can i bind the resultset of rows to columns..................


eg:

col1 | col2
-------------
name | siva
class | btech
mark | 75
year | 2010

i need this result set to be like this

name | class | mark | year
---------------------------
siva | btech | 75 | 2010

how can i get this result in the stored procedure itself.......

pls help me..............


thanks in advance.............
Posted

nr 2 today with the same question. So same answer: read my article about dynamic pivoting: Dynamic Pivoting with Cubes and eventhandlers in SQL Server 2005, 2008 and 2008 R2[^]
 
Share this answer
 
Comments
AditSheth 20-Sep-11 7:11am    
my vote 5
MemberCva 20-Sep-11 7:25am    
thanks yar,
but in my need there is no need of aggregate or group by..
i just got the result set to bind rows to columns........

thanks in advance,
with regards...
siva
Herman<T>.Instance 20-Sep-11 7:37am    
you don't have to do the summming. Only the first 3 or 4 steps solve your question
Use Pivot Function to convert row into column.

Syntax:

XML
SELECT
  [non-pivoted column], -- optional
  [additional non-pivoted columns], -- optional
  [first pivoted column],
  [additional pivoted columns]
FROM (
  SELECT query producing sql data for pivot
  -- select pivot columns as dimensions and
  -- value columns as measures from sql tables
) AS TableAlias
PIVOT
(
  <aggregation function>(column for aggregation or measure column) -- MIN,MAX,SUM,etc
  FOR [<column name containing values for pivot table columns>]
  IN (
    [first pivoted column], ..., [last pivoted column]
  )
) AS PivotTableAlias
ORDER BY clause -- optional
 
Share this answer
 
thanks all for your help.....
i got my desired output by using the query like this.............

SELECT distinct(inst_ID) as ID,
[PO_Date] = (SELECT distinct(Val_col) FROM TR1 WHERE COL_NAME = 'Date' and inst_ID=m.INST_ID),
[Po_No]= (SELECT distinct(Val_col) FROM TR1 WHERE COL_NAME = 'Po_no' and inst_ID=m.JITS_INST_ID),
[Vendor_Code] = (SELECT distinct(Val_col) FROM TR1 WHERE COL_NAME = 'Vendor_Code' and inst_ID=m.inst_ID),
[Vendor_Name]=(SELECT distinct(Val_col) FROM TR1 WHERE COL_NAME = 'Vendor_Name' and inst_ID=m.inst_ID),
[Total_Amt]= (SELECT distinct(Val_col) FROM TR1 WHERE COL_NAME = 'Total_Amt' and inst_ID=m.inst_ID)
FROM JITS_TR1_VALUE m
GROUP BY inst_ID

with regards.....

siva
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900