Suppose there is a table Employee and recordset as
EmpID
|
Name
|
Sal
|
1
|
PQR
|
1000
|
2
|
XYZ
|
2000
|
3
|
ABC
|
3000
|
And aspected Result as
FieldName
|
Value
|
EmpID
|
1
|
Name
|
PQR
|
Sal
|
1000
|
EmpID
|
2
|
Name
|
XYZ
|
Sal
|
2000
|
EmpID
|
3
|
Name
|
ABC
|
Sal
|
3000
|
Query -
SELECT FieldName, Value
FROM
(
SELECT CAST(EmpID AS VARCHAR(10))EmpID,
CAST(name AS VARCHAR(10)) Name,
CAST(sal AS VARCHAR(10)) Sal
FROM Employee
)x
UNPIVOT
(
Value
FOR FieldName in (EmpID,Name,Sal)
)y