Sql server 2005 SQL select:将字段拆分为多行,并用char(13)标记
我的问题如下:Sql server 2005 SQL select:将字段拆分为多行,并用char(13)标记,sql-server-2005,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 | iParentEntit
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\我猜是字符(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
i、 我想用DB的下一行字符分割我的注释字段,并用这个分割重复其他字段…有什么帮助吗
编辑:
啊,,
我从他那里得到一个暗示
如果我的查询无效,请更正
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
在不了解应用程序的任何细节的情况下,您需要执行以下操作:
- 使用初始查询查询数据库并将其存储在
- 循环遍历DataTable中的行,并使用
- 将StringBuilder的内容写入文本文件
如果您还有其他问题,请添加评论。这会破坏数据库的逻辑。为什么要这样做?实际上,我的目的是从sql中选择数据按原样写入文件(pritable…),您需要使用应用程序代码来完成这项操作。我假定您使用的是ASP.Net。你能解释一下你希望最终产品是什么样的吗?我是在windows服务(.NET)下做的。。实际上,它的目的是将所有结果转储到一个文本文件中,以便第三方可以接收该文件,对其进行解析并打印,而无需对格式进行任何更改。Greg,实际上我不必在我的应用程序中这样做。显然,应用程序/服务可以很容易地做到这一点。问题是,我编写了一个文件编写器,可以在文件中写入多个项。我的意思是,上面的查询是我的SP返回的项目的子集。我无法指定查询所选的项目。
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