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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. update Query with Order by Clause- SQL Server2008

update Query with Order by Clause- SQL Server2008

Scheduled Pinned Locked Moved Database
databasesaleshelpannouncement
7 Posts 5 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.
  • P Offline
    P Offline
    Paramu1973
    wrote on last edited by
    #1

    I have a update query, But need with Order by Clause. I cannot identify the error.... update sales_master set un_rate = (select qtnmaster.un_rate where qtnmaster.itemcode=sales_master.itemcode order by (0+qtnmaster.Qtnno)) Thanks For The Ideas... :thumbsup:

    M I L C 4 Replies Last reply
    0
    • P Paramu1973

      I have a update query, But need with Order by Clause. I cannot identify the error.... update sales_master set un_rate = (select qtnmaster.un_rate where qtnmaster.itemcode=sales_master.itemcode order by (0+qtnmaster.Qtnno)) Thanks For The Ideas... :thumbsup:

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

      Why on earth would you need an order by in an update clause (illegal for a start) you are identifying the individual records to update!

      Never underestimate the power of human stupidity RAH

      I 1 Reply Last reply
      0
      • M Mycroft Holmes

        Why on earth would you need an order by in an update clause (illegal for a start) you are identifying the individual records to update!

        Never underestimate the power of human stupidity RAH

        I Offline
        I Offline
        i j russell
        wrote on last edited by
        #3

        You can use an ORDER BY clause in an UPDATE statement as long as it is in a subquery.

        1 Reply Last reply
        0
        • P Paramu1973

          I have a update query, But need with Order by Clause. I cannot identify the error.... update sales_master set un_rate = (select qtnmaster.un_rate where qtnmaster.itemcode=sales_master.itemcode order by (0+qtnmaster.Qtnno)) Thanks For The Ideas... :thumbsup:

          I Offline
          I Offline
          i j russell
          wrote on last edited by
          #4

          Have a look at CROSS APPLY.

          1 Reply Last reply
          0
          • P Paramu1973

            I have a update query, But need with Order by Clause. I cannot identify the error.... update sales_master set un_rate = (select qtnmaster.un_rate where qtnmaster.itemcode=sales_master.itemcode order by (0+qtnmaster.Qtnno)) Thanks For The Ideas... :thumbsup:

            L Offline
            L Offline
            loyal ginger
            wrote on last edited by
            #5

            There is no problem with the order by clause. However, your subquery may return multiple values, which makes the assignment not valid. You need to refine your subquery so that only the desired value is returned. For example, when you use order by clause and use "top 1", then the "un_rate" of the record with the smallest "Qtnno" value is returned. Without a clear knowledge of your original intension, I don't have a clear answer to your question.

            modified on Tuesday, January 12, 2010 11:02 AM

            1 Reply Last reply
            0
            • P Paramu1973

              I have a update query, But need with Order by Clause. I cannot identify the error.... update sales_master set un_rate = (select qtnmaster.un_rate where qtnmaster.itemcode=sales_master.itemcode order by (0+qtnmaster.Qtnno)) Thanks For The Ideas... :thumbsup:

              C Offline
              C Offline
              Corporal Agarn
              wrote on last edited by
              #6

              In T-SQL

              Update sales_master
              set un_rate = qtnmaster.un_rate
              from sales_master
              inner join qtnmaster
              ON sales_master.itemcode = qtnmaster.itemcode

              I 1 Reply Last reply
              0
              • C Corporal Agarn

                In T-SQL

                Update sales_master
                set un_rate = qtnmaster.un_rate
                from sales_master
                inner join qtnmaster
                ON sales_master.itemcode = qtnmaster.itemcode

                I Offline
                I Offline
                i j russell
                wrote on last edited by
                #7

                This will only work if there are no more than 1 record in qtnmaster per itemcode.

                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