SQL中的String.Join

时间:2011-01-27 10:37:47

标签: sql sql-server sql-server-2005 tsql user-defined-functions

我想从名为RMA(简化)的表中选择:

 idRMA| RMA_Number
 -----------------------
 1      RMA0006701
 2      RMA0006730
 3      RMA0006736
 4      RMA0006739
 5      RMA0006742

RMAtdefSymptomCode之间有一个名为trelRMA_SymptomCode的联结表:

 fiSymptomCode| fiRMA
 -----------------------
 1              1
 1              2
 2              2
 5              3
 7              3
 8              3
 2              5
 3              5
 4              5
 5              5

为了完整起见,这是tdefSymptomCode

idSymptomCode    SymptomCodeNumber      SymptomCodeName
1                0000                   Audio problem
2                0100                   SIM problem
3                0200                   Appearance problem
4                0300                   Network problem
5                0500                   On/Off problem

问:

每个RMA都可以有0-5个SymptomCodes。如何在标量值函数中将SymptomCodeNumber与':'等分隔符一起加入SymptomCodeNumber,这样我只得到一个varchar值。

像这样(getRmaSymptomCodes是SVF):

SELECT idRMA, RMA_Number, dbo.getRmaSymptomCodes(idRMA,':') AS Symptoms FROM RMA

这可能是3种不同RMA的症状(都只有一种症状):

RMA_Number    SymptomCodeNumber
RMA0004823    0100
RMA0004823    0200
RMA0000083    0300
RMA0000084    0300
RMA0000084    0400

这应该连接为:

RMA0004823    0100:0200
RMA0000083    0300
RMA0000084    0300:0400

提前谢谢

更新:感谢我创造了这个功能

CREATE FUNCTION [dbo].[getRmaSymptomCodes]
(
    @idRMA int,
    @delimiter varchar(5)
)
RETURNS VARCHAR(8000)
AS
BEGIN
    DECLARE @Codes VARCHAR(8000) 
    SELECT @Codes = COALESCE(@Codes + @delimiter, '') +  tdefSymptomCode.SymptomCodeNumber
    FROM  RMA INNER JOIN
        trelRMA_SymptomCode ON RMA.IdRMA = trelRMA_SymptomCode.fiRMA INNER JOIN
        tdefSymptomCode ON trelRMA_SymptomCode.fiSymptomCode = tdefSymptomCode.idSymptomCode
    where idRMA=@idRMA
    order by SymptomCodeNumber
    return @Codes
END

4 个答案:

答案 0 :(得分:4)

这会吗?

DECLARE @Codes VARCHAR(8000) 
SELECT @Codes = COALESCE(@Codes + ', ': '') +  tdefSymptomCode.SymptomCodeNumber
    FROM  RMA INNER JOIN
        trelRMA_SymptomCode ON RMA.IdRMA = trelRMA_SymptomCode.fiRMA INNER JOIN
        tdefSymptomCode ON trelRMA_SymptomCode.fiSymptomCode = tdefSymptomCode.idSymptomCode
    where idRMA=2
    order by SymptomCodeNumber
return @Codes

答案 1 :(得分:3)

使用经典的XML PATH技巧:

declare @RMA  as table(idRMA int, RMA_Number nvarchar(20))

declare @trelRMA_SymptomCode as table (fiSymptomCode int, fiRMA int)

declare @tdefSymptomCode as table (idSymptomCode int, SymptomCodeNumber nvarchar(4), SymptomCodeName nvarchar(20))

insert into @RMA values
(1,      'RMA0006701'),
(2,      'RMA0006730'),
(3,      'RMA0006736'),
(4,      'RMA0006739'),
(5,      'RMA0006742')

insert into @trelRMA_SymptomCode values
(1,              1),
(1,              2),
(2,              2),
(5,              3),
(7,              3),
(8,              3),
(2,              5),
(3,              5),
(4,              5),
(5,              5)

insert into @tdefSymptomCode values
(1,                '0000',                   'Audio problem'),
(2,                '0100',                   'SIM problem'),
(3,                '0200',                   'Appearance problem'),
(4,                '0300',                   'Network problem'),
(5,                '0500',                   'On/Off problem')

select RMA_Number,
 STUFF(
    (
    SELECT
      ':' + SymptomCodeNumber
    FROM @tdefSymptomCode def 
    join @trelRMA_SymptomCode rel on def.idSymptomCode = rel.fiSymptomCode
    where rel.fiRMA=rma.idRMA
    FOR XML PATH('')
    ), 1, 1, '')
from @rma rma

结果

RMA0006701  0000
RMA0006730  0000:0100
RMA0006736  0500
RMA0006739  NULL
RMA0006742  0100:0200:0300:0500

答案 2 :(得分:2)

select rma_number,
   stuff((select ':' + c.symptomcodenumber
    from trelRMA_SymptomCode r
    inner join tdefsymptomcode c on c.idsymptomcode = r.fisymptomcode
    where r.fiRMA = rma.idRMA
    order by c.symptomcodenumber
    for xml path('')), 1, 1, '') SymptomCodeName
from rma

答案 3 :(得分:1)

这是应该让你去的东西:

ALTER FUNCTION [dbo].[GetUCColumns]
(@tableid INT, @index INT)
RETURNS VARCHAR (MAX)
AS
BEGIN

    DECLARE @cols varchar(max)

    SELECT @cols = COALESCE(@cols + ', ', '') + [name]
    FROM [sys].[columns]
    where object_id = @tableid AND column_id IN 
    (SELECT [column_id]
      FROM [sys].[index_columns]
      where object_id = @tableid AND @index = index_id)

    RETURN @cols
    END