Results 1 to 6 of 6

Thread: Adding auto number

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2003
    Posts
    68

    Adding auto number

    I got a add form which is able to increment a no, But how do i set in e sql statement? I wan to insert data into e database without asking e user to type wat record is that, i wan e form to auto increment e no. How do i done it?

  2. #2
    Hyperactive Member MarkusJ_NZ's Avatar
    Join Date
    Jun 2001
    Posts
    375
    This is for SQL server and VB.Net:

    Insert the record into the database first and let the database assign the autonumber (In SQL server its a special variable called @@IDENTITY). Retrieve the @@IDENTITY by passing back the value from a stored procedure to the calling function, SP shown below...

    VB Code:
    1. CREATE PROCEDURE InsertPage
    2. @BookID int,
    3. @PersonID int,
    4. @PageOrder float,
    5. @PageName varchar(50),
    6. @TemplateInfo nvarchar(5) = NULL
    7.  AS
    8. BEGIN
    9. Declare @nID int
    10.  
    11. -- Try to do the update first
    12. INSERT INTO BookPage (BookID, PersonID, PageOrder, PageName,TemplateInfo)
    13. VALUES
    14. (@BookID, @PersonID,@PageOrder, @PageName,@TemplateInfo)
    15. -- Get the Identity
    16. Select @nID = @@IDENTITY
    17. END
    18. Return @nID
    19. GO

    This stored procedure passes back the inserted autonumber.

    Call the Stored procedure via ADO.Net and then assign this number to your form

    hth
    Cheers
    MarkusJ

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2003
    Posts
    68
    wat is @@IDENTITY? Can u briefly describe it?
    I dun understand this line.
    Select @nID = @@IDENTITY
    END
    Return @nID
    I need to change anything from e sql server database?

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Sep 2003
    Posts
    68
    ya, i got it liao, thanks.

    But if i auto increment e no from table01, i also need the incremented no to save to another table02 too.
    How do i do it?

  5. #5
    Hyperactive Member MarkusJ_NZ's Avatar
    Join Date
    Jun 2001
    Posts
    375
    Hi, there are a couple of ways using SQL. One is using a Trigger for Insert or you could use the Stored procedure to do the insert into the other table

    VB Code:
    1. CREATE PROCEDURE InsertPage
    2. @BookID int,
    3. @PersonID int,
    4. @PageOrder float,
    5. @PageName varchar(50),
    6. @TemplateInfo nvarchar(5) = NULL
    7.  AS
    8. BEGIN
    9. Declare @nID int
    10.  
    11. -- Try to do the update first
    12. INSERT INTO BookPage (BookID, PersonID, PageOrder, PageName,TemplateInfo)
    13. VALUES
    14. (@BookID, @PersonID,@PageOrder, @PageName,@TemplateInfo)
    15. -- Get the Identity
    16. Select @nID = @@IDENTITY
    17.  
    18. /* Now do the insert into the other table
    19.  
    20. INSERT INTO YourTable(ID, attribute1, attribute2) VALUES (@nID,'Bill','gates')
    21.  
    22. */
    23. END
    24. Return @nID
    25. GO

    If you are going to start doing multiple Inserts, updates etc don't forget to use transactions

    hth
    Cheers
    MarkusJ

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Sep 2003
    Posts
    68
    Hi, but from table01, i need e increment no to refer to another table02. As my form make use of 2 insert statements.
    How i do refer to it?

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