Click here to Skip to main content
15,888,816 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi,

i select one row data always
example:

SQL
SELECT * from tbl_test(table name) where test_id = 1

this query give me a result like

test_id       name      address     mobile
1        TestName   Baroda     6565656565


but i want to select data in pivot manner
want result like

ID     CLM_NAME        VALUE
1       test_id          1
2       name           TestName
3       address        Baroda
4       mobile         6565656565

Please help me ..thanks
Posted
Updated 20-Apr-13 0:30am
v2

1 solution

Try this:
SQL
DECLARE @id INT
SET @id = 1

SELECT ROW_NUMBER() OVER(ORDER BY T.RowID) AS ID, CLM_NAME, [VALUE]
FROM (
    SELECT test_id AS RowID, 'test_id' AS CLM_NAME, CONVERT(NVARCHAR(10), test_id) AS [VALUE]
    FROM tbl_test
    UNION ALL
    SELECT test_id AS RowID, 'name' AS CLM_NAME, [name] AS [VALUE]
    FROM tbl_test
    UNION ALL
    SELECT test_id AS RowID, 'address' AS CLM_NAME, address AS [VALUE]
    FROM tbl_test
    UNION ALL
    SELECT test_id AS RowID, 'mobile' AS CLM_NAME, CONVERT(NVARCHAR(30), mobile) AS [VALUE]
    FROM tbl_test
) AS T
WHERE T.RowID = @id
 
Share this answer
 
v2
Comments
PKriyshnA 20-Apr-13 8:54am    
Thank You So much
Maciej Los 20-Apr-13 8:56am    
You're welcome, call again ;)
PKriyshnA 20-Apr-13 9:39am    
Hello...if i have columns more than 500. then my query will become very long. so have any another solution ?? i have tried using dynamic columns but got some errors . SQL Query is: select COLUMN_NAME,exec('SELECT '+ COLUMN_NAME +' FROM Tbl_customer where cst_id=1') from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='Tbl_customer' Please help again if you can...thanks
Maciej Los 20-Apr-13 10:15am    
I'll promise you to help, but not today, OK?
PKriyshnA 20-Apr-13 23:14pm    
can you please give me a solution today ??

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