Results 1 to 4 of 4

Thread: SQL insert command, how to return the created id value?

  1. #1
    IBM
    IBM is offline
    there but for the grace of God, go I IBM's Avatar
    Join Date
    Dec 2003
    Location
    West London
    Posts
    4,187
    Thanks
    149
    Thanked
    244 times in 145 posts
    • IBM's system
      • Motherboard:
      • Asus P5K Deluxe
      • CPU:
      • Intel E6600 Core2Duo 2.40GHz
      • Memory:
      • 2x2GB kit (1GBx2), Ballistix 240-pin DIMM, DDR2 PC2-6400
      • Storage:
      • 150G WD SATA 10k RAPTOR, 500GB WD SATA Enterprise
      • Graphics card(s):
      • Leadtek NVIDIA GeForce PX8800GTS 640MB
      • PSU:
      • CORSAIR HX 620W MODULAR PSU
      • Case:
      • Antec P182 Black Case
      • Monitor(s):
      • Dell 2407WPF A04
      • Internet:
      • domestic zoom

    SQL insert command, how to return the created id value?

    I'm inserting into an access db, and I just want to return the id value of the row that's created. I used to be able to do it with Stored Procedures in MSSQL Server, but I'm not having much luck with straight SQL.

    So I've got INSERT INTO tblName (fieldname1, fieldname2, fieldname3) VALUES ('a','b','c'), and I want it to return the id, so the next page in the site knows what entry I'm talking about. I don't want to get just the most recently created row, as I'm expecting fairly high traffic, and there's always the theoretical chance that someone will create a new item in the short period between insertion and retrieval.

    I could always create a unique identifier from the server, date/time stamp, and then use that to retrieve the information, but that just seems daft, and there has to be an easier way to do it.

    Anyone got any ideas?

    Muchas Gracias, Merci, Terima Kasih, Ta, Danke, Much Obliged, and Thanks.
    sig removed by Zak33

  2. #2
    TiG
    TiG is offline
    Walk a mile in other peoples shoes...
    Join Date
    Jul 2003
    Location
    Questioning it all
    Posts
    6,213
    Thanks
    45
    Thanked
    48 times in 43 posts
    If you are expecting fairly high traffic what the hell are you doing using Access?. Surely MSDE would be so much more appropriate. Then you could use Stored Procedures and do this properly.

    I'd certainly not advise doing a high load site against access.

    TiG
    -- Hexus Meets Rock! --

  3. #3
    Pixel Abuser Spunkey's Avatar
    Join Date
    Nov 2003
    Location
    Milton Keynes
    Posts
    1,523
    Thanks
    0
    Thanked
    0 times in 0 posts
    as you mentioned 'next page' i assume your going to be calling this Stored Procedure from a PHP/ASP page?

    If you're using ASP, either of these should do it...
    Code:
    set rsTest = server.createobject("ADODB.RecordSet")
    rsTest.Open "spTestStoredProc", Conn, 3, 1    ' the 3,1 cursor and locktype enable the recordset to return the primary key.
      iNewID = rsTest("ID")
    rsTest.close
    set rsTest = nothing
    some server installations have a beef with that, so if that dont not work, try this...
    Code:
    set Conn = server.createobject("ADODB.Connection")
    SQL = "INSERT INTO Test (Column1,Column2) VALUES ('Norks','Baps');
    cn.execute SQL
    
    set rsTestID = server.createobject("ADODB.Recordset")
    rsTestID.open "SELECT @@IDENTITY AS 'NewID';", Conn, 2, 3
      iNewID = rsTestID("NewID")
    rsTestID.close
    set rsTestID = nothing
    HTH!

    *edit*
    and what TiG said, Access Backend + High Volume Site = Tears and P45

  4. #4
    IBM
    IBM is offline
    there but for the grace of God, go I IBM's Avatar
    Join Date
    Dec 2003
    Location
    West London
    Posts
    4,187
    Thanks
    149
    Thanked
    244 times in 145 posts
    • IBM's system
      • Motherboard:
      • Asus P5K Deluxe
      • CPU:
      • Intel E6600 Core2Duo 2.40GHz
      • Memory:
      • 2x2GB kit (1GBx2), Ballistix 240-pin DIMM, DDR2 PC2-6400
      • Storage:
      • 150G WD SATA 10k RAPTOR, 500GB WD SATA Enterprise
      • Graphics card(s):
      • Leadtek NVIDIA GeForce PX8800GTS 640MB
      • PSU:
      • CORSAIR HX 620W MODULAR PSU
      • Case:
      • Antec P182 Black Case
      • Monitor(s):
      • Dell 2407WPF A04
      • Internet:
      • domestic zoom
    It's a fair cop guv, and you're quite right, I should indeed be avoiding Access like the plague infested totally inadequate thing it is.

    However, I haven't worked with SQL server in three years, and then it was within a company with legit copies, and a support service to help me through the numerous problems I've encountered, plus a load of in house talent to assist my learning. If it turns out that the site generates the kind of traffic I'm talking about, then I'll be able to pay a SQL bod to port all my stuff across to SQL server. Until then I'm stuck with Access, although since you're quite right, I'll start digging through my old SQL Server resources and seeing where I can get to....although I still can't afford a legit copy of Server for development, and I'm not one for supporting software piracy....

    So with that in mind, does anyone have any helpful suggestions to the problem at hand, and at least that way, even if I do decide to scrap the access stuff done so far, I'll know my code is solid?
    sig removed by Zak33

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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