如何将所有列值显示为一行 [英] how to display all column values as a row
问题描述
大家好,
我有一张桌子(可能是DataTable)
ID Name Department
1 ABC Oracle
2 DEF JAVA
3 GHI C#
现在我希望这个输出应该是:
Column1 Column2
ID 1,2,3
名称ABC,DEF,GHI
部门Oracle,JAVA,C#
请帮我使用C#或SQL代码。
提前谢谢。
我不确定这是不是最好的办法但你可以:
- 使用SELECT * FROM myTable
和DataAdapter。 http://msdn.microsoft.com/de-de/library/ bh8kx08z%28v = vs.80%29.aspx [ ^ ]
- 然后我会列出每个列的列表并填写它们的主题每一行。
- 否则,如果您想要一个DataTable作为结果,您可以使用加倍的foreach循环来处理这样的事情。或者你看看:
使用C#转置数据表 [ ^ ]
希望这会有所帮助。
check它在这里:
http:// sql- tricks.blogspot.com/2011/04/sql-server-rows-transpose.html [ ^ ]
您好b $ b
您可以使用来自下面的代码:
声明@ID nVarChar(500),
@Name nVarChar(500),
@部门nVarChar(500)
----------------------------------------- --------------------------------------
选择@ID = N''' ',
@N ame = N'''',
@DepartMent = N''''
-------------------------- -------------------------------------------------- -
选择@ID = @ID +'',''+演员(ID为VarChar),
@Name = @Name +'',''+姓名,
@Department = @Department +'',''+ Department
来自[YourTableName]
--------------------------- -------------------------------------------------- -
选择@ID =子串(@ ID,2,Len(@ID)),
@Name =子串(@ Name,2,Len(@Name)),
@ Department = Substring(@Department,2,Len(@Department))
------------------------------- ------------------------------------------------
选择N''ID'',@ ID
Union
选择N''Name'',@ Name
Union
选择N''Department'',@ Department
如果您想动态使用此动作,请告诉我为您编写动态版本。
我希望它有用
Hi All,
I have a table(may be DataTable) as
ID Name Department 1 ABC Oracle 2 DEF JAVA 3 GHI C#
Now I want this output should be :
Column1 Column2 ID 1,2,3 Name ABC,DEF,GHI Department Oracle,JAVA,C#
Please help me in C# or SQL code.
Thanks in advance.解决方案Hi,
i''m not sure if this is the best approach but you can:
- fill a datatableobject in c# with aSELECT * FROM myTable
and the DataAdapter. http://msdn.microsoft.com/de-de/library/bh8kx08z%28v=vs.80%29.aspx[^]
- then i would make a list for each column and fill them with the entrys of each row.
- Otherwise, if you want to have a DataTable as result, your can use a doubled foreach loop to handle stuff like this. Or you have a look at:
Transpose a DataTable using C#[^]
hope this helps.
check it out here:
http://sql-tricks.blogspot.com/2011/04/sql-server-rows-transpose.html[^]
Hi
You can use from bellow code:
Declare @ID nVarChar(500), @Name nVarChar(500), @Department nVarChar(500) ------------------------------------------------------------------------------- Select @ID = N'''', @Name = N'''', @DepartMent = N'''' ------------------------------------------------------------------------------ Select @ID = @ID + '', '' + Cast(ID As VarChar), @Name = @Name + '', '' + Name, @Department = @Department + '', '' + Department From [YourTableName] ------------------------------------------------------------------------------- Select @ID = Substring(@ID, 2, Len(@ID)), @Name = Substring(@Name, 2, Len(@Name)), @Department = Substring(@Department, 2, Len(@Department)) ------------------------------------------------------------------------------- Select N''ID'', @ID Union Select N''Name'', @Name Union Select N''Department'',@Department
If you want using this action dynamically, please tell me to write dynamic version for you.
I hope it''s helpful
这篇关于如何将所有列值显示为一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!