Results 1 to 10 of 10
  1. #1
    accessmatt is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Aug 2011
    Posts
    199

    How to add auto-number ID using SQL

    Hello,

    Using SQL, can someone show me how do I add a field to this query which is an ID that auto-numbers each row?

    Code:
    SELECT Table1.*FROM Table2;
    Thanks,

  2. #2
    NTC is offline VIP
    Windows 7 64bit Access 2013
    Join Date
    Nov 2009
    Posts
    2,392
    numbering the rows/records of a query result is complicated code - essentially trying to do something which inherently is not available. In the Report object one has the numbering method available. But if you bing/google the topic of numbering query records you will find a variety of examples.

  3. #3
    Micron is offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,818
    If this is a one-off thing, convert the query to a make table query then add the primary key (ID) afterwards. If not, then I think there is an issue with db normalization and design. Adding an auto id isn't something you normally do with queries - you design the table that way. Perhaps you need to elaborate more on what it is you're doing.

    Is that a typo (how do you select all of Table1 from Table2)?
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #4
    accessmatt is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Aug 2011
    Posts
    199
    thank you both for weighing in.

  5. #5
    accessmatt is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Aug 2011
    Posts
    199
    How can I use sql or Access to create an autonumber field when I run an INTO (make table) query?
    Last edited by accessmatt; 01-27-2016 at 07:59 AM. Reason: deleted

  6. #6
    orange's Avatar
    orange is offline Moderator
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,742
    When you do a SELECT INTO the design of the receiving table is already known.
    If you want to create a Table with an autonumber field, you use a Data definition query such as

    Code:
    CREATE TABLE testAncestor (
    	ancID counter
    	,FirstName TEXT (20) NOT NULL
    	,LastName TEXT (20) NOT NULL
    	,CONSTRAINT PK PRIMARY KEY (ancID)
    	);
    This creates a new table called testAncestor with fields

    ancId which will be autonumber (COUNTER is the datatype for autonumber)
    FirstName text field 20 char
    LastName text field 20 char

    NOT NULL means a value must be supplied for each record.
    The CONSTRAINT clause makes ancID the Primary Key for this table.

  7. #7
    accessmatt is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Aug 2011
    Posts
    199
    Thanks @orange but is there a way I can use Create Table in conjunction with fields and data in an existing query. The example you gave creates the definition but I want to pull data into there, with a new field called ID that auto-increments.

  8. #8
    orange's Avatar
    orange is offline Moderator
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,742
    Yes, but---

    1. The table must exist before you can add data to it.
    2. You can use and Append query (Insert with SELECT..)

    Example:

    You would create the table as mentioned previously, then this sort of thing
    Code:
    INSERT INTO Customers (CustomerName, Country)
        SELECT SupplierName, Country FROM Suppliers;

  9. #9
    accessmatt is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Aug 2011
    Posts
    199
    @orange, thanks that worked perfect.

  10. #10
    orange's Avatar
    orange is offline Moderator
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,742
    Happy to help. For SQL syntax and examples see w3schools

Please reply to this thread with any new information or opinions.

Similar Threads

  1. auto number
    By Jen0dorf in forum Access
    Replies: 2
    Last Post: 09-24-2015, 06:27 AM
  2. Replies: 5
    Last Post: 11-30-2014, 12:46 PM
  3. Auto number automatically next number
    By wnicole in forum Access
    Replies: 3
    Last Post: 10-17-2013, 08:45 AM
  4. Auto Number
    By sah in forum Forms
    Replies: 1
    Last Post: 04-30-2012, 07:20 AM
  5. Need help with an auto number
    By Wrangler in forum Database Design
    Replies: 1
    Last Post: 02-10-2006, 03:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums