Results 1 to 3 of 3

Thread: Problems returning the ID of the last record added using a Stored Procedure

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Problems returning the ID of the last record added using a Stored Procedure

    I'm making an app to add test questions to a SQL DB. I'm planning on using a Stored procedure to insert the question and then would like to return the Identity so I can use that to do the inserts for the answers into the Answers table. So far I have this as the stored procedure.
    Code:
    CREATE PROCEDURE sp_AddQuestion
    (
    	@question AS VARCHAR (8000),
    	@Identity AS INT
    )
    AS
    	INSERT INTO Questions(questions) values(@question)
    	RETURN SELECT @@IDENTITY as 'Identity'
    GO
    and in the form I'm using this code
    VB Code:
    1. Private Sub cmdInsert_Click()
    2. Dim objCmd As ADODB.Command
    3. Dim rs As ADODB.Recordset
    4.  
    5.     Set objCmd = New ADODB.Command
    6.     With objCmd
    7.         Set .ActiveConnection = Cn
    8.         .CommandType = adCmdStoredProc
    9.         .CommandText = "sp_AddQuestion"
    10.        
    11.         .Parameters.Append .CreateParameter("@question", adVarChar, adParamInput, 300, "Test question Input")
    12.         .Parameters.Append .CreateParameter("@Identity", adInteger, adParamOutput, 4)
    13.    
    14.         Set rs = .Execute
    15.         MsgBox rs.Fields(0)
    16.     End With
    17. End Sub
    When I try to run this I'm getting the following error on the execute line.
    Formal parameter ‘@Identity’ was defined as OUTPUT but the actual parameter not declared OUTPUT.
    What does this error mean and how can I return the id of the last record entered?

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263
    Several ways to do this - but since you want to use an OUTPUT parameter try this...

    Code:
    CREATE PROCEDURE sp_AddQuestion
    (
    	@question AS VARCHAR (8000),
    	@Ident_Return AS INT OUTPUT
    )
    AS
                    SET NOCOUNT ON
    	INSERT INTO Questions(questions) values(@question)
    	SET @IDENT_RETURN=@@IDENTITY
    GO
    Then maybe something more like this in VB

    VB Code:
    1. Private Sub cmdInsert_Click()
    2. Dim objCmd As ADODB.Command
    3. ' Dim rs As ADODB.Recordset
    4.  
    5.     Set objCmd = New ADODB.Command
    6.     With objCmd
    7.         Set .ActiveConnection = Cn
    8.         .CommandType = adCmdStoredProc
    9.         .CommandText = "sp_AddQuestion"
    10.        
    11.         .Parameters.Append .CreateParameter("@question", adVarChar, adParamInput, 300, "Test question Input")
    12.         .Parameters.Append .CreateParameter("@Ident_Return", adInteger, adParamOutput, 4)
    13.    
    14.         objCmd.Execute
    15.         MsgBox objCmd.Parameters(1).Value ' rs.Fields(0)
    16.     End With
    17. End Sub

    I've done all this free-hand - have no VB or SQL on this machine.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141
    Thank you szlamany! That worked.

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