将字符串拆分为列和行

时间:2013-08-12 10:27:58

标签: sql tsql split multiple-columns

我有一个要拆分的字符串;它的格式如下:

---------------------------------------------------------
HEADING DETAILS HERE:
~
INVOICE1^VALUE1^VALUE2^
INVOICE2^VALUE1^VALUE2^
INVOICE3^VALUE1^VALUE2^
TOTAL^OFVALUE1^OFVALUE2^
~
FOOTER DETAILS HERE
---------------------------------------------------------

我的输出应该是一个包含3列的表格:

|    INVOICE    |    VALUE1    |    VALUE2    |
INVOICE 1---------- VALUE1  ---------- VALUE2 
INVOICE 2---------- VALUE1  ---------- VALUE2 
INVOICE 3---------- VALUE1  ---------- VALUE2 

我的想法是用第一个分隔符〜分割 并使用分隔符^

拆分2

我做了一个分割函数,这是我第一次用分隔符〜

分割的结果
ROW 1   HEADER DETAILS HERE:

ROW 2   INVOICE1^VALUE1^VALUE2^ INVOICE2^VALUE1^VALUE2^ INVOICE3^VALUE1^VALUE2^

ROW 3   FOOTER DETAILS HERE

但是,我需要做的是将结果放入列而不是行。另外,我需要获得ROW 2的结果并再次拆分并将其放在这样的列中:

INVOICE column -    VALUE1 column   - VALUE2 column
INVOICE 1---------- VALUE1  ---------- VALUE2 
INVOICE 2---------- VALUE1  ---------- VALUE2 
INVOICE 3---------- VALUE1  ---------- VALUE2

我希望有人能够阅读这个问题并帮助我编写TSQL代码。

关注问题:2013年8月13日
我试图将Rs的代码改为这样的......

USE [test]
GO
/****** Object:  StoredProcedure [dbo].[SplitSP4]    Script Date: 8/13/2013 7:33:31 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[SplitSP4] 
    -- Add the parameters for the stored procedure here
    --<@Param1, sysname, @p1> <Datatype_For_Param1, , int> = <Default_Value_For_Param1, , 0>, 
    --<@Param2, sysname, @p2> <Datatype_For_Param2, , int> = <Default_Value_For_Param2, , 0>
    @TABLE_id varchar(50)
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    --SET NOCOUNT ON;

declare @string varchar(8000)
declare @stringXML xml
declare @NewLine char(2)
set @NewLine=char(13)+char(10)
set @string = (SELECT [dbo].[TABLE_NAME].[iam_string] FROM [dbo].[TABLE_NAME] WHERE [dbo].[TABLE_NAME].[id] = @TABLE_id)
--'HEADING DETAILS HERE:
--~
--INVOICE1^VALUE1^VALUE2^
--INVOICE2^VALUE1^VALUE2^
--INVOICE3^VALUE1^VALUE2^
--TOTAL^OFVALUE1^OFVALUE2^
--~
--FOOTER DETAILS HERE'

declare @table1 table(cols varchar(max))

--convert string to xml
SELECT @stringXML = CONVERT(xml,'<root><i>' + 
                    Replace(@string, @NewLine, '</i><i>') + '</i></root>')

--split xml string into different rows
insert into @table1 
select Cols = I.c.value('.', 'varchar(MAX)')
from @stringXML.nodes('/root/i') I(c)

--split invoice rows into columns
select     
    Inv.value('/Invoice[1]/Attribute[1]','varchar(50)') AS [Invoice],
    Inv.value('/Invoice[1]/Attribute[2]','varchar(50)') AS [Value1],
    Inv.value('/Invoice[1]/Attribute[3]','varchar(50)') AS [Value2]
from
    (
        select CONVERT(XML,'<Invoice><Attribute>' 
            + REPLACE([cols],'~', '</Attribute><Attribute>') 
            + '</Attribute></Invoice>') Inv 
            from @table1 where cols like '%^%'
    ) x

end

...让我能够为CRYSTAL REPORT做一个存储过程。但似乎当我执行这个STORED PROCEDURE时它返回了这个错误。

  

Msg 9421,Level 16,State 1,Procedure SplitSP4,Line 34
      XML解析:第1行,第31个字符,非法名称字符

我想知道如何修复我的代码以适应示例XML代码?

2 个答案:

答案 0 :(得分:1)

假设您的文字在每行末尾都有换行符,请尝试使用

declare @string varchar(4000)
declare @stringXML xml
declare @NewLine char(2)
set @NewLine=char(13)+char(10)
set @string = 'HEADING DETAILS HERE:
~
INVOICE1^VALUE1^VALUE2^
INVOICE2^VALUE1^VALUE2^
INVOICE3^VALUE1^VALUE2^
TOTAL^OFVALUE1^OFVALUE2^
~
FOOTER DETAILS HERE'

declare @table1 table(cols varchar(max))

--convert string to xml
SELECT @stringXML = CONVERT(xml,'<root><i>' + 
                    Replace(@string, @NewLine, '</i><i>') + '</i></root>')

--split xml string into different rows
insert into @table1 
select Cols = I.c.value('.', 'varchar(MAX)')
from @stringXML.nodes('/root/i') I(c)

--split invoice rows into columns
select     
    Inv.value('/Invoice[1]/Attribute[1]','varchar(50)') AS [Invoice],
    Inv.value('/Invoice[1]/Attribute[2]','varchar(50)') AS [Value1],
    Inv.value('/Invoice[1]/Attribute[3]','varchar(50)') AS [Value2]
from
    (
        select CONVERT(XML,'<Invoice><Attribute>' 
            + REPLACE([cols],'^', '</Attribute><Attribute>') 
            + '</Attribute></Invoice>') Inv 
            from @table1 where cols like '%^%'
    ) x

您已经编写了分割函数,您可以使用上面的最后一个选择语句( - split invoice rows into columns)和现有的split函数,将row2转换为xml并将第2行拆分为列

答案 1 :(得分:0)

首先,将第一次拆分的结果存储到临时表中。然后,如果^存在于行值中,则可以在该临时表上使用游标并拆分行。

我使用上面的查询为您创建了存储过程 -

IF OBJECT_ID(N'[dbo].[SplitSP4]', N'P') IS NOT NULL
 DROP PROCEDURE [dbo].[SplitSP4]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[SplitSP4]
    @table_id INT
AS

declare @stringXML xml
declare @NewLine char(2)
DECLARE @string VARCHAR(MAX)
set @NewLine=char(13)+char(10)

declare @table1 table(cols varchar(max))

SELECT @string = string_value FROM table_name WHERE id = @table_id

--convert string to xml
SELECT @stringXML = CONVERT(xml,'<root><i>' + 
                Replace(@string, @NewLine, '</i><i>') + '</i></root>')

--split xml string into different rows
insert into @table1 
select Cols = I.c.value('.', 'varchar(MAX)')
from @stringXML.nodes('/root/i') I(c)

--split invoice rows into columns
select     
    Inv.value('/Invoice[1]/Attribute[1]','varchar(50)') AS [Invoice],
    Inv.value('/Invoice[1]/Attribute[2]','varchar(50)') AS [Value1],
    Inv.value('/Invoice[1]/Attribute[3]','varchar(50)') AS [Value2]
from
(
    select CONVERT(XML,'<Invoice><Attribute>' 
        + REPLACE([cols],'^', '</Attribute><Attribute>') 
        + '</Attribute></Invoice>') Inv 
        from @table1 where cols like '%^%'
) x

并称之为

EXEC SplitSP4 1