Of course, I could go into mysql console and type the Function. But what if I want to store it for future use? What do I do?
views:
27answers:
2
+1
A:
Most projects have an SQL file to initialize the database from scratch. This way, one can set up the application database by simply running this SQL file. Your CREATE PROCEDURE/FUNCTION query would also go in this file.
Sjoerd
2010-07-01 06:41:00
my.cnf? how does the syntax go?
TIMEX
2010-07-01 06:41:35
Not `my.cnf`. Usually you have something like `install.sql`, which you use to create database for new instalation -- `create table` statements and so on. This is also a good place for stored procedures.
che
2010-07-01 06:50:26
A:
There's a good tutorial here:
http://www.databasejournal.com/features/mysql/article.php/3525581/MySQL-Stored-Procedures-Part-1.htm
You need to use stored procedures. Once written, these are stored in the database along with your tables. They can be invoked using the CALL <procedure>
statement.
Here's an example procedure that populates table1
with random values:
DELIMITER //
DROP PROCEDURE IF EXISTS autofill//
CREATE PROCEDURE autofill()
BEGIN
DECLARE i INT DEFAULT 0;
WHILE i < 20000 DO
INSERT INTO table1 (size) VALUES (FLOOR((RAND() * 1000)));
SET i = i + 1;
END WHILE;
END;
//
DELIMITER ;
Once the procedure has been written, it is called like this:
CALL autofill();
Mike
2010-07-01 06:43:01