-1

I need to insert a row in asp-classic and mysql and get his unique id. Here is my code:

sql = "insert into o_operaciones (idoperacion, fechaalta, nombre)"
sql = sql & ") VALUES ("
sql = sql & "DEFAULT,now(),'" & nombre & "');"
sql = sql & "SELECT max(idoperacion) as id from o_operaciones; "
Set rsInsert = Conex.Execute(sql)
idoperacion = cdbl(rsInsert("id"))

With this code i don´t have errors but the recordset it´s empty or closed after execute.

I know there is another ways to do the same, but i think this is the faster.

I tried SET NOCOUNT but don't work in MySql.

user692942
  • 16,398
  • 7
  • 76
  • 175
  • possible duplicate of [How to get the ID of INSERTed row in mysql?](http://stackoverflow.com/questions/7501464/how-to-get-the-id-of-inserted-row-in-mysql) – user692942 May 20 '15 at 22:11

1 Answers1

-1

I'm not sure if you can do that the way you are trying to.

You can use something like the following if you're able / don't mind using stored procedures:

CREATE PROCEDURE MyProcedure
    @col1            VARCHAR(20),
    @new_id    INT    OUTPUT
AS
BEGIN
    SET NOCOUNT ON

    INSERT INTO MyTable (col1)
    VALUES (@col1)

    SELECT @new_id = SCOPE_IDENTITY()

    SELECT @new_id AS id

    RETURN
END
Martin Hansen Lennox
  • 2,837
  • 2
  • 23
  • 64