How to find the Column Name of a Table from its Column Value in SQL Server 2005?
I have a view which returns a result as follows:
D1 D2 D3 D1_CODE D2_CODE D3_CODE
---- ---- ---- --------- --------- ---------
10.00 13.00 14.00 OD LENGTH THICKNESS
I created the view on the below selection query,
SELECT
MKT_MRL_Detail.D1,
MKT_MRL_Detail.D2,
MKT_MRL_Detail.D3,
MAS_SizeType.D1_Code,
MAS_SizeType.D2_Code,
MAS_SizeType.D3_Code
FROM
MKT_MRL_Detail INNER JOIN
STR_Item ON STR_Item.ItemID = MKT_MRL_Detail.ItemID INNER JOIN
MAS_SizeType ON MAS_SizeType.SizeTypeID = STR_Item.SizeTypeID
I have return the view Based on the Inner Join of Two Tables. The above view returns only one row. Here, I want to find which开发者_运维问答 Column has a value 'LENGTH': I am using SQL Server 2005. Can any one please help?
My best suggestion would be to re-write the view (or possibly even the database) since this is not a very good design. However, I doubt that is possible. If it is not, here is my suggestion:
SELECT *
FROM (
SELECT D1 AS Value, D1_CODE AS Code
FROM view
UNION ALL
SELECT D2, D2_CODE
FROM view
UNION ALL
SELECT D3, D3_CODE
FROM view
) AS myInfo
WHERE Code='LENGTH'
It isn't pretty but it would get the job done.
精彩评论