SQLServer:使用其他表中的DATA对COLUMN值进行分组和替换,而不使用UDF

时间:2016-02-03 16:12:52

标签: sql-server sql-server-2008

我想替换@CommentsTable列中的数字"评论"使用@ModTable表中的等效文本,而不在单个SELECT中使用UDF。可能与CTE。用REPLACE试过STUFF,但没有运气。

任何建议都会有很大的帮助!

样品:

DECLARE @ModTable TABLE
(  
    ID INT,  
    ModName VARCHAR(10),  
    ModPos VARCHAR(10) 
)

DECLARE @CommentsTable TABLE
(  
    ID INT,  
    Comments VARCHAR(100)
)

INSERT INTO @CommentsTable  
VALUES  (1, 'MyFirst 5 Comments with 6'),
        (2, 'MySecond comments'),
        (3, 'MyThird comments 5')

INSERT INTO @ModTABLE  
VALUES  (1, '[FIVE]', '5'),
        (1, '[SIX]', '6'),
        (1, '[ONE]', '1'),
        (1, '[TWO]', '2')

SELECT T1.ID, <<REPLACED COMMENTS>>     
FROM @CommentsTable T1  
GROUP BY T1.ID, T1.Comments


**Expected Result:**
ID Comments               
1  MyFirst [FIVE] Comments with [SIX]
2  MySecond comments
3  MyThird comments [FIVE]

4 个答案:

答案 0 :(得分:0)

创建一个光标,跨越@ModTable并进行每次替换

DECLARE replcursor FOR SELECT ModPos, ModName FROM @ModTable;
OPEN replcursor;

DECLARE modpos varchar(100) DEFAULT "";
DECLARE modname varchar(100) DEFAULT "";

get_loop: LOOP
    FETCH replcursor INTO @modpos, @modname

    SELECT T1.ID, REPLACE(T1.Comments, @modpos, @modname)     
    FROM @CommentsTable T1  
    GROUP BY T1.ID, T1.Comments
END LOOP get_loop;

当然,您可以将结果存储在临时表中,并在循环结束时完全获得结果。

答案 1 :(得分:0)

您可以使用while循环迭代记录和mod。我稍微修改了你的@ModTable以获得ID的唯一值。如果这不是您的数据结构,那么您可以使用类似ROW_NUMBER()的窗口函数来获取可以迭代的唯一值。

修改后的脚本示例:

  DECLARE @ModTable TABLE
 (  
    ID INT,  
    ModName VARCHAR(10),  
    ModPos VARCHAR(10) 
  )

   DECLARE @CommentsTable TABLE
(  
    ID INT,  
    Comments VARCHAR(100)
)


   INSERT INTO @CommentsTable  
VALUES  (1, 'MyFirst 5 Comments with 6'),
        (2, 'MySecond comments'),
        (3, 'MyThird comments 5')

   INSERT INTO @ModTABLE  
VALUES  (1, '[FIVE]', '5'),
        (2, '[SIX]', '6'),
        (3, '[ONE]', '1'),
        (4, '[TWO]', '2')


declare @revisedTable table (id int, comments varchar(100))
declare @modcount int = (select count(*) from @ModTable)
declare @commentcount int = (select count(*) from @CommentsTable)
declare @currentcomment varchar(100) = ''
while @commentcount > 0
    begin
        set @modcount = (select count(*) from @ModTable)
        set @currentcomment = (select Comments from @CommentsTable where ID = @commentcount)
        while @modcount > 0
            begin
                set @currentcomment = REPLACE(  @currentcomment, 
                                                (SELECT TOP 1 ModPos FROM @ModTable WHERE ID = @modcount), 
                                                (SELECT TOP 1 ModName FROM @ModTable WHERE ID = @modcount))
                set @modcount = @modcount - 1
            end
        INSERT INTO @revisedTable (id, comments) 
        SELECT @commentcount, @currentcomment
        set @commentcount = @commentcount - 1
    end

SELECT *
FROM @revisedTable
order by id

答案 2 :(得分:0)

我认为即使我通常避免递归查询也会有效。它假定您有连续的ID:

with Comments as
( 
    select ID, Comments, 0 as ConnectID
    from @CommentsTable
    union all
    select ID, replace(c.Comments, m.ModPos, m.ModName), m.ConnectID
    from Comments c inner join @ModTable m on m.ConnectID = c.ConnectID + 1
) 
select * from Comments
where ConnectID = (select max(ID) from @ModTable)

答案 3 :(得分:0)

=&GT; CLR函数()

因为我在&#34;评论表&#34;中有很多记录。和#34; ModTable&#34;每个评论会有多个ModName,最后决定使用CLR函数。感谢大家的建议和指示。