MySQL Stored Procedures IF NOT EXISTS()

I have been using MSSQL with ASP.NET alot lately, and I really wanted to get some of the benefits of stored procedures into a PHP application I’m writing. I’ve known MySQL has supported them for a while but I guess I was always a bit too lazy to go through old code and convert it. I used the tool HeidiSQL to make the process a bit more painless.

The first stored procedure I attempted to write accepted 3 parameters and based on one of them checks a lookup table, and creates a new value in it if the same value does not already exist. Normally I would use an IF NOT EXISTS clause, which in Microsoft SQL would look like this

CREATE PROCEDURE someProc
@SomeParam varchar(50),
AS
BEGIN
	DECLARE @SomeId int

	IF  NOT EXISTS(SELECT (1) FROM lookup_table WHERE someField = @SomeParam )
		BEGIN
			INSERT INTO lookup_table (someField) VALUES(@someParam)
			SELECT @SomeId = @@identity
		END
	ELSE
		BEGIN
			SELECT @SomeId = id FROM lookup_table WHERE someField = @SomeParam
		END

	-- Do some other stuff here with the id you just received and other data
END

The above snippet would take a value, check a table for it, get its id, and if the record doesn’t exist in the database create it and return the id. Then you could continue on and insert that id into another table or what have you.

Accomplishing the same thing with a MySQL stored procedure is a bit different. The code below shows how you would accomplish the same task.

CREATE PROCEDURE 'someProc' (IN 'in_SomeParam' INT)
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
BEGIN
	DECLARE SomeId int;
	DECLARE CheckExists int;
	SET CheckExists = 0;  

	SELECT count(*) INTO CheckExists from lookup_table WHERE someField = in_SomeParam;   

	IF (CheckExists > 0) THEN
		SELECT id INTO SomeId FROM lookup_table WHERE someField = in_SomeParam;
	ELSE
		INSERT INTO lookup_table (someField ) VALUES(in_SomeParam);
		SELECT SomeId = LAST_INSERT_ID();
	END IF; 

	# Do some other stuff here with the id you just received and other data
END;

I’m sure I will find some more differences in things I am used to doing with stored procedures using T-SQL, and when I do I’ll post them here.

This entry was posted in MySQL, Programming, Web Development and tagged , , , . Bookmark the permalink.

6 Responses to MySQL Stored Procedures IF NOT EXISTS()

  1. Andrew says:

    How did you connect to the Database in the first place? HeidiSQL isn’t letting me do it, any help?

  2. admin says:

    I connected through Heidi.. it was on my local machine so I just used localhost.

  3. Lenn Dolling says:

    Sweet Stuff. working on some sp/fn’s :)

    should have a android app going soon with a nice database with some intricate stored procedures embedding some nice logic.

    8cups A Day™ Android™ app.

  4. rehan says:

    Thanks for the help, MSSQL to MYSQL not as seamless as I hoped.

  5. Irfan says:

    need to add @ when INTO value a variable otherwise mysql throw error saying variable not defined.

    so it’ll be like

    SELECT count(*) INTO @CheckExists from lookup_table WHERE someField = in_SomeParam;

  6. Daniel says:

    Thanks for the help!!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>