选择中更新中的动态列名称

时间:2019-04-01 07:35:54

标签: sql-server

我想从UPDATE FROM SELECT语句动态检索列,但遇到错误

我想更新t_ipd表中的{e.region}+'_CAP'。这里的e.region是动态的,它来自select语句。但是e.region+'_CAP' =提供了错误。请告知

请在下面找到代码

update t_ipd set e.region+'_CAP' = x.RGN_USD_CAP

from 
(

  (select e.region, r.REQUEST_ID as req_id,SUM(e.TOTAL_CAPITAL*e.CONVERSION_RATE) as RGN_USD_CAP,
  SUM(e.AMORT_TOTAL_EXP*e.CONVERSION_RATE) as RGN_USD_EXP
       from

       t_ipd i inner join t_ct_request r on i.T_Code = r.REQUEST_ID 
       inner join  t_ct_request_estimation re
       on r.REQUEST_ID = re.REQUEST_ID inner join t_ct_estimation e
       on re.REQUEST_EST_ID = e.REQUEST_EST_ID 

       where  e.region != 'EMEA'
       --and e.country!='BELGIUM' 
       and    re.REQUEST_ID in 
       (
       SELECT
              DISTINCT TOP 2147483647  REQUEST_ID
       FROM
              T_CT_REQUEST
       where

              IS_DELETED = 'N'
              AND NO_OF_CHILD_REQ != -1   
       )GROUP BY e.REGION,r.request_id)
       union
       (select e.region, r.REQUEST_ID as req_id,SUM(e.TOTAL_CAPITAL*e.CONVERSION_RATE) as RGN_USD_CAP,
       SUM(e.AMORT_TOTAL_EXP*e.CONVERSION_RATE)  as RGN_USD_EXP
       from

       t_ipd i inner join t_ct_request r on i.T_Code = r.REQUEST_ID 
       inner join  t_ct_request_estimation re
       on r.REQUEST_ID = re.REQUEST_ID inner join t_ct_estimation e
       on re.REQUEST_EST_ID = e.REQUEST_EST_ID 

       where  e.region = 'EMEA' 
       and    re.REQUEST_ID in 
       (
       SELECT
              DISTINCT TOP 2147483647  REQUEST_ID
       FROM
              T_CT_REQUEST
       where

              IS_DELETED = 'N'
              AND NO_OF_CHILD_REQ != -1   
       )GROUP BY e.REGION,r.request_id)
)x
where t_ipd.t_code = x.req_id

1 个答案:

答案 0 :(得分:0)

我会为此使用枢轴...

UPDATE t 
SET A_CAP = A, B_CAP = B, C.CAP = C, D.CAP = D
FROM t_ipd t
JOIN (
    SELECT 
        e.region, 
        r.REQUEST_ID AS req_id,
        SUM(e.TOTAL_CAPITAL*e.CONVERSION_RATE) as RGN_USD_CAP,
        SUM(e.AMORT_TOTAL_EXP*e.CONVERSION_RATE) as RGN_USD_EXP
    FROM t_ct_request r 
    JOIN t_ct_request_estimation re ON r.REQUEST_ID = re.REQUEST_ID 
    JOIN t_ct_estimation e ON re.REQUEST_EST_ID = e.REQUEST_EST_ID 
    WHERE 
        r.REQUEST_ID AND r.IS_DELETED = 'N' AND r.NO_OF_CHILD_REQ != -1   
    GROUP BY e.REGION,r.request_id
) x
PIVOT (
    MAX(RGN_USD_CAP) FOR Region IN ([A], [B], [C], [D])
) pvt ON t.t_code = pvt.req_id

如果区域列表很大,则可以使用动态SQL

DECLARE @SQL NVARCHAR(MAX) 

SELECT abc = STUFF((
            SELECT DISTINCT ', ' + QUOTENAME(region + '_CAP') + ' = ' + QUOTENAME(region)
            FROM t_ct_request
            FOR XML PATH('')
            ), 1, 1, '')

SET @SQL = N'
UPDATE t 
SET ' + STUFF((
            SELECT DISTINCT ', ' + QUOTENAME(region + '_CAP') + ' = ' + QUOTENAME(region)
            FROM t_ct_request
            FOR XML PATH('')
            ), 1, 1, '') + '
FROM t_ipd t
JOIN (
    SELECT 
        e.region, 
        r.REQUEST_ID AS req_id,
        SUM(e.TOTAL_CAPITAL*e.CONVERSION_RATE) as RGN_USD_CAP,
        SUM(e.AMORT_TOTAL_EXP*e.CONVERSION_RATE) as RGN_USD_EXP
    FROM t_ct_request r 
    JOIN t_ct_request_estimation re ON r.REQUEST_ID = re.REQUEST_ID 
    JOIN t_ct_estimation e ON re.REQUEST_EST_ID = e.REQUEST_EST_ID 
    WHERE 
        r.REQUEST_ID AND r.IS_DELETED = 'N' AND r.NO_OF_CHILD_REQ != -1   
    GROUP BY e.REGION,r.request_id
) x
PIVOT (
    MAX(RGN_USD_CAP) FOR Region IN (' + STUFF((
            SELECT DISTINCT ', ' + QUOTENAME(ProductID)
            FROM MyTable
            FOR XML PATH('')
            ), 1, 1, '') + ')
) pvt ON t.t_code = pvt.req_id'

EXEC (@SQL)

注意,我在您的UNION查询中没有发现任何区别,并且IN表达式过于冗长(如果您仍然需要它们,请检查我的以前的版本并使用EXISTS),我删除了不必要的代码并优化了查询