Procedure to create unique values for different ID's

  • Hi,

    I have One table name Company as Follows

    Create table Company

    (

    ID char(2),

    CompanyID char(1),

    CategoryID varchar(10),

    Primary key(CompanyID, CategoryID)

    )

    I want to make one procedure 'USP_NextID' such as when i pass 'ID' and 'CompanyID'

    as parameter then the value should get inserted into to Company Table.

    Also the categoryid should be auto incremented.

    Consider I have two differennt companies A and B

    When i m logged in Company A and i m executing USP_NextID

    then then categorid should be incremented as follows

    CA001, CA002, CA003

    When i m getting logged in Company B and i m executing USP_NextID

    then then categorid should be incremented as follows

    CA001, CA002, CA003

    when I execute procedure like this USP_NextID 'CA', 'A'

    The values that will be inserted into company table will be

    ID CompanyID CategoryID

    CAACA001

    Example When i execute the procedure

    USP_NextID 'CA', 'A'

    USP_NextID 'CA', 'A'

    USP_NextID 'CA', 'A'

    USP_NextID 'CA', 'B'

    USP_NextID 'CA', 'B'

    USP_NextID 'CA', 'B'

    Then the output of the Table should be

    ID CompanyID CategoryID

    CAACA001

    CAACA002

    BA A CA003

    CA B CA001

    CA B CA002

    CA B CA003

    Plz give me some solution.

    Regards,

    Nikhil P Desai

  • Your expected output:

    ID CompanyID CategoryID

    CA A CA001

    CA A CA002

    BA A CA003 -- is this a typing error and it should be CA A CA003?

    CA B CA001

    CA B CA002

    CA B CA003

    What should happen if I ran your code this way:

    USP_NextID 'CA', 'B'

    USP_NextID 'CA', 'B'

    USP_NextID 'CA', 'B'

    USP_NextID 'CA', 'B'-- 1 additional execution

    USP_NextID 'CA', 'B'-- another additional execution

    If everything seems to be going well, you have obviously overlooked something.

    Ron

    Please help us, help you -before posting a question please read[/url]
    Before posting a performance problem please read[/url]

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply