Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server TSQL更新select语句中的记录_Sql Server_Tsql_Ssrs 2012 - Fatal编程技术网

Sql server TSQL更新select语句中的记录

Sql server TSQL更新select语句中的记录,sql-server,tsql,ssrs-2012,Sql Server,Tsql,Ssrs 2012,当通过select语句检索数据集时,我希望根据每个检索到的记录的列中的值更新现有记录。我不能使用函数更新记录,也不能在select语句中使用存储过程。我还指出,我不能通过从函数执行存储过程来规避这些限制 有什么想法吗?下面是我当前的代码和注释,我觉得我需要调用一些东西来更新记录。这段代码将在SSRS中使用,因此一个解决方案可能是在报表自定义代码中执行存储过程,但我也无法让它工作 select sw.SMACTVSEQ as JDE_ActiveRec ,sw.SMCSSEQ as JD

当通过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

在数据查询之前,将更新查询放在报表查询的TSQL中。只要您的查询有效且您有权限,SSRS将运行TSQL来更新数据、使用临时表等。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

这对你有用吗:

    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\u Mode变量,否则会出现错误。

select就是select。更新就是更新。似乎您不需要调用一个strored proc来同时执行这两个操作……您不能在同一个语句中混合使用不同类型的语句。您不能在选择中更新,或在插入中删除,或诸如此类的操作。谢谢您的评论。他们只是证实了我已经知道的。我只是希望找到一种在检索记录时进行更新的方法。这就是我要做的。我仍然很惊讶有多少人认为tat SSR只能做单选语句。谢谢你的建议。不幸的是,ab.ABURRF是一个随每个选定记录而变化的变量,在select语句之前是未知的。