2014-10-30 99 views
0

我目前遇到了一個可能具有簡單解決方案的挑戰,但不知何故,我無法想出它。SQL Server:通過查找其他表替換字段中的值

我有被格式化兩個字段表A如下:

[ID] [Codes] 
1  A;B 
2  D 
3  A;C 

表B格式如下:

[ID] [Codes] 
A  Apple 
B  Orange 
C  Pear 
D  Strawberry 

我想這樣做是查找/替換爲了生成以下輸出

a.[ID] a.[Parsed_Codes] 
    1  Apple;Orange 
    2  Strawberry 
    3  Apple;Pear 

總之我想代替表A中的代碼,在表B中與這些代碼相關聯的值。

當然,我可以只寫一個長替換語句(在我的情況下有幾百個代碼),但這似乎是一種效率極低的方法。

謝謝!

回答

1

簡單的方法是通過將表A [代碼] column data(csv) into separate rows. 然後與表B加入,以獲得各自的代碼。最後convert the rows to CSV得到結果。嘗試這個。

CREATE TABLE #tablea 
    ([ID] INT,[Codes] VARCHAR(100)) 

INSERT INTO #tablea 
VALUES  (1,'A;B'),(2,'D'),(3,'A;C') 

CREATE TABLE #tableB 
    ([ID] VARCHAR(100),[Codes] VARCHAR(100)) 

INSERT INTO #tableb 
VALUES  ('A','Apple'),('B','Orange'), 
      ('C','Pear'),('D','Strawberry') 

SELECT a.id, 
     a.Codes old_code, 
     b.Codes Parsed_Codes 
INTO #final 
FROM #tableb b 
     JOIN (SELECT id, 
        codes, 
        Split.a.value('.', 'VARCHAR(100)') [new_Codes] 
      FROM (SELECT id, 
          [Codes], 
          Cast ('<M>' + Replace([Codes], ';', '</M><M>') 
            + '</M>' AS XML) AS Data 
        FROM #tablea) AS A 
        CROSS APPLY Data.nodes ('/M') AS Split(a)) a 
     ON a.new_Codes = b.id 

SELECT t1.ID, 
     old_code, 
     Stuff((SELECT '; ' + CONVERT(VARCHAR, Parsed_Codes) 
       FROM #final b 
       WHERE b.ID = t1.ID 
       FOR XML PATH('')), 1, 2, '') 
FROM #final t1 
GROUP BY t1.id, 
      old_code 

OUTPUT

ID old_code Parsed_Codes 
-- -------- ------------ 
1 A;B   Apple; Orange 
2 D   Strawberry 
3 A;C   Apple; Pear 

(注:臨時表,可避免爲了避免混淆代碼我用臨時表)

+0

由於普拉迪普。像魅力一樣工作! – 2014-10-30 10:32:48

0

首先創建此函數..它用於將分隔字符串拆分爲變量表。然後,我們可以使用此函數來確定另一個表中的代碼,並使用STUFF函數將它們作爲一個字符串返回。

CREATE FUNCTION [dbo].[fnSplitString] 
    (
    @string NVarchar(MAX) 
    ,@delimiter Char(1) = ',' 
    ) 
RETURNS @t TABLE (string NVarchar(MAX)) 
AS 
BEGIN 
    DECLARE @pos Int 
    DECLARE @piece Varchar(500) 

    IF RIGHT(RTRIM(@string), 1) <> @delimiter 
     SET @string = @string + @delimiter 

    SET @pos = PATINDEX('%' + @delimiter + '%', @string) 
    WHILE @pos <> 0 
     BEGIN 
     SET @piece = LEFT(@string, @pos - 1) 
     INSERT @t 
        SELECT @piece 

     SET @string = STUFF(@string, 1, @pos, '') 
     SET @pos = PATINDEX('%' + @delimiter + '%', @string) 
     END 

    RETURN 
END 

然後運行下面的查詢......

DECLARE @result TABLE 
    (
    [ID] Int 
    ,[CommaDelimitedCodes] Varchar(500) 
    ) 
DECLARE @codes TABLE 
    (
    [ID] Varchar(500) 
    ,[FullNames] Varchar(500) 
    ) 
INSERT INTO @result 
     SELECT 1 
       ,'A;B' 
INSERT INTO @result 
     SELECT 2 
       ,'D' 
INSERT INTO @result 
     SELECT 3 
       ,'A;C' 

INSERT INTO @codes 
     SELECT 'A' 
       ,'Apple' 
INSERT INTO @codes 
     SELECT 'B' 
       ,'Orange' 
INSERT INTO @codes 
     SELECT 'C' 
       ,'Pear' 
INSERT INTO @codes 
     SELECT 'D' 
       ,'Strawberry' 

SELECT * 
     ,STUFF((
       SELECT ', ' + [FullNames] 
       FROM @codes t 
       WHERE id IN (SELECT * 
           FROM dbo.fnSplitString(r.[CommaDelimitedCodes], ';')) 
       FOR 
       XML PATH('') 
       ), 1, 2, '') AS taglist 
FROM  @result AS r 

我創建了兩個變量表來測試 - 但顯然你的情況,你需要替換那些在你的表實際字段名。