SQL select:将字段拆分为多个由char(13)标记的行

时间:2011-11-18 06:56:51

标签: sql-server-2005

我的查询为:

SELECT 'Item' AS TypeID, 
    iORCompID AS iEntityID,
    iORCompID AS iParentEntityID,
    '' as Source,
    vComments as Comment
FROM 
    OrderResultComponents
WHERE IOrderID = @IEntityID 

我得到的数据是

TypeID  |   iEntityID   |   iParentEntityID     |   Source  |   Comment
Item    |   1045        |   1045                |           |   Item Found  some pending comments \X000d\ by UserID1
Item    |   1027        |   1027                |           |   Item Found  with some pending comments \X000d\ by UserID2
Item    |   5389        |   5389                |           |   Item Found  with \X000d\ some \X000d\ pending comments \X000d\ by UserID1

(\ X000d \是Char(13)我猜)

但我希望数据如下:

TypeID  |   iEntityID   |   iParentEntityID     |   Source  |   Comment
Item    |   1045        |   1045                |           |   Item Found  some pending comments
Item    |   1045        |   1045                |           |   by UserID1
Item    |   1027        |   1027                |           |   Item Found  with some pending comments 
Item    |   1027        |   1027                |           |   by UserID2
Item    |   5389        |   5389                |           |   Item Found  with 
Item    |   5389        |   5389                |           |   some
Item    |   5389        |   5389                |           |   pending comments 
Item    |   5389        |   5389                |           |   by UserID1

即。我想用DB的下一行字符拆分我的Comment字段,并用这个拆分重复其他字段......任何帮助???

编辑: 啊, 我从Split one column into multiple rows

得到了一个提示

如果无效,请更正我的查询。

SELECT     
'Item' AS TypeID, '' as SetID,T.iORCompID , RIGHT(LEFT(T.vComments,Number-1),    
CHARINDEX(char(13),REVERSE(LEFT(char(13)+T.vComments,Number-1)))) 
FROM     master..spt_values,     OrderResultComponents T 
WHERE     Type = 'P' 
AND Number BETWEEN 1 
AND LEN(T.vComments)+1    
AND (SUBSTRING(T.vComments,Number,1) = char(13) ) AND T.IOrderID = @iEntityID

2 个答案:

答案 0 :(得分:0)

在不知道有关您的应用程序的任何细节的情况下,您需要执行以下操作:

  • 使用初始查询查询数据库并将其存储在DataTable
  • 遍历DataTable中的行,并使用StringBuilder
  • 连接数据
  • 将StringBuilder的内容写入文本文件

有关如何创建和写入文本文件的信息,请查看此StackOverflow thread

如果您有任何其他问题,请添加评论。

答案 1 :(得分:0)

declare @index int;
declare @TypeID varchar(10),@left varchar(max);
declare @iEntityID int,@iParentEntityID int;
declare @Source varchar(max),@Comment varchar(max);

declare split_cursor cursor for 
select * from OrderResultComponents
open split_cursor
fetch next from split_cursor into @TypeID,@iEntityID,@iParentEntityID,@Source,@Comment
while (@@fetch_status=0)
begin
    set @index=charindex('\X000d',@Comment);
    while(@index!=0)
        begin
            set @left=substring(@Comment,1,@index-1);
            set @Comment=substring(@Comment,@index+8,len(@Comment));
            insert into #split values (@TypeID,@iEntityID,@iParentEntityID,@Source,@left);
            set @index=charindex('\X000d',@Comment);
        end
        insert into #split values (@TypeID,@iEntityID,@iParentEntityID,@Source,@Comment);
    fetch next from split_cursor into @TypeID,@iEntityID,@iParentEntityID,@Source,@Comment
end
select * from OrderResultComponents;
select * from #split;
close split_cursor
deallocate split_cursor
truncate table #split