Results 1 to 7 of 7

Thread: Question about SQL Transaction/Stored Procedures and Identity Fields

Threaded View

  1. #1

    Thread Starter
    Addicted Member riechan's Avatar
    Join Date
    Feb 2008
    Location
    Japan
    Posts
    254

    Question about SQL Transaction/Stored Procedures and Identity Fields

    Getting a "Procedure or function spNewPatient expects parameter @PXID (Identity Field) which was not supplied." when I try to insert data using dynamic SP and transactions. However, @PXID is an Identity field in tblPatient, so I didn't include it on the input parameters. Do I also have to begin a transaction here (since the stored procedure is technically a transaction)? Is that the reason why the @PXID is not generated by the stored procedure?

    Code:
    Public SQLParams As New List(Of SqlParameter)
    Public Function ExecuteSP(SPName As String) As Integer
    	Dim _ReturnVal As Integer
    	Try
    		SQLConn.Open()
    		SQLCmd = New SqlCommand(SPName, SQLConn)
    		SQLCmd.CommandType = CommandType.StoredProcedure
    
    		For Each p As SqlParameter In SQLParams
    			SQLCmd.Parameters.Add(p)
    			SQLCmd.Parameters(p.ParameterName).Value = p.Value
    		Next
    
    		_ReturnVal = SQLCmd.ExecuteScalar()
    		SQLConn.Close()
    		Return _ReturnVal
    	Catch ex As Exception
    		MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Encountered Critical Error")
    		If SQLConn.State = ConnectionState.Open Then SQLConn.Close()
    		Return -1
    	End Try
    	
    	FlushParams()
    End Function
    
    Public Sub AddParams(Name As String, Value As Object, Optional DataType As DbType = DbType.String)
    	Dim newParams As New SqlParameter With {.ParameterName = Name, .Value = Value, .DbType = DataType}
    	SQLParams.Add(newParams)
    End Sub
    Code:
    USE [dbMacondo]
    ALTER PROC [dbo].[spNewPatient]
    	@PXID			int OUTPUT,
    	<Input Variables>
    AS
    BEGIN
    	BEGIN TRANSACTION
    		BEGIN TRY
    			INSERT INTO tblPatient (...)
    			VALUES (...);
    
    			SELECT @PXID = SCOPE_IDENTITY();
    			COMMIT TRANSACTION
    		END TRY
    
    	BEGIN CATCH
    		IF XACT_STATE() <> 0
    			ROLLBACK TRANSACTION
    	END CATCH
    END
    Edit: I changed the order of the fields in the stored procedure. It looks like none of the parameters I tried to pass from VB to SQL were transferred... I checked the list (SQLParams) and everything was in order.

    Edit 2: Checked it again, and it's still looking for the @PXID Identity Field. Removed the @PXID in the stored procedure and everything went through nicely. However, how would I be able to implement this with using an output variable for scope_identity() in the stored procedure?
    Last edited by riechan; Mar 25th, 2016 at 03:10 AM.
    ====================
    ほんとにどもありがとう!

    Rie Ishida

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