Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Can I SELECT information the same time i INSERT

Can I SELECT information the same time i INSERT

Scheduled Pinned Locked Moved Database
htmldatabasehelpquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Robby
    wrote on last edited by
    #1

    I want to know if there is a way to get the PK from a row the minute I insert information. Here's the scenario, what's the most efficient way of doing this? 1. I have 50 tables that represent 50 HTML forms. 2. I am inserting results from OnlineAccess.asp into dbo.OnlineAccess. 3. I am keeping all dateTime stamps along with the status of the request (OPEN, BEING PROCESSED, COMPLETE) in another table called dateTime. 4. I want to get the PK of the information entered into dbo.OnlineAccess so that I can insert this into dbo.DateTime for relation to dbo.OnlineAccess's information. Is there any "bi-di" capabilities that can be used to return the PK without having to run another line of code to SELECT the PK WHERE . For instance, I know I can do it this way, but it seems like too much leg work: sqlInsert = "INSERT into OnlineAccess(userID,lastName,firstName,...) values('"&userID&"','"&lastName&"','"&firstName&"',...)" sqlSelect = "SELECT requestID FROM OnlineAccess WHERE userID='"&userID&"' AND lastName='"&lastName&"' AND ..." RS = MyConn.Execute(sqlSelect) requestID = RS("requestID") sqlInsert = "INSERT into DateTime (requestID,status) values('"&requestID&"','OPEN')" (I have a trigger that takes care of the dateTime stamps based on the status) Can anyone help me on this one? Thanks in advance! Robby

    D J 2 Replies Last reply
    0
    • R Robby

      I want to know if there is a way to get the PK from a row the minute I insert information. Here's the scenario, what's the most efficient way of doing this? 1. I have 50 tables that represent 50 HTML forms. 2. I am inserting results from OnlineAccess.asp into dbo.OnlineAccess. 3. I am keeping all dateTime stamps along with the status of the request (OPEN, BEING PROCESSED, COMPLETE) in another table called dateTime. 4. I want to get the PK of the information entered into dbo.OnlineAccess so that I can insert this into dbo.DateTime for relation to dbo.OnlineAccess's information. Is there any "bi-di" capabilities that can be used to return the PK without having to run another line of code to SELECT the PK WHERE . For instance, I know I can do it this way, but it seems like too much leg work: sqlInsert = "INSERT into OnlineAccess(userID,lastName,firstName,...) values('"&userID&"','"&lastName&"','"&firstName&"',...)" sqlSelect = "SELECT requestID FROM OnlineAccess WHERE userID='"&userID&"' AND lastName='"&lastName&"' AND ..." RS = MyConn.Execute(sqlSelect) requestID = RS("requestID") sqlInsert = "INSERT into DateTime (requestID,status) values('"&requestID&"','OPEN')" (I have a trigger that takes care of the dateTime stamps based on the status) Can anyone help me on this one? Thanks in advance! Robby

      D Offline
      D Offline
      David Salter
      wrote on last edited by
      #2

      Probably the best way would be to write a stored procedure that can then insert your data and then return the PK to the caller. I know this works with SQL Server, but I've had trouble returning values from SPs in Oracle. Dave.

      1 Reply Last reply
      0
      • R Robby

        I want to know if there is a way to get the PK from a row the minute I insert information. Here's the scenario, what's the most efficient way of doing this? 1. I have 50 tables that represent 50 HTML forms. 2. I am inserting results from OnlineAccess.asp into dbo.OnlineAccess. 3. I am keeping all dateTime stamps along with the status of the request (OPEN, BEING PROCESSED, COMPLETE) in another table called dateTime. 4. I want to get the PK of the information entered into dbo.OnlineAccess so that I can insert this into dbo.DateTime for relation to dbo.OnlineAccess's information. Is there any "bi-di" capabilities that can be used to return the PK without having to run another line of code to SELECT the PK WHERE . For instance, I know I can do it this way, but it seems like too much leg work: sqlInsert = "INSERT into OnlineAccess(userID,lastName,firstName,...) values('"&userID&"','"&lastName&"','"&firstName&"',...)" sqlSelect = "SELECT requestID FROM OnlineAccess WHERE userID='"&userID&"' AND lastName='"&lastName&"' AND ..." RS = MyConn.Execute(sqlSelect) requestID = RS("requestID") sqlInsert = "INSERT into DateTime (requestID,status) values('"&requestID&"','OPEN')" (I have a trigger that takes care of the dateTime stamps based on the status) Can anyone help me on this one? Thanks in advance! Robby

        J Offline
        J Offline
        Jon Hulatt
        wrote on last edited by
        #3

        Indeed, using a stored procedure would by far and away be the best solution. after an insert, sql server special variable @@IDENTITY contains the new pk of what you just inserted. so this will probably work:-

        sqlInsert = "INSERT into OnlineAccess(userID,lastName,firstName,...) values('"&userID&"','"&lastName&"','"&firstName&"',...) ; SELECT @@IDENTITY AS [newkeyname]"

        RS = MyConn.Execute(sqlInsert)
        requestID = RS("newkeyname")

        But really thats a bag of sh!t of an implementation. Stored procedures are the way to go. Why? 1. stored procedures run much quicker, because they are precompiled. 2. They are like functions in proper languages; you can change the internal code without changing the interface. You could most probably do the whole job in one SP, like this:

        SET QUOTED_IDENTIFIER OFF
        GO
        SET ANSI_NULLS OFF
        GO

        CREATE PROCEDURE [p_YourProc]
        @UserId int,
        @Firstname varchar(100),
        @Lastname varchar(100),
        @NewPKout integer output

        AS

        -- define a sql variable
        declare @NewPK int;

        -- do the main insert first.
        INSERT into OnlineAccess(userID,lastName,firstName) values
        ( @UserId, @LastName, @Firstname);

        -- retrieve the new key
        select @NewPK=@@IDENTITY;

        -- do the other inserts
        INSERT into DateTime (requestID,status)
        values(@NewPK,'OPEN')

        -- might as well send the pk back to asp
        set @NewPKout = @NewPK

        GO
        SET QUOTED_IDENTIFIER OFF
        GO
        SET ANSI_NULLS ON
        GO

        There. Bob's your uncle. Signature space for rent. Apply by email to....

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups