Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL INSERT with OUTPUT like MSSQL

I am pretty sure MySQL does not have the INSERT INTO table OUTPUT [column value] that MSSQL does - http://msdn.microsoft.com/en-us/library/ms177564.aspx (or http://blogs.msdn.com/b/sqltips/archive/2005/06/13/output-clause.aspx)

What's an easy way to replicate this? (I am moving a MSSQL app to MySQL. The 'OUTPUT' is a unique identifier and a int currently, so maybe I could just SELECT MAX (int) and add one, and generate a UID prior to insert?)

Thanks

like image 534
AaronM Avatar asked Mar 29 '11 07:03

AaronM


People also ask

Does insert () replace?

You can use the INSERT OR REPLACE statement to write new rows or replace existing rows in the table. The syntax and behavior of the INSERT OR REPLACE statement is similar to the INSERT statement. Unlike the INSERT statement, the INSERT OR REPLACE statement does not generate an error if a row already exists.

How do I get output in MySQL?

The Output is located at the bottom of MySQL Workbench. Its select box includes the Action Output , History Output , and Text Output options.

How can I get identity value after inserting SQL Server?

Once we insert a row in a table, the @@IDENTITY function column gives the IDENTITY value generated by the statement. If we run any query that did not generate IDENTITY values, we get NULL value in the output. The SQL @@IDENTITY runs under the scope of the current session.

What is the difference between insert and insert into SQL?

If you are using Insert or Insert into both will insert the data in Table. However Insert into is basically used to fatch the data from another table using select command and insert into table where you want to insert the data.


1 Answers

If this value is an auto-increment field, you can run SELECT LAST_INSERT_ID(); after running the insert, and you'll get the last value inserted into this field. See: http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_last-insert-id

like image 70
Galz Avatar answered Sep 21 '22 17:09

Galz