多行应该用TAB分隔写入一行 [英] Mutilple rows should write into one row with TAB delimited
问题描述
我正在使用SQL SERVER 2008 R2,我有像简单选择的情况,从内部联接的两个表中给出多行。我想简单地将这些多行放入TAB分隔的单行中。
这是我的简单SQL
选择''xxx' ',a.userid,a.status,ISNULL(Convert(varchar,a.time,108),'''')AS TIME,a.uid
来自TABLE AS a WITH(NOLOCK)
内连接表as b WITH(NOLOCK)
a.status = b.statuscode
我收到的数据通过使用上面的SQL作为
xxx john CMD 10:50:06 plk
xxx smith CMDT 10:50:29 smk
xxx chris CMDT 10:50:29 ll
xxx smith CMD 10:50:38 plk
我希望在一个地方得到这个如下线
xxx john CMD 10:50:06 plk smith CMDT 10:50:29 smk chris CMDT 10:50:29 lll smith CMD 10:50:38 plk
I am using SQL SERVER 2008 R2 and I have situation like simple select gives multiple rows from two tables with inner join. I want to get simply these mutilple rows into one single row with TAB delimited.
Here is my simple SQL
Select ''xxx'',a.userid, a.status,ISNULL(Convert(varchar,a.time,108),'' '' ) AS TIME,a.uid
from TABLE AS a WITH (NOLOCK)
Inner join TABLE AS b WITH (NOLOCK) on
a.status = b.statuscode
I am getting the data by using above SQL as
xxx john CMD 10:50:06 plk
xxx smith CMDT 10:50:29 smk
xxx chris CMDT 10:50:29 ll
xxx smith CMD 10:50:38 plk
where as I want to get this in all in ONE line like below
xxx john CMD 10:50:06 plk smith CMDT 10:50:29 smk chris CMDT 10:50:29 lll smith CMD 10:50:38 plk
推荐答案
在这里,读取这个并根据您的要求调整一下。
快乐编码!!!
~~ CK
Here, read this and tweak it a bit for you requirement.
Happy Coding!!!
~~ CK
您可以使用SQL Server的XML功能来聚合字符串。
You could use the XML capabilities of SQL Server to aggregate strings.
谢谢CK。我无法找到如何根据我的要求调整代码,或者我可能无法捕获您在此代码中编写的逻辑。我并不担心TAB分隔,如果我知道只需将多行数据放入一行就足够了。我可以很容易地获得TAB值,但是当从SQL SERVER检索这些多条记录时,我想要将所有逻辑放在一行中。
Thanks CK. I can''t find how to tweak your code for my requirement, or may be I am unable to catch the logic that you had written in this code. I am not worried about TAB delimited, if I know the code for just make multiple rows of data into one single row is enough. TAB values I can obtain easily but I want the logic for getting all in one row when retrieving from SQL SERVER these multiple records.
这篇关于多行应该用TAB分隔写入一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!