pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

動態游標不反映對基表的更新

Dynamic cursor not reflecting updates to base table(動態游標不反映對基表的更新)
本文介紹了動態游標不反映對基表的更新的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

據我所知,使用 dynamic 游標將反映對基表所做的任何更改.為什么在下面的例子中沒有出現這種情況?

It is my understanding that with a cursor that is dynamic will reflect any changes that are made to the base tables. Why doesn't that occur in the following example ?

我用一個表變量和一個具有相同結果的實際表嘗試了這個.如果 @@fetch_status 循環開始后的那行沒有被注釋,我會得到我期望的結果.

I tried this with a table variable and an actual table with the same results. If the line after the beginning of the @@fetch_status loop is uncommented I get the results I expect.

declare @BalanceTable table
(
    LineId int not null identity(1, 1),
    Qty int not null,
    Price   money not null
)

insert into @BalanceTable (Qty, Price) values (3000, 1)
insert into @BalanceTable (Qty, Price) values (40, 2)
insert into @BalanceTable (Qty, Price) values (1, 1)
insert into @BalanceTable (Qty, Price) values (2000, 1)
insert into @BalanceTable (Qty, Price) values (4047, 2)
insert into @BalanceTable (Qty, Price) values (-3000, 1)
insert into @BalanceTable (Qty, Price) values (-38, 2)
insert into @BalanceTable (Qty, Price) values (3000, 1)

declare BalanceTable cursor
  dynamic for
    select LineId, Qty, Price
      from @BalanceTable
      order by LineId

declare @LineId int
declare @Qty int
declare @Price money

open BalanceTable

fetch next from BalanceTable into @LineId, @Qty, @Price

while @@fetch_status = 0
begin
    -- select @Qty = Qty, @Price = Price from @BalanceTable where LineId = @LineId

    declare @SearchLessZero bit
    set @SearchLessZero = case when @Qty > 0 then 1 else 0 end

    declare @OffsetLineId int
    declare @OffsetQty int
    set @OffsetLineId = -1

    while @Qty > 0 and @OffsetLineId is not null
    begin
        select @OffsetLineId = min(LineId)
          from @BalanceTable
          where LineId > @LineId and Price = @Price and 
            ((@SearchLessZero = 1 and Qty < 0) or (@SearchLessZero = 0 and Qty > 0))

        if @OffsetLineId is not null
        begin
            select @OffsetQty = Qty
              from @BalanceTable
              where LineId = @OffsetLineId

            if @Qty > -@OffsetQty
            begin
                set @Qty = @Qty + @OffsetQty
                set @OffsetQty = 0
            end
            else
            begin
                set @OffsetQty = @OffsetQty + @Qty
                set @Qty = 0
            end

            update @BalanceTable set Qty = @OffsetQty where LineId =     @OffsetLineId
        end
    end

    update @BalanceTable set Qty = @Qty where LineId = @LineId

    fetch next from BalanceTable into @LineId, @Qty, @Price
end

close BalanceTable
deallocate BalanceTable

select *
  from @BalanceTable
  order by LineId

推薦答案

在動態游標中只允許很少的執行計劃運算符.如果游標查詢的執行計劃包含不允許的操作符,游標將轉換為快照游標,因此不會看到更新.

Only very few execution plan operators are permitted in a dynamic cursor. If the execution plan for the cursor query contains a non-permitted operator, the cursor gets converted into a snapshot cursor and hence does not see updates.

如果您查看游標的執行計劃,您會發現就是這樣:

if you look at the execution plan for your cursor you see that just that happened:

查詢中的問題運算符是排序.刪除它,您將看到更新.

The problem operator in your query is the sort. Remove it and you will see updates.

如果需要對數據進行排序,在表中添加聚集索引,這樣ORDER BY就不需要排序運算符了.

If you need the data sorted, add a clustered index to the table, so that the ORDER BY does not require a sort operator.

這篇關于動態游標不反映對基表的更新的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Modify Existing decimal places info(修改現有小數位信息)
The correlation name #39;CONVERT#39; is specified multiple times(多次指定相關名稱“CONVERT)
T-SQL left join not returning null columns(T-SQL 左連接不返回空列)
remove duplicates from comma or pipeline operator string(從逗號或管道運算符字符串中刪除重復項)
Change an iterative query to a relational set-based query(將迭代查詢更改為基于關系集的查詢)
concatenate a zero onto sql server select value shows 4 digits still and not 5(將零連接到 sql server 選擇值仍然顯示 4 位而不是 5)
主站蜘蛛池模板: 烟气在线监测系统_烟气在线监测仪_扬尘检测仪_空气质量监测站「山东风途物联网」 | 厚壁钢管-厚壁无缝钢管-小口径厚壁钢管-大口径厚壁钢管 - 聊城宽达钢管有限公司 | OpenI 启智 新一代人工智能开源开放平台| 不锈钢水箱生产厂家_消防水箱生产厂家-河南联固供水设备有限公司 | 高低温老化试验机-步入式/低温恒温恒湿试验机-百科 | 长沙发电机-湖南发电机-柴油发电机供应厂家-长沙明邦智能科技 | 采暖炉_取暖炉_生物质颗粒锅炉_颗粒壁炉_厂家加盟批发_烟台蓝澳采暖设备有限公司 | 奥因-光触媒除甲醛公司-除甲醛加盟公司十大品牌 | 胶原检测试剂盒,弹性蛋白检测试剂盒,类克ELISA试剂盒,阿达木单抗ELISA试剂盒-北京群晓科苑生物技术有限公司 | Eiafans.com_环评爱好者 环评网|环评论坛|环评报告公示网|竣工环保验收公示网|环保验收报告公示网|环保自主验收公示|环评公示网|环保公示网|注册环评工程师|环境影响评价|环评师|规划环评|环评报告|环评考试网|环评论坛 - Powered by Discuz! | 不锈钢复合板厂家_钛钢复合板批发_铜铝复合板供应-威海泓方金属复合材料股份有限公司 | 传动滚筒_厂家-淄博海恒机械制造厂 | 礼仪庆典公司,礼仪策划公司,庆典公司,演出公司,演艺公司,年会酒会,生日寿宴,动工仪式,开工仪式,奠基典礼,商务会议,竣工落成,乔迁揭牌,签约启动-东莞市开门红文化传媒有限公司 | 恒温槽_恒温水槽_恒温水浴槽-上海方瑞仪器有限公司 | 小学教案模板_中学教师优秀教案_高中教学设计模板_教育巴巴 | 高光谱相机-近红外高光谱相机厂家-高光谱成像仪-SINESPEC 赛斯拜克 | 骨密度检测仪_骨密度分析仪_骨密度仪_动脉硬化检测仪专业生产厂家【品源医疗】 | 吊篮式|移动式冷热冲击试验箱-二槽冷热冲击试验箱-广东科宝 | 环压强度试验机-拉链拉力试验机-上海倾技仪器仪表科技有限公司 | 山东限矩型液力偶合器_液力耦合器易熔塞厂家-淄博市汇川源机械厂 | 防水套管厂家_刚性防水套管_柔性防水套管_不锈钢防水套管-郑州中泰管道 | 碳钢法兰厂家,非标法兰,定制异型,法兰生产厂家-河北九瑞管道 | 凝胶成像仪,化学发光凝胶成像系统,凝胶成像分析系统-上海培清科技有限公司 | 美国查特CHART MVE液氮罐_查特杜瓦瓶_制造全球品质液氮罐 | 合肥网带炉_安徽箱式炉_钟罩炉-合肥品炙装备科技有限公司 | 氧化锆纤维_1800度高温退火炉_1800度高温烧结炉-南京理工宇龙新材料股份有限公司 | 电销卡 防封电销卡 不封号电销卡 电话销售卡 白名单电销卡 电销系统 外呼系统 | 二手光谱仪维修-德国OBLF光谱仪|进口斯派克光谱仪-热电ARL光谱仪-意大利GNR光谱仪-永晖检测 | 桐城新闻网—桐城市融媒体中心主办| 【孔氏陶粒】建筑回填陶粒-南京/合肥/武汉/郑州/重庆/成都/杭州陶粒厂家 | 煤矿人员精确定位系统_矿用无线通信系统_煤矿广播系统 | 小程序开发公司-小程序制作-微信小程序开发-小程序定制-咏熠软件 | 杭州高温泵_热水泵_高温油泵|昆山奥兰克泵业制造有限公司 | 膏方加工_丸剂贴牌_膏滋代加工_湖北康瑞生物科技有限公司 | 重庆轻质隔墙板-重庆安吉升科技有限公司| 河南卓美创业科技有限公司-河南卓美防雷公司-防雷接地-防雷工程-重庆避雷针-避雷器-防雷检测-避雷带-避雷针-避雷塔、机房防雷、古建筑防雷等-山西防雷公司 | 步进_伺服_行星减速机,微型直流电机,大功率直流电机-淄博冠意传动机械 | 打造全球沸石生态圈 - 国投盛世| 杭州厂房降温,车间降温设备,车间通风降温,厂房降温方案,杭州嘉友实业爽风品牌 | 济宁工业提升门|济宁电动防火门|济宁快速堆积门-济宁市统一电动门有限公司 | 合肥卓创建筑装饰,专业办公室装饰、商业空间装修与设计。 |