| | |
UDF in MySQL
Please support our MySQL advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
Something like this? http://en.oreilly.com/mysql2008/publ...ule/detail/128
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
•
•
Join Date: Apr 2008
Posts: 296
Reputation:
Solved Threads: 42
hi,
MySQL does not support user defined functions (UDF) in the sense of standard SQL, which, for example, allow such a declaration in its own PSM language:
In MySQL kind of UDF can be defined as a plain C program. Such C-coded program must be linked dynamically, there is also special header handling. This approach is rather complicated and error prone. Once Borland's interbase had had same approach.
brs,
tesu
MySQL does not support user defined functions (UDF) in the sense of standard SQL, which, for example, allow such a declaration in its own PSM language:
sql Syntax (Toggle Plain Text)
CREATE FUNCTION avgg(*) RETURNS FLOAT ap FLOAT SELECT avg(price) FROM goods INTO ap return ap -- usage SELECT id FROM goods WHERE price < avgg(*)
brs,
tesu
•
•
Join Date: Apr 2008
Posts: 296
Reputation:
Solved Threads: 42
well tanha,
though i ve written UDFs for Sybase Databases a good many times, it s rather time-consuming doing that within MySQL. You may have a look at
http://dev.mysql.com/doc/refman/5.0/en/adding-udf.html
to get an impression of the hard work to be done for it (as opposed to the convenient way when programming UDF by using SQL1999 standards).
krs,
tesu
though i ve written UDFs for Sybase Databases a good many times, it s rather time-consuming doing that within MySQL. You may have a look at
http://dev.mysql.com/doc/refman/5.0/en/adding-udf.html
to get an impression of the hard work to be done for it (as opposed to the convenient way when programming UDF by using SQL1999 standards).
krs,
tesu
•
•
Join Date: Apr 2008
Posts: 296
Reputation:
Solved Threads: 42
Here is an UDF example which runs on SQL Anywhere 9:
I use select upper(substring(... to capitalize first char. You can also use this select within MySQL because all char functions are also available there.
sql Syntax (Toggle Plain Text)
CREATE FUNCTION upperFirstChar(IN isc VARCHAR(1000)) RETURNS VARCHAR(1000) /* Usage: select upperFirstChar('hello!') AS 'Capitalized 1st Character'; Result: Capitalized 1st Character ------------------------- Hello! */ BEGIN DECLARE rtc VARCHAR(1000); SELECT UPPER(SUBSTRING(isc, 1, 1)) + LOWER(SUBSTRING(isc, 2, LENGTH(isc)-1)) INTO rtc; RETURN rtc; END;
•
•
Join Date: Aug 2007
Posts: 199
Reputation:
Solved Threads: 0
sir,
I can not create the below function in MySQL...
what I should I do to just create the below function in MySQL 5.0.41. could you just guide through that
I can not create the below function in MySQL...
what I should I do to just create the below function in MySQL 5.0.41. could you just guide through that
•
•
•
•
Here is an UDF example which runs on SQL Anywhere 9:
I use select upper(substring(... to capitalize first char. You can also use this select within MySQL because all char functions are also available there.sql Syntax (Toggle Plain Text)
CREATE FUNCTION upperFirstChar(IN isc VARCHAR(1000)) RETURNS VARCHAR(1000) /* Usage: select upperFirstChar('hello!') AS 'Capitalized 1st Character'; Result: Capitalized 1st Character ------------------------- Hello! */ BEGIN DECLARE rtc VARCHAR(1000); SELECT UPPER(SUBSTRING(isc, 1, 1)) + LOWER(SUBSTRING(isc, 2, LENGTH(isc)-1)) INTO rtc; RETURN rtc; END;
•
•
Join Date: Apr 2008
Posts: 296
Reputation:
Solved Threads: 42
Hi tanha,
the given UDF should only be an example on how to program UDF using SQL Standard features. Since SQL 2003 (not 1999) there are UDFs defined, and they can be created with a rather new procedural language called PSM (Persistent Storage Moduls now part of SQL Standard, somehow weird name for programming language), and today, nearly all databases support this PSM, e.g. MS SQL Server, Oracle (within PL/SQL), DB/2, Sybase Databases etc. So far, MySQL 5 does not support PSM, maybe further version will do so.
Therefore, it is impossible to create this UDF example on a MySQL Database.
As already stated, in to-day MySQL there is an other approach for programming UDF, which is based on C programming and dynamically loaded libraries.
http://dev.mysql.com/doc/refman/5.0/en/adding-udf.html[/url] explains much about those UDFs.
http://www.mysqludf.org/lib_mysqludf_str/index.php[/url] has a great collection of already programmed UDF.
There you can find a function str_ucfirst (makes uppercase the first character of the string) which does exactly the task you are looking for. You can download the source, then take a look at the three programming-parts the function is made up. You should carefully follow the instructions on how to create and install such library. The most important step is that the dynamically loaded library must be installed on the computer where the MySQL server is running. I hope, you have access to your server.
krs,
tesu
the given UDF should only be an example on how to program UDF using SQL Standard features. Since SQL 2003 (not 1999) there are UDFs defined, and they can be created with a rather new procedural language called PSM (Persistent Storage Moduls now part of SQL Standard, somehow weird name for programming language), and today, nearly all databases support this PSM, e.g. MS SQL Server, Oracle (within PL/SQL), DB/2, Sybase Databases etc. So far, MySQL 5 does not support PSM, maybe further version will do so.
Therefore, it is impossible to create this UDF example on a MySQL Database.
As already stated, in to-day MySQL there is an other approach for programming UDF, which is based on C programming and dynamically loaded libraries.
http://dev.mysql.com/doc/refman/5.0/en/adding-udf.html[/url] explains much about those UDFs.
http://www.mysqludf.org/lib_mysqludf_str/index.php[/url] has a great collection of already programmed UDF.
There you can find a function str_ucfirst (makes uppercase the first character of the string) which does exactly the task you are looking for. You can download the source, then take a look at the three programming-parts the function is made up. You should carefully follow the instructions on how to create and install such library. The most important step is that the dynamically loaded library must be installed on the computer where the MySQL server is running. I hope, you have access to your server.
krs,
tesu
Last edited by tesuji; May 11th, 2008 at 8:13 am.
![]() |
Other Threads in the MySQL Forum
- Previous Thread: traveldb script
- Next Thread: postgreSQL downloafِ
| Thread Tools | Search this Thread |
agplv3 alfresco amazon api aws bizspark breathalyzer changingprices cmg communityjournalism contentmanagement contractors copyright count court crm database design developer development distinct drupal dui ec2 email enterprise eudora facebook form foss gartner gnu government gpl greenit groklaw groupware hiring hyperic images innerjoins insert ip joebrockmeier join journalism keyword keywords kickfire laptop law legal license licensing linux maintenance managing mariadb matchingcolumns metron micromanage microsoft microsoftexchange mindtouch montywidenius mozilla multiple mysql mysqlcolumnupdating mysqldatetimeordermax() mysqlindex mysqlinternalqueries mysqlquery mysqlsearch news open-xchange opendatabasealliance opengovernment opensource oracle penelope php priceupdating query referencedesign reorderingcolumns resultset saas select sharepoint simpledb sourcecode spotify sql sugarcrm syntax techsupport thunderbird transparency virtualization






