A MySQL INSERT
statement doesn't return a resultset; so it can't be used a row source for a query. If the INSERT
statement fails, it will throw an error.
To return a resultset, a separate SELECT
statement would need to be executed.
These two statements could be executed in the context of a stored procedure, if the goal is to execute a single database call.
EDIT:
Q: I want to know if it's possible to do this with just one query?
A: No, it's not possible with just one statement. You'd need two statements: an INSERT
statement (to perform the insert operation) and an SELECT
statement (to return a result).
To do this in a single database call, you'd need a MySQL stored program that performs the two statements: first, the INSERT, and catch errors thrown (with a CONTINUE HANDLER
), and then a SELECT
to return a resultset.
It's not clear why you need a resultset that returns a column containing 'PASS'
or NULL
.
The normative pattern would be to run the INSERT
statement, check to see whether it threw an error, and/or check "affected rows" using function available in the client library, and/or run a separate SELECT
statement to retrieve the value from the MySQL ROW_COUNT()
function, e.g.
INSERT INTO mytable (mycol) VALUES ('myval') ;
SELECT IF(ROW_COUNT(),'PASS',NULL) ;
(To do this within a stored procedure, you'd need to define a CONTINUE HANDLER
in the procedure to "catch" errors from the INSERT
so that the procedure will continue running (rather than throwing an error) so the SELECT
statement will be run.)