Results 1 to 3 of 3

Thread: global variable

  1. #1
    Join Date
    Mar 2007
    Posts
    1

    Question Unanswered: global variable

    Hi all,

    Can someone tell me how to set our own global variable that share
    same value for each client connected to mysql server?

    i tried this :

    Select max(invoiceNumber) + 1 from invoiceMaster into @no;

    but @no is only available at current session.
    How to set this @no to be global so when each client "Select @no;"
    they will share the same value?

    I think this solution to solve invoiceNumber problem.
    If there is 2 client connected to mysql server and suppose
    currently on invoiceMaster, there are 1 row with invoiceNumber 1.
    When they open invoice form using "Select max(invoiceNumber) + 1 from invoiceMaster", each client will get same invoiceNumber which is 2.

    And if they try to save the invoice, there will be problem because
    invoiceNumber is a primary key.

    So, i think there should be some global variable on the server.

    So, when client A open the invoice form, this global variable(@no) will be set to max(invoiceNumber) + 1 which is 2;

    If client B try to get the currently available invoiceNumber, @no
    will be set to @no+1 which is now 3.

    If one of them cancel the transaction, @no will be set to @no-1.

    Do you guys know other solution?

    Thanks folks.

    Oh ya, i use mysql 5.0.27

    PSorry if i put this question on wrong section.

  2. #2
    Join Date
    Mar 2004
    Posts
    480
    last_insert_id is most likely the direction you should be looking.

  3. #3
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    to expand on what guelphdad said, you should not be using the MAX+1 technique if the invoiceNumber is an auto_increment
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •