Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. refresh values after each row update [modified]

refresh values after each row update [modified]

Scheduled Pinned Locked Moved Database
databasetoolshelpannouncement
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mersad00
    wrote on last edited by
    #1

    hi everybody, I have a big problem with update statement, let me mention the situation by exact script and results! first create trans table=>

    create table trans(
    id bigint identity(1,1) not null,
    price decimal(18,0) null,
    relatedid bigint null)

    Then fill it by this script

    Insert into trans (price,relatedid)values(100,NULL)
    Insert into trans (price,relatedid)values(100,NULL)
    Insert into trans (price,relatedid)values(500,NULL)
    Insert into trans (price,relatedid)values(1,NULL)
    Insert into trans (price,relatedid)values(1,3)

    Now run this query!

    Update trans
    set price=(case when relatedid is null then (select avg(price) from trans where id=
    After execution of query I expect this result:
    id-----price-----relatedid
    1 100 NULL
    2 300 NULL
    3 200 NULL
    4 200 3 <======== realy expected!!!!

    But this is the result!!!
    id-----price-----relatedid
    1 100 NULL
    2 300 NULL
    3 200 NULL <======= #3 row
    4 1 3 <===== wrong value, i mean this is the value before update

    row with id 3 has been updated before but update doesnt know this!!!!!

    WHAT I NEED i need to get expected result from this execution!!!! plzzzzzzzzzzz

    modified on Tuesday, April 12, 2011 10:34 AM

    M W 2 Replies Last reply
    0
    • M mersad00

      hi everybody, I have a big problem with update statement, let me mention the situation by exact script and results! first create trans table=>

      create table trans(
      id bigint identity(1,1) not null,
      price decimal(18,0) null,
      relatedid bigint null)

      Then fill it by this script

      Insert into trans (price,relatedid)values(100,NULL)
      Insert into trans (price,relatedid)values(100,NULL)
      Insert into trans (price,relatedid)values(500,NULL)
      Insert into trans (price,relatedid)values(1,NULL)
      Insert into trans (price,relatedid)values(1,3)

      Now run this query!

      Update trans
      set price=(case when relatedid is null then (select avg(price) from trans where id=
      After execution of query I expect this result:
      id-----price-----relatedid
      1 100 NULL
      2 300 NULL
      3 200 NULL
      4 200 3 <======== realy expected!!!!

      But this is the result!!!
      id-----price-----relatedid
      1 100 NULL
      2 300 NULL
      3 200 NULL <======= #3 row
      4 1 3 <===== wrong value, i mean this is the value before update

      row with id 3 has been updated before but update doesnt know this!!!!!

      WHAT I NEED i need to get expected result from this execution!!!! plzzzzzzzzzzz

      modified on Tuesday, April 12, 2011 10:34 AM

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      It sounds like you have processing half in your database and half in you application. Change the design to move all the processing into one platform or make sure the database returns the results from the update (possibly with a simple select from updated record changing the return type to a datatable)

      Never underestimate the power of human stupidity RAH

      1 Reply Last reply
      0
      • M mersad00

        hi everybody, I have a big problem with update statement, let me mention the situation by exact script and results! first create trans table=>

        create table trans(
        id bigint identity(1,1) not null,
        price decimal(18,0) null,
        relatedid bigint null)

        Then fill it by this script

        Insert into trans (price,relatedid)values(100,NULL)
        Insert into trans (price,relatedid)values(100,NULL)
        Insert into trans (price,relatedid)values(500,NULL)
        Insert into trans (price,relatedid)values(1,NULL)
        Insert into trans (price,relatedid)values(1,3)

        Now run this query!

        Update trans
        set price=(case when relatedid is null then (select avg(price) from trans where id=
        After execution of query I expect this result:
        id-----price-----relatedid
        1 100 NULL
        2 300 NULL
        3 200 NULL
        4 200 3 <======== realy expected!!!!

        But this is the result!!!
        id-----price-----relatedid
        1 100 NULL
        2 300 NULL
        3 200 NULL <======= #3 row
        4 1 3 <===== wrong value, i mean this is the value before update

        row with id 3 has been updated before but update doesnt know this!!!!!

        WHAT I NEED i need to get expected result from this execution!!!! plzzzzzzzzzzz

        modified on Tuesday, April 12, 2011 10:34 AM

        W Offline
        W Offline
        Wendelius
        wrote on last edited by
        #3

        So are you saying that the function doesn't see the refreshed situation. If that's the case, perhaps you're running the update in a different transaction or you are feeding 'old' values as parameters to the function. The function should be able to fetch the current data from the database even if the transaction is still going on as long as you use the same transaction.

        The need to optimize rises from a bad design.My articles[^]

        M 1 Reply Last reply
        0
        • W Wendelius

          So are you saying that the function doesn't see the refreshed situation. If that's the case, perhaps you're running the update in a different transaction or you are feeding 'old' values as parameters to the function. The function should be able to fetch the current data from the database even if the transaction is still going on as long as you use the same transaction.

          The need to optimize rises from a bad design.My articles[^]

          M Offline
          M Offline
          mersad00
          wrote on last edited by
          #4

          plz take look at edited message...

          W 1 Reply Last reply
          0
          • M mersad00

            plz take look at edited message...

            W Offline
            W Offline
            Wendelius
            wrote on last edited by
            #5

            Answerred the later post.

            The need to optimize rises from a bad design.My articles[^]

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups