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. MAX () function with INSERT INTO?

MAX () function with INSERT INTO?

Scheduled Pinned Locked Moved Database
helpquestion
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.
  • L Offline
    L Offline
    Luka Grabarevic
    wrote on last edited by
    #1

    hi I have following problem. i'd like to use the MAX() function to get the highest ID from a table. i've used is lik this: INSERT INTO Table (ID, Employee, Project, Date, ...) VALUES (MAX(ID) + 1,@Employee,@Project,@Date, ...) but this is not working. i've searched a bit in the internet that it's not possible to put functions into VALUES. so i'd like to know another way to solve this problem. thanks in advance greetz pdluke PS: sorry for my bad english

    P S D 3 Replies Last reply
    0
    • L Luka Grabarevic

      hi I have following problem. i'd like to use the MAX() function to get the highest ID from a table. i've used is lik this: INSERT INTO Table (ID, Employee, Project, Date, ...) VALUES (MAX(ID) + 1,@Employee,@Project,@Date, ...) but this is not working. i've searched a bit in the internet that it's not possible to put functions into VALUES. so i'd like to know another way to solve this problem. thanks in advance greetz pdluke PS: sorry for my bad english

      P Offline
      P Offline
      Paddy Boyd
      wrote on last edited by
      #2

      You could either make the id an identity column, or you can get the value using a sub select:

      insert into A
      (id, ....)
      values
      ((SELECT MAX(ID) FROM A), ...)

      L 1 Reply Last reply
      0
      • P Paddy Boyd

        You could either make the id an identity column, or you can get the value using a sub select:

        insert into A
        (id, ....)
        values
        ((SELECT MAX(ID) FROM A), ...)

        L Offline
        L Offline
        Luka Grabarevic
        wrote on last edited by
        #3

        yes an sub select is one possibility, but I'm using MSSQL and an sub select is not supported like in MySQL.

        1 Reply Last reply
        0
        • L Luka Grabarevic

          hi I have following problem. i'd like to use the MAX() function to get the highest ID from a table. i've used is lik this: INSERT INTO Table (ID, Employee, Project, Date, ...) VALUES (MAX(ID) + 1,@Employee,@Project,@Date, ...) but this is not working. i've searched a bit in the internet that it's not possible to put functions into VALUES. so i'd like to know another way to solve this problem. thanks in advance greetz pdluke PS: sorry for my bad english

          S Offline
          S Offline
          samerh
          wrote on last edited by
          #4

          Why don't you make the ID a primary key and set its Identity to true, then it will auto increment by 1.

          L 1 Reply Last reply
          0
          • S samerh

            Why don't you make the ID a primary key and set its Identity to true, then it will auto increment by 1.

            L Offline
            L Offline
            Luka Grabarevic
            wrote on last edited by
            #5

            the problem is that I'm using this database via a WCF-Service. so multiuser accessibility is the main problem. If 2 users are saving their date at the same, who would this be solved with assigning the IDs? so I've tryed this with MAX(). I really don't know how it would act with the IDs. I would be grateful if someone could tell me if this could work like you say. greetz pdluke

            A 1 Reply Last reply
            0
            • L Luka Grabarevic

              the problem is that I'm using this database via a WCF-Service. so multiuser accessibility is the main problem. If 2 users are saving their date at the same, who would this be solved with assigning the IDs? so I've tryed this with MAX(). I really don't know how it would act with the IDs. I would be grateful if someone could tell me if this could work like you say. greetz pdluke

              A Offline
              A Offline
              andyharman
              wrote on last edited by
              #6

              Accoring to this [^] mySQL support sequence numbers. Regards Andy

              1 Reply Last reply
              0
              • L Luka Grabarevic

                hi I have following problem. i'd like to use the MAX() function to get the highest ID from a table. i've used is lik this: INSERT INTO Table (ID, Employee, Project, Date, ...) VALUES (MAX(ID) + 1,@Employee,@Project,@Date, ...) but this is not working. i've searched a bit in the internet that it's not possible to put functions into VALUES. so i'd like to know another way to solve this problem. thanks in advance greetz pdluke PS: sorry for my bad english

                D Offline
                D Offline
                DQNOK
                wrote on last edited by
                #7

                I use the following construct in my system: INSERT INTO myTable(ID, Employee, Project, Date, ...) SELECT MAX(ID)+1, @Employee, @Project, @Date, ... FROM myTable; and it works fine. Hope it helps.

                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