Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jan 2003
    Posts
    111
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Setting a table field based on another table's last entry

    Is there anyway to set up a SQL database table column to number itself based on the last entry in a different table?

    Example:

    "Table a" has a field tablebID that needs to be the next number available from "Table b" ID field.

    So if "Table b" looked like this:
    ID, ENTRY
    1, first entry
    2, second entry
    3, third entry

    The next entry in "Table a" would automatically assign 4 to the tablebID column.

    Anyone follow that? Is that possible to do in the SQL Server database structure?
    Last edited by zenweezil; 09-13-2005 at 12:58 AM.

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts
    But what should happen if the last record in table b is deleted, the next autonumber (if you have the id in table b as an autonumber with increment 1) would still be 4, because id's are not automatically re-used.
    Is the desired entry in table a still 4 or is it 3?

    Tell us what you are trying to do, there might be a better solution to find
    I am the luckiest man in the world

  • #3
    Regular Coder
    Join Date
    Jan 2003
    Posts
    111
    Thanks
    2
    Thanked 0 Times in 0 Posts
    It wouldn't matter if it was 3 or 4 as long as it remained unique and corresponded between tables.

    The reason I want the database structure to handle this rather than having an asp page call out the last ID of table B and insert into table a is because all the rest of the entry is being submitted via a csv import and I can't add those to the csv and am not sure how to insert other fields simulatenously wiht the csv import.

    One thought is to find a more manual way to import the csv as of now I was using an Interakt extension - perhaps if I could find some code on how to hardcode a csv INSERT I could include my ID fields.

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts
    well, you could use a trigger which fires after the insert. Let the trigger find out the id of the inserted record. Then store that id in table a
    Code:
    CREATE TRIGGER StoreNewId ON [dbo].[b] 
    FOR INSERT 
    AS
    UPDATE a SET TableBid = ((SELECT id FROM inserted) + 1)
    it needs one record in table a, but this should work
    Last edited by Roelf; 09-14-2005 at 07:37 AM.
    I am the luckiest man in the world

  • #5
    Regular Coder
    Join Date
    Jan 2003
    Posts
    111
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks, I'll give that a try.


  •  

    Posting Permissions

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