Results 1 to 6 of 6

Thread: Multi User Problems

Threaded View

  1. #6
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Multi User Problems

    We have tables in our financial packages that have compound primary keys.

    PK is FiscalYr (int) and TransactionNumber (int).

    We cannot use IDENTITY (MS SQL version of AUTONUMBER) on the TransactionNumber column.

    So instead we have a CONFIG table where we store the "last Transaction" number in use.

    In order to grab "three transaction number from this table" we do the following - note we do this in STORED PROCEDURES but you could do it from VB just the same against any type of database.

    This is pseudo-code.

    1) Get the "last transaction number in use" (Select TransNum From ConfigTable)

    2) Remember this original "last trans number" (OrigLastTrans = TransNumb)

    3) Add "three" to this value (NewTransNumb = TransNumb + 3)

    4) Now attempt the UPDATE - this attempt to UPDATE uses a trick to handle multiuser environments

    ....Update ConfigTable Set TransNum=NewTransNumb Where TransNumb=OrigTransNumb

    5) Now the important part - check the "ROWCOUNT" to see if the update occured - we use @@ROWCOUNT in the STORED PROCEDURE - but most providers/ADO have a method to tell you if a row was updated.

    6) If the row was not updated - the row count is zero - then you know that someone else updated before you (the multiuser part of this). If this is true then simply got back to step 1 and do this all over again.

    Here is a snippet of the code from MS SQL SERVER (from a STORED PROCEDURE)

    Code:
    RedoB:
    	Set @DataValue=(select DataValue from Control_T
    				where FiscalYr=@FiscalYr and Item='LAST BATCH')
    	Set @NewDataValue=Right('000000'+Cast(Cast(@DataValue as int)+1 as VarChar(6)),6)
    	
    	Update Control_T
    	Set DataValue=@NewDataValue,Tdate=@Tdate
    	Where FiscalYr=@FiscalYr and Item='LAST BATCH' and DataValue=@DataValue
    	
    	If @@RowCount<>1 Goto RedoB
    Last edited by szlamany; Feb 6th, 2007 at 10:28 AM.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

Posting Permissions

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



Click Here to Expand Forum to Full Width