Get table ID after insert with ColdFusion and MySQL

前端 未结 5 462
遥遥无期
遥遥无期 2021-01-04 02:33

My normal process for inserting into one table and then getting the ID back so that I can insert into another table is like this in MSSQL:

DECLARE @transacti         


        
5条回答
  •  清歌不尽
    2021-01-04 03:11

    Here is a quick solution for MSSQL. It uses the SCOPE_IDENTITY() function that returns the ID of the last row inserted in the previous insert statement.

    http://msdn.microsoft.com/en-us/library/ms190315.aspx

    
        DECLARE @iNewGeneratedID INT
    
        INSERT INTO transactions
            (
                transactionDate,
                transactionAmount
            )
        VALUES
           (
                ,
                
           )
    
        SET @iNewGeneratedID = SCOPE_IDENTITY()
    
        INSERT INTO transactionItems
            (
                transactionID,
                itemID,
                itemAmount
            )
        VALUES
            (
                @iNewGeneratedID,
               ,
               
            )
    
        SELECT @iNewGeneratedID AS iNewGeneratedID
    
    

提交回复
热议问题