TSQL更新select语句中的记录

时间:2018-11-08 22:38:24

标签: sql-server tsql ssrs-2012

通过select语句检索数据集时,我想基于每个检索到的记录的列中的值来更新现有记录。我不能使用函数来更新记录,也不能在select语句中使用存储过程。我还认为我无法通过从函数执行存储过程来规避这些限制。

有什么想法吗?以下是带有注释的当前代码,我觉得我需要调用一些会更新记录的内容。该代码将在SSRS中使用,因此一种解决方案可能是在报表自定义代码中执行存储过程,但我也无法使它正常工作。

select sw.SMACTVSEQ as JDE_ActiveRec 
    ,sw.SMCSSEQ as JDE_SalesSeq
    ,bu.MCRP04 as JDE_DivID
    ,sw.SMHBMCUS as JDE_CommID
    ,cm.CMDL01 as JDE_CommunityName
    ,rtrim(sw.SMMCU) as JDE_LotID
    ,sw.SMBYR as JDE_BuyerABNo
    ,JDE_SSID = case
        When ab.ABURRF is null then cast(0 as int)
        When ab.ABURRF = '' then cast(0 as int)
        else cast(ab.ABURRF as int)
        end
    ,ss.Customer_ID as SS_CustID
    ,ss.Lot_ID as SS_LotID
    ,ss.Customer_Status as SS_CustStatus
    ,[dbo].[udf_ConvertJDEdate](sw.SMCDJ) as JDE_DateClosed
    ,case when ab.ABURRF >0 then 'Manually Update'
        else
            case when @Update_Mode ='Yes' then
            'Yes/Error' 
            /* **************
               Replace 'Yes/Error' with procedure to update  
               JDE_F0101_ABURRF and return 'Yes' or 'Error'
               ************** */
            else @Update_Mode
            end
        end as Update_Mode
    from [dbo].[crp_F44H501] sw
        left outer join [dbo].[crp_F44H101] cm 
            on cm.[CMHBMCUS]=sw.smhbmcus and cm.[CMCPHASE]=sw.[SMCPHASE] 
        left outer join [dbo].[crp_F0006] bu
            on bu.mcmcu = sw.smmcu 
        left outer join [dbo].[stg_F0101] ab
            on ab.aban8 = sw.SMBYR
        left outer join (
            select distinct(lot_id)
                ,customer_ID
                ,customer_status
            from [dbo].[SS_FactDemographic]
                        ) ss
            on ltrim(ss.lot_id) = ltrim(sw.SMMCU)

    Where sw.smactvseq='1' 
        and sw.SMBYR > 0
        and ab.ABURRF <> ss.Customer_ID
        and ss.Customer_Status = 'Buyer'
        and (bu.MCRP04 = @Division_ID or @Division_ID ='All')
        and (ltrim(sw.SMHBMCUS) = @Community_ID or @Community_ID ='All')
    Order by JDE_DivID,JDE_CommID,JDE_LotID

2 个答案:

答案 0 :(得分:1)

在数据查询之前将UPDATE查询添加到报表查询的TSQL中。只要您的查询有效且具有权限,SSRS就会运行TSQL来更新数据,使用TEMP表等。SSRS查询编辑器可能不喜欢您可以做的所有事情,因此您可能需要在SSMS中进行查询(无论如何我通常都会这样做。

我认为您的查询将类似于:

UPDATE sw
SET Update_Mode = CASE WHEN ab.ABURRF >0 THEN 'Manually Update'
                        ELSE CASE WHEN @Update_Mode ='Yes' THEN 'Yes/Error' ELSE @Update_Mode END
                        END
FROM [dbo].[crp_F44H501] sw  
INNER JOIN [dbo].[stg_F0101] ab on ab.aban8 = sw.SMBYR


select sw.SMACTVSEQ as JDE_ActiveRec 
    ,sw.SMCSSEQ as JDE_SalesSeq
    ,bu.MCRP04 as JDE_DivID
    ,sw.SMHBMCUS as JDE_CommID
    ,cm.CMDL01 as JDE_CommunityName
    ,rtrim(sw.SMMCU) as JDE_LotID
    ,sw.SMBYR as JDE_BuyerABNo
    ,JDE_SSID = case
        When ab.ABURRF is null then cast(0 as int)
        When ab.ABURRF = '' then cast(0 as int)
        else cast(ab.ABURRF as int)
        end
    ,ss.Customer_ID as SS_CustID
    ,ss.Lot_ID as SS_LotID
    ,ss.Customer_Status as SS_CustStatus
    ,[dbo].[udf_ConvertJDEdate](sw.SMCDJ) as JDE_DateClosed
    ,sw.Update_Mode
    from [dbo].[crp_F44H501] sw
        left outer join [dbo].[crp_F44H101] cm 
            on cm.[CMHBMCUS]=sw.smhbmcus and cm.[CMCPHASE]=sw.[SMCPHASE] 
        left outer join [dbo].[crp_F0006] bu
            on bu.mcmcu = sw.smmcu 
        left outer join [dbo].[stg_F0101] ab
            on ab.aban8 = sw.SMBYR
        left outer join (
            select distinct(lot_id)
                ,customer_ID
                ,customer_status
            from [dbo].[SS_FactDemographic]
                        ) ss
            on ltrim(ss.lot_id) = ltrim(sw.SMMCU)
    Where sw.smactvseq='1' 
        and sw.SMBYR > 0
        and ab.ABURRF <> ss.Customer_ID
        and ss.Customer_Status = 'Buyer'
        and (bu.MCRP04 = @Division_ID or @Division_ID ='All')
        and (ltrim(sw.SMHBMCUS) = @Community_ID or @Community_ID ='All')
    Order by JDE_DivID,JDE_CommID,JDE_LotID

答案 1 :(得分:0)

这项工作对您有用吗

    update X set Update_Mode =  case when X.ABURRF      >   0   then 'Manually Update'  else
                                case when @Update_Mode  =   ''  then 'Yes/Error'        else    @Update_Mode
                                end
                                end
    from
    (
    select
        sw.SMACTVSEQ        as  JDE_ActiveRec 
    ,   sw.SMCSSEQ          as  JDE_SalesSeq
    ,   bu.MCRP04           as  JDE_DivID
    ,   sw.SMHBMCUS         as  JDE_CommID
    ,   cm.CMDL01           as  JDE_CommunityName
    ,   rtrim(sw.SMMCU)     as  JDE_LotID
    ,   sw.SMBYR            as  JDE_BuyerABNo
    ,   JDE_SSID            =   case
                                When ab.ABURRF is null  then cast(0 as int)
                                When ab.ABURRF = ''     then cast(0 as int)
                                else                    cast(ab.ABURRF as int)
                                end
    ,   ss.Customer_ID as SS_CustID
    ,   ss.Lot_ID as SS_LotID
    ,   ss.Customer_Status as SS_CustStatus
    ,   [dbo].[udf_ConvertJDEdate](sw.SMCDJ) as JDE_DateClosed
    ,   ab.ABURRF
    ,   ''                  as  Update_Mode
    --, case when ab.ABURRF >0 then 'Manually Update'
    --    else
    --        case when @Update_Mode ='Yes' then
    --        'Yes/Error' 
    --        /* **************
    --            Replace 'Yes/Error' with procedure to update  
    --            JDE_F0101_ABURRF and return 'Yes' or 'Error'
    --            ************** */
    --        else @Update_Mode
    --        end
    --    end as Update_Mode
    from            [dbo].[crp_F44H501] sw
    left outer join [dbo].[crp_F44H101] cm on cm.[CMHBMCUS]=sw.smhbmcus and cm.[CMCPHASE]=sw.[SMCPHASE] 
    left outer join [dbo].[crp_F0006] bu    on bu.mcmcu = sw.smmcu 
    left outer join [dbo].[stg_F0101] ab    on ab.aban8 = sw.SMBYR
    left outer join (select distinct(lot_id),customer_ID,customer_status from [dbo].[SS_FactDemographic]) ss  on ltrim(ss.lot_id) = ltrim(sw.SMMCU)

    Where   sw.smactvseq='1' 
    and     sw.SMBYR > 0
    and     ab.ABURRF <> ss.Customer_ID
    and     ss.Customer_Status = 'Buyer'
    and     (bu.MCRP04 = @Division_ID or @Division_ID ='All')
    and     (ltrim(sw.SMHBMCUS) = @Community_ID or @Community_ID ='All')
    )   X

    --Order by  JDE_DivID
    --,         JDE_CommID
    --,         JDE_LotID

请记住,必须在运行此更新之前声明@Update_Mode变量,否则会出现错误。