在SQL Server中使用循环语句将列的值显示为行 [英] displaying the values of a column as rows using looping statement in sql server

查看:86
本文介绍了在SQL Server中使用循环语句将列的值显示为行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


Hello Everyone,


Select NewDate INTO #MyTable from @Months
--while(NewDate<12)
select convert( varchar(50), NewDate, 103 ) as 'NewDate' from #MyTable
drop table #MyTable



如上,我有一个临时表,从中可以得到名为"NewDate"的列的日期(值).如:

输出:



As in the above, I have a temporary table from which I am getting the the dates(values) for a column called ''NewDate''. As:

output:

NewDate
07/01/2011
07/02/2011
07/03/2011
07/04/2011
07/05/2011




在这里,我需要遍历NewDate的所有值,以便以列格式显示它们.




Here I need to loop through all the values of NewDate in order to display them in a column format.. As:

NewDate  07/01/2011 07/02/2011 07/03/2011 07/04/2011 07/05/2011




请任何人建议我,如何使用循环并获取行(值)作为列.我不想使用PIVOT.

问候,




Please any one suggest me, how to use the looping and get the rows(values) as columns. I don''t want to use PIVOT.

Regards,

推荐答案

某些解决方案此处 [ ^ ]可能会给您一些帮助
Some solutions here[^] might give you some ideas.


通常是列到行或行到列,请使用PIVOT方法.您可以在查询中或在数据透视后添加格式.

枢转后进行格式化可能会更容易和更好,因为枢转本身是一项昂贵的操作,不需要在其上施加更多的负担.

这是枢转方法的Microsoft文档

http://msdn.microsoft.com/en-us/library/ms177410.aspx [ ^ ]
Generally column to rows or Rows to colums, use PIVOT method. You can include format on the query or after pivoting.

It may be easier and better to do formatting after pivoting, because pivoting itself is a costly operation and doesn''t need to put more load on it.

Here is the Microsoft documentation of pivoting method

http://msdn.microsoft.com/en-us/library/ms177410.aspx[^]


这篇关于在SQL Server中使用循环语句将列的值显示为行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆