I am converting an ADODB application to ADO.Net, and it includes in it a command that inserts a new row and returns the autoincrement value like this:-
INSERT INTO [MyDB].[dbo].[MyTable] (COLUMN1, COLUMN2) OUTPUT inserted.ID_PRIMARY
VALUES ('This', 'That')
One can then grab the OUTPUT value and away you go. But when I try that using ADO.Net, like this:-
command_string = (as above)
Dim insert_command As SqlCommand = New SqlCommand(command_string, database_connection)
Dim output_parameter As SqlParameter = New SqlParameter("@inserted.ID_PRIMARY", SqlDbType.Int)
Dim transaction As SqlTransaction = database_connection.BeginTransaction(System.Data.IsolationLevel.Serializable)
insert_command.Transaction = transaction
output_parameter.Direction = ParameterDirection.Output
insert_command.Parameters.Add(output_parameter)
insert_command.ExecuteNonQuery()
transaction.Commit
an error is returned:-
SqlException (0x80131904) Incorrect Syntax Near '.'
I appreciate that I could use a stored procedure to perform the insert and retrieve a return value, as explained here, but I'd like to know if there is any way this can be done without a stored procedure? Or am I overlooking some trivial error?