使用递归的字符串替换在SQL中不起作用(字符串的第一部分没有被替换,所有工作正常)

时间:2016-01-11 23:41:47

标签: sql sql-server sql-server-2008

我有以下代码,我试图用数值替换字符串。基于按功能分组。 (简单地根据条件多次替换单个字符串)。

它工作正常,但字符串的第一部分没有得到替换休息一切正常。我不知道这个问题究竟发生在哪里。

这部分人能帮助我吗?

CREATE TABLE #MathTemp1
(
IDNUM INTEGER IDENTITY(1,1),
YEARMONTH VARCHAR(256),
OUTPUTFORMULA VARCHAR(256)
)

CREATE TABLE #MathLogicTable
(
IDNUM INTEGER,
FORMULA Varchar(256),
INPUTName varchar(160),
AttributeValue Decimal(15,3),
yearmonth varchar(160)
)

INSERT INTO #MathLogicTable ([IDNUM], [formula], [INPUTNAME], [AttributeValue], [YEARMONTH])
VALUES ('1', '( (CV3: 1) + (CV3: 2) + (CV3: 3) + (CV3: 4) + (CV3: 5) + (CV3: 6) + (CV3: 7) + (CV3: 8) + (CV3: 9) + (CV3: 10) + (CV3: 11) + (CV3: 12) )/1000000', 'CV3: 1 ', '17.000', '2013')
    ,('2', '( (CV3: 1) + (CV3: 2) + (CV3: 3) + (CV3: 4) + (CV3: 5) + (CV3: 6) + (CV3: 7) + (CV3: 8) + (CV3: 9) + (CV3: 10) + (CV3: 11) + (CV3: 12) )/1000000', 'CV3: 2','25.000' ,'2013')
    ,('3', '( (CV3: 1) + (CV3: 2) + (CV3: 3) + (CV3: 4) + (CV3: 5) + (CV3: 6) + (CV3: 7) + (CV3: 8) + (CV3: 9) + (CV3: 10) + (CV3: 11) + (CV3: 12) )/1000000', 'CV3: 3','12.000' ,'2013')
    ,('4', '( (CV3: 1) + (CV3: 2) + (CV3: 3) + (CV3: 4) + (CV3: 5) + (CV3: 6) + (CV3: 7) + (CV3: 8) + (CV3: 9) + (CV3: 10) + (CV3: 11) + (CV3: 12) )/1000000', 'CV3: 4','13.000' ,'2013')
  ,('5', '( (CV3: 1) + (CV3: 2) + (CV3: 3) + (CV3: 4) + (CV3: 5) + (CV3: 6) + (CV3: 7) + (CV3: 8) + (CV3: 9) + (CV3: 10) + (CV3: 11) + (CV3: 12) )/1000000', 'CV3: 5','11.000' ,'2013')


  ,('6', '( (CV3: 1) + (CV3: 2) + (CV3: 3) + (CV3: 4) + (CV3: 5) + (CV3: 6) + (CV3: 7) + (CV3: 8) + (CV3: 9) + (CV3: 10) + (CV3: 11) + (CV3: 12) )/1000000', 'CV3: 6','10.000' ,'2013')
     ,('7', '( (CV3: 1) + (CV3: 2) + (CV3: 3) + (CV3: 4) + (CV3: 5) + (CV3: 6) + (CV3: 7) + (CV3: 8) + (CV3: 9) + (CV3: 10) + (CV3: 11) + (CV3: 12) )/1000000', 'CV3: 7','21.000' ,'2013')
    ,('8', '( (CV3: 1) + (CV3: 2) + (CV3: 3) + (CV3: 4) + (CV3: 5) + (CV3: 6) + (CV3: 7) + (CV3: 8) + (CV3: 9) + (CV3: 10) + (CV3: 11) + (CV3: 12) )/1000000', 'CV3: 8','26.000' ,'2013')
    ,('9', '( (CV3: 1) + (CV3: 2) + (CV3: 3) + (CV3: 4) + (CV3: 5) + (CV3: 6) + (CV3: 7) + (CV3: 8) + (CV3: 9) + (CV3: 10) + (CV3: 11) + (CV3: 12) )/1000000', 'CV3: 9','23.000' ,'2013')   
  ,('10', '( (CV3: 1) + (CV3: 2) + (CV3: 3) + (CV3: 4) + (CV3: 5) + (CV3: 6) + (CV3: 7) + (CV3: 8) + (CV3: 9) + (CV3: 10) + (CV3: 11) + (CV3: 12) )/1000000', 'CV3: 10','27.000' ,'2013')
  ,('11', '( (CV3: 1) + (CV3: 2) + (CV3: 3) + (CV3: 4) + (CV3: 5) + (CV3: 6) + (CV3: 7) + (CV3: 8) + (CV3: 9) + (CV3: 10) + (CV3: 11) + (CV3: 12) )/1000000', 'CV3: 11','51.000' ,'2013')
  ,('12', '( (CV3: 1) + (CV3: 2) + (CV3: 3) + (CV3: 4) + (CV3: 5) + (CV3: 6) + (CV3: 7) + (CV3: 8) + (CV3: 9) + (CV3: 10) + (CV3: 11) + (CV3: 12) )/1000000', 'CV3: 12','46.000' ,'2013')

  ,('1', '( (CV3: 1) + (CV3: 2) + (CV3: 3) + (CV3: 4) + (CV3: 5) + (CV3: 6) + (CV3: 7) + (CV3: 8) + (CV3: 9) + (CV3: 10) + (CV3: 11) + (CV3: 12) )/1000000', 'CV3: 1 ', '17.000', '2014')
    ,('2', '( (CV3: 1) + (CV3: 2) + (CV3: 3) + (CV3: 4) + (CV3: 5) + (CV3: 6) + (CV3: 7) + (CV3: 8) + (CV3: 9) + (CV3: 10) + (CV3: 11) + (CV3: 12) )/1000000', 'CV3: 2','25.000' ,'2014')
    ,('3', '( (CV3: 1) + (CV3: 2) + (CV3: 3) + (CV3: 4) + (CV3: 5) + (CV3: 6) + (CV3: 7) + (CV3: 8) + (CV3: 9) + (CV3: 10) + (CV3: 11) + (CV3: 12) )/1000000', 'CV3: 3','12.000' ,'2014')
    ,('4', '( (CV3: 1) + (CV3: 2) + (CV3: 3) + (CV3: 4) + (CV3: 5) + (CV3: 6) + (CV3: 7) + (CV3: 8) + (CV3: 9) + (CV3: 10) + (CV3: 11) + (CV3: 12) )/1000000', 'CV3: 4','13.000' ,'2014')
  ,('5', '( (CV3: 1) + (CV3: 2) + (CV3: 3) + (CV3: 4) + (CV3: 5) + (CV3: 6) + (CV3: 7) + (CV3: 8) + (CV3: 9) + (CV3: 10) + (CV3: 11) + (CV3: 12) )/1000000', 'CV3: 5','11.000' ,'2014')


  ,('6', '( (CV3: 1) + (CV3: 2) + (CV3: 3) + (CV3: 4) + (CV3: 5) + (CV3: 6) + (CV3: 7) + (CV3: 8) + (CV3: 9) + (CV3: 10) + (CV3: 11) + (CV3: 12) )/1000000', 'CV3: 6','10.000' ,'2014')
    ,('7', '( (CV3: 1) + (CV3: 2) + (CV3: 3) + (CV3: 4) + (CV3: 5) + (CV3: 6) + (CV3: 7) + (CV3: 8) + (CV3: 9) + (CV3: 10) + (CV3: 11) + (CV3: 12) )/1000000', 'CV3: 7','21.000' ,'2014')
    ,('8', '( (CV3: 1) + (CV3: 2) + (CV3: 3) + (CV3: 4) + (CV3: 5) + (CV3: 6) + (CV3: 7) + (CV3: 8) + (CV3: 9) + (CV3: 10) + (CV3: 11) + (CV3: 12) )/1000000', 'CV3: 8','26.000' ,'2014')
    ,('9', '( (CV3: 1) + (CV3: 2) + (CV3: 3) + (CV3: 4) + (CV3: 5) + (CV3: 6) + (CV3: 7) + (CV3: 8) + (CV3: 9) + (CV3: 10) + (CV3: 11) + (CV3: 12) )/1000000', 'CV3: 9','23.000' ,'2014')   
  ,('10', '( (CV3: 1) + (CV3: 2) + (CV3: 3) + (CV3: 4) + (CV3: 5) + (CV3: 6) + (CV3: 7) + (CV3: 8) + (CV3: 9) + (CV3: 10) + (CV3: 11) + (CV3: 12) )/1000000', 'CV3: 10','27.000' ,'2014')
  ,('11', '( (CV3: 1) + (CV3: 2) + (CV3: 3) + (CV3: 4) + (CV3: 5) + (CV3: 6) + (CV3: 7) + (CV3: 8) + (CV3: 9) + (CV3: 10) + (CV3: 11) + (CV3: 12) )/1000000', 'CV3: 11','51.000' ,'2014')
  ,('12', '( (CV3: 1) + (CV3: 2) + (CV3: 3) + (CV3: 4) + (CV3: 5) + (CV3: 6) + (CV3: 7) + (CV3: 8) + (CV3: 9) + (CV3: 10) + (CV3: 11) + (CV3: 12) )/1000000', 'CV3: 12','46.000' ,'2014')




Select * from #MathLogicTable

;WITH DataSource1 AS
( 
    SELECT ROW_NUMBER() OVER(PARTITION BY [YEARMONTH] ORDER BY [IDNUM]) AS [ReplacementOrderID]
      ,[YEARMONTH]
      ,[formula]
      ,[INPUTNAME] AS [ReplacementString]
      ,AttributeValue AS [ReplacementValue]
    FROM #MathLogicTable
)

,RecursiveDataSource AS
(
    SELECT [ReplacementOrderID]
      ,[YEARMONTH]
      ,REPLACE([formula], [ReplacementString], [ReplacementValue]) AS [formula]
    FROM DataSource1 
    WHERE [ReplacementOrderID] = 1
    UNION ALL
    SELECT DS.[ReplacementOrderID]
      ,DS.[YEARMONTH]
      ,REPLACE(RDS.[formula], DS.[ReplacementString], DS.[ReplacementValue]) AS [formula]
    FROM RecursiveDataSource RDS
    INNER JOIN DataSource1 DS
    ON RDS.[ReplacementOrderID] + 1 = DS.[ReplacementOrderID]
    AND RDS.[YEARMONTH] = DS.[YEARMONTH]
)

INSERT INTO #MathTemp1
SELECT RDS.[YEARMONTH]
    ,RDS.[formula]
FROM RecursiveDataSource RDS
INNER JOIN 
(
    SELECT [YEARMONTH]
      ,MAX([ReplacementOrderID]) AS [ReplacementOrderID]
    FROM DataSource1 
    GROUP BY [YEARMONTH]
) DS
    ON RDS.[YEARMONTH] = DS.[YEARMONTH]
    AND RDS.[ReplacementOrderID] = DS.[ReplacementOrderID]
ORDER BY RDS.[YEARMONTH]
option (maxrecursion 0);

SELECT * FROM #MathTemp1

--DROP TABLE #MathLogicTable
--DROP TABLE #MathTemp1

我得到的输出显示在下面

IDNUM  YEARMONTH        OUTPUTFORMULA
1       2013        ( (CV3: 1) + (25.000) + (12.000) + (13.000) + (11.000) + (10.000) + (21.000) + (26.000) + (23.000) + (27.000) + (51.000) + (46.000) )/1000000
2       2014        ( (CV3: 1) + (25.000) + (12.000) + (13.000) + (11.000) + (10.000) + (21.000) + (26.000) + (23.000) + (27.000) + (51.000) + (46.000) )/1000000

但是所需的输出如下所示。问题是第一个字符串没有在输出公式中被替换。

 IDNUM  YEARMONTH        OUTPUTFORMULA
1       2013        ( (17.000) + (25.000) + (12.000) + (13.000) + (11.000) + (10.000) + (21.000) + (26.000) + (23.000) + (27.000) + (51.000) + (46.000) )/1000000
2       2014        ( (17.000) + (25.000) + (12.000) + (13.000) + (11.000) + (10.000) + (21.000) + (26.000) + (23.000) + (27.000) + (51.000) + (46.000) )/1000000

1 个答案:

答案 0 :(得分:1)

问题是,在您的表格创建中,对于InputNAME列,'CV3: 1 '的空格应为'CV3: 1'

CREATE TABLE #MathTemp1
(
IDNUM INTEGER IDENTITY(1,1),
YEARMONTH VARCHAR(256),
OUTPUTFORMULA VARCHAR(256)
)

CREATE TABLE #MathLogicTable
(
IDNUM INTEGER,
FORMULA Varchar(256),
INPUTName varchar(160),
AttributeValue Decimal(15,3),
yearmonth varchar(160)
)

INSERT INTO #MathLogicTable ([IDNUM], [formula], [INPUTNAME], [AttributeValue], [YEARMONTH])
VALUES ('1', '( (CV3: 1) + (CV3: 2) + (CV3: 3) + (CV3: 4) + (CV3: 5) + (CV3: 6) + (CV3: 7) + (CV3: 8) + (CV3: 9) + (CV3: 10) + (CV3: 11) + (CV3: 12) )/1000000', 'CV3: 1', '17.000', '2013')
    ,('2', '( (CV3: 1) + (CV3: 2) + (CV3: 3) + (CV3: 4) + (CV3: 5) + (CV3: 6) + (CV3: 7) + (CV3: 8) + (CV3: 9) + (CV3: 10) + (CV3: 11) + (CV3: 12) )/1000000', 'CV3: 2','25.000' ,'2013')
    ,('3', '( (CV3: 1) + (CV3: 2) + (CV3: 3) + (CV3: 4) + (CV3: 5) + (CV3: 6) + (CV3: 7) + (CV3: 8) + (CV3: 9) + (CV3: 10) + (CV3: 11) + (CV3: 12) )/1000000', 'CV3: 3','12.000' ,'2013')
    ,('4', '( (CV3: 1) + (CV3: 2) + (CV3: 3) + (CV3: 4) + (CV3: 5) + (CV3: 6) + (CV3: 7) + (CV3: 8) + (CV3: 9) + (CV3: 10) + (CV3: 11) + (CV3: 12) )/1000000', 'CV3: 4','13.000' ,'2013')
  ,('5', '( (CV3: 1) + (CV3: 2) + (CV3: 3) + (CV3: 4) + (CV3: 5) + (CV3: 6) + (CV3: 7) + (CV3: 8) + (CV3: 9) + (CV3: 10) + (CV3: 11) + (CV3: 12) )/1000000', 'CV3: 5','11.000' ,'2013')


  ,('6', '( (CV3: 1) + (CV3: 2) + (CV3: 3) + (CV3: 4) + (CV3: 5) + (CV3: 6) + (CV3: 7) + (CV3: 8) + (CV3: 9) + (CV3: 10) + (CV3: 11) + (CV3: 12) )/1000000', 'CV3: 6','10.000' ,'2013')
     ,('7', '( (CV3: 1) + (CV3: 2) + (CV3: 3) + (CV3: 4) + (CV3: 5) + (CV3: 6) + (CV3: 7) + (CV3: 8) + (CV3: 9) + (CV3: 10) + (CV3: 11) + (CV3: 12) )/1000000', 'CV3: 7','21.000' ,'2013')
    ,('8', '( (CV3: 1) + (CV3: 2) + (CV3: 3) + (CV3: 4) + (CV3: 5) + (CV3: 6) + (CV3: 7) + (CV3: 8) + (CV3: 9) + (CV3: 10) + (CV3: 11) + (CV3: 12) )/1000000', 'CV3: 8','26.000' ,'2013')
    ,('9', '( (CV3: 1) + (CV3: 2) + (CV3: 3) + (CV3: 4) + (CV3: 5) + (CV3: 6) + (CV3: 7) + (CV3: 8) + (CV3: 9) + (CV3: 10) + (CV3: 11) + (CV3: 12) )/1000000', 'CV3: 9','23.000' ,'2013')   
  ,('10', '( (CV3: 1) + (CV3: 2) + (CV3: 3) + (CV3: 4) + (CV3: 5) + (CV3: 6) + (CV3: 7) + (CV3: 8) + (CV3: 9) + (CV3: 10) + (CV3: 11) + (CV3: 12) )/1000000', 'CV3: 10','27.000' ,'2013')
  ,('11', '( (CV3: 1) + (CV3: 2) + (CV3: 3) + (CV3: 4) + (CV3: 5) + (CV3: 6) + (CV3: 7) + (CV3: 8) + (CV3: 9) + (CV3: 10) + (CV3: 11) + (CV3: 12) )/1000000', 'CV3: 11','51.000' ,'2013')
  ,('12', '( (CV3: 1) + (CV3: 2) + (CV3: 3) + (CV3: 4) + (CV3: 5) + (CV3: 6) + (CV3: 7) + (CV3: 8) + (CV3: 9) + (CV3: 10) + (CV3: 11) + (CV3: 12) )/1000000', 'CV3: 12','46.000' ,'2013')

  ,('1', '( (CV3: 1) + (CV3: 2) + (CV3: 3) + (CV3: 4) + (CV3: 5) + (CV3: 6) + (CV3: 7) + (CV3: 8) + (CV3: 9) + (CV3: 10) + (CV3: 11) + (CV3: 12) )/1000000', 'CV3: 1', '17.000', '2014')
    ,('2', '( (CV3: 1) + (CV3: 2) + (CV3: 3) + (CV3: 4) + (CV3: 5) + (CV3: 6) + (CV3: 7) + (CV3: 8) + (CV3: 9) + (CV3: 10) + (CV3: 11) + (CV3: 12) )/1000000', 'CV3: 2','25.000' ,'2014')
    ,('3', '( (CV3: 1) + (CV3: 2) + (CV3: 3) + (CV3: 4) + (CV3: 5) + (CV3: 6) + (CV3: 7) + (CV3: 8) + (CV3: 9) + (CV3: 10) + (CV3: 11) + (CV3: 12) )/1000000', 'CV3: 3','12.000' ,'2014')
    ,('4', '( (CV3: 1) + (CV3: 2) + (CV3: 3) + (CV3: 4) + (CV3: 5) + (CV3: 6) + (CV3: 7) + (CV3: 8) + (CV3: 9) + (CV3: 10) + (CV3: 11) + (CV3: 12) )/1000000', 'CV3: 4','13.000' ,'2014')
  ,('5', '( (CV3: 1) + (CV3: 2) + (CV3: 3) + (CV3: 4) + (CV3: 5) + (CV3: 6) + (CV3: 7) + (CV3: 8) + (CV3: 9) + (CV3: 10) + (CV3: 11) + (CV3: 12) )/1000000', 'CV3: 5','11.000' ,'2014')


  ,('6', '( (CV3: 1) + (CV3: 2) + (CV3: 3) + (CV3: 4) + (CV3: 5) + (CV3: 6) + (CV3: 7) + (CV3: 8) + (CV3: 9) + (CV3: 10) + (CV3: 11) + (CV3: 12) )/1000000', 'CV3: 6','10.000' ,'2014')
    ,('7', '( (CV3: 1) + (CV3: 2) + (CV3: 3) + (CV3: 4) + (CV3: 5) + (CV3: 6) + (CV3: 7) + (CV3: 8) + (CV3: 9) + (CV3: 10) + (CV3: 11) + (CV3: 12) )/1000000', 'CV3: 7','21.000' ,'2014')
    ,('8', '( (CV3: 1) + (CV3: 2) + (CV3: 3) + (CV3: 4) + (CV3: 5) + (CV3: 6) + (CV3: 7) + (CV3: 8) + (CV3: 9) + (CV3: 10) + (CV3: 11) + (CV3: 12) )/1000000', 'CV3: 8','26.000' ,'2014')
    ,('9', '( (CV3: 1) + (CV3: 2) + (CV3: 3) + (CV3: 4) + (CV3: 5) + (CV3: 6) + (CV3: 7) + (CV3: 8) + (CV3: 9) + (CV3: 10) + (CV3: 11) + (CV3: 12) )/1000000', 'CV3: 9','23.000' ,'2014')   
  ,('10', '( (CV3: 1) + (CV3: 2) + (CV3: 3) + (CV3: 4) + (CV3: 5) + (CV3: 6) + (CV3: 7) + (CV3: 8) + (CV3: 9) + (CV3: 10) + (CV3: 11) + (CV3: 12) )/1000000', 'CV3: 10','27.000' ,'2014')
  ,('11', '( (CV3: 1) + (CV3: 2) + (CV3: 3) + (CV3: 4) + (CV3: 5) + (CV3: 6) + (CV3: 7) + (CV3: 8) + (CV3: 9) + (CV3: 10) + (CV3: 11) + (CV3: 12) )/1000000', 'CV3: 11','51.000' ,'2014')
  ,('12', '( (CV3: 1) + (CV3: 2) + (CV3: 3) + (CV3: 4) + (CV3: 5) + (CV3: 6) + (CV3: 7) + (CV3: 8) + (CV3: 9) + (CV3: 10) + (CV3: 11) + (CV3: 12) )/1000000', 'CV3: 12','46.000' ,'2014')




Select * from #MathLogicTable

;WITH DataSource1 AS
( 
    SELECT ROW_NUMBER() OVER(PARTITION BY [YEARMONTH] ORDER BY [IDNUM]) AS [ReplacementOrderID]
      ,[YEARMONTH]
      ,[formula]
      ,[INPUTNAME] AS [ReplacementString]
      ,AttributeValue AS [ReplacementValue]
    FROM #MathLogicTable
)

,RecursiveDataSource AS
(
    SELECT [ReplacementOrderID]
      ,[YEARMONTH]
      ,REPLACE([formula], [ReplacementString], [ReplacementValue]) AS [formula]
    FROM DataSource1 
    WHERE [ReplacementOrderID] = 1
    UNION ALL
    SELECT DS.[ReplacementOrderID]
      ,DS.[YEARMONTH]
      ,REPLACE(RDS.[formula], DS.[ReplacementString], DS.[ReplacementValue]) AS [formula]
    FROM RecursiveDataSource RDS
    INNER JOIN DataSource1 DS
    ON RDS.[ReplacementOrderID] + 1 = DS.[ReplacementOrderID]
    AND RDS.[YEARMONTH] = DS.[YEARMONTH]
)

INSERT INTO #MathTemp1
SELECT RDS.[YEARMONTH]
    ,RDS.[formula]
FROM RecursiveDataSource RDS
INNER JOIN 
(
    SELECT [YEARMONTH]
      ,MAX([ReplacementOrderID]) AS [ReplacementOrderID]
    FROM DataSource1 
    GROUP BY [YEARMONTH]
) DS
    ON RDS.[YEARMONTH] = DS.[YEARMONTH]
    AND RDS.[ReplacementOrderID] = DS.[ReplacementOrderID]
ORDER BY RDS.[YEARMONTH]
option (maxrecursion 0);

SELECT * FROM #MathTemp1

--DROP TABLE #MathLogicTable
--DROP TABLE #MathTemp1