Skip to content

Instantly share code, notes, and snippets.

@Sigmanificient
Created October 11, 2021 01:53
Show Gist options
  • Save Sigmanificient/0d7c0a24333b829e051059ff50abe4a8 to your computer and use it in GitHub Desktop.
Save Sigmanificient/0d7c0a24333b829e051059ff50abe4a8 to your computer and use it in GitHub Desktop.
A mysql routine to insert value within a table and retrieve the pk (with autoincrement)
drop function if exists create_server;
delimiter $$
CREATE FUNCTION create_server(server_name varchar(50), user_id int)
RETURNS int
DETERMINISTIC
BEGIN
INSERT INTO server(name, owner_id) VALUES (server_name, user_id);
return last_insert_id();
END $$
delimiter ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment