MySQL CREATE FUNCTION Guide – Stored & Loadable Functions

Other Blogs

Blogs ❯❯ SQL

Image could not load

MySQL Function by Shahadat Rahman on Unsplash

What is function ?

Function reusable piece of SQL code या block of SQL code होता है जो कि कोई specific task perform करता है। एक बार define करने के बाद हम इन्हें SQL Query में कितनी ही बार call कर सकते हैं।

MySQL में predefined useful function तो होते ही हैं लेकिन अपने need के according हम Custom Functions भी बना सकते हैं।

CREATE FUNCTION in MySQL?

CREATE FUNCTION का use MySQL में दो तरह के routines बनाने के लिए होता है — Stored Functions (SQL के अंदर लिखे हुए) या Loadable (UDF) Functions जो C/C++ library से link होती हैं।

  • Stored Function वो होती है जिसमे आप SQL के blocks लिख के एक single scalar value return करवा सकते हैं।

  • Loadable (or user-defined) function के लिए आप एक external shared library बना के MySQL के plugin directory से link करते हैं।

MySQL Function Syntax

DELIMITER // CREATE FUNCTION myfunc(param1 INT, param2 VARCHAR(10)) RETURNS INT DETERMINISTIC READS SQL DATA BEGIN DECLARE result INT; SET result = param1 * LENGTH(param2); RETURN result; END// DELIMITER ;
  • CREATE FUNCTION के बाद function name और parameters आते हैं।

  • RETURNS clause बताता है return datatype.

  • DETERMINISTIC & lainnya characteristics (जैसे READS SQL DATA) optimizer accuracy के लिए mandatory होती हैं।

Permissions & Routine Creation

  • Function create करने के लिए CREATE ROUTINE privilege जरूरी है।

  • अगर MySQL binary logging on है, तो SUPER privilege भी जरूरी हो सकता है। 

  • By default, creator को ALTER ROUTINE और EXECUTE भी मिल जाती है।

MySQL Function Example

CREATE FUNCTION hello(s CHAR(20)) RETURNS CHAR(50) DETERMINISTIC RETURN CONCAT('Hello, ', s, '!');

ये function सीधा SELECT statement में use होगा।

Calling function

SELECT hello('world'); -- Returns: Hello, world!

Use Cases & Best Practices

कब use करें? अगर आपको business logic या calculation को reusable banana हो — for example, tax calculation, discount logic, या string manipulations — stored function सही रहेगा।

  • हमेशा DECLARE `DETERMINISTIC` use करें अगर deterministic हो।

  • Specify `READS SQL DATA` अगर आप SELECT statements use कर रहे हो। 

  • DML (INSERT/UPDATE/DELETE) को stored functions के अंदर avoid करें म क्योंकि stored functions data को modify नहीं कर सकते हैं।

MySQL Loadable Functions

अगर आप MySQL built‑in capabilities से आगे function चाहते हैं (जैसे Math, Text‑processing that SQL can’t do fast), तो Loadable Function create कर सकते हैं -

CREATE FUNCTION myreverse RETURNS STRING SONAME 'udf_example.so';
  • ये C/C++ code एक .so (Linux) या .dll (Windows) में compile होनी चाहिए और plugin directory में copy कि जाती है।

  • Use करने के लिए MySQL user को INSERT privilege on mysql.func जरूरी है क्योंकि MySQL registry वहाँ update होता है।

Remove करने के लिए -

DROP FUNCTION myreverse;

Common Errors & Fixes

Functions को define और call करते time गलती के बहुत ज्यादा chances हैं जिन्हे ध्यान में रखना जरूरी है। ऐसी कुछ्ह common mistakes/fixes के बारे में बात करेंगे।

1. "Syntax error near ‘END’"

Function body में multiple statements और semicolons हैं, मगर अगर आपने DELIMITER command correctly set नहीं किया, तो MySQL interpreter last statement पर 'END' के near syntax error throw करता है।

Fix :

  • DELIMITER // या DELIMITER $$ use करें , function लिखने के बाद END// या END$$ करो, fir DELIMITER ; वापस restore करो।

  • मतलब DELIMITER client‑level command है जो बताता है कि SQL statement कहाँ ख़त्म होता है।

2. Function name conflict

अगर आपका function का नाम किसी built-in function जैसा है (जैसे GROUP_CONCAT, NOW, SHA1, etc.), तो MySQL warning या unexpected behavior दे सकता है।

Fix :

  • या तो function का नाम change करो : e.g., hello_world की जगह HELLO().

  • या फिर MySQL parser confusion avoid करने के लिए function name के बाद space लगाओ —जैसे SHA2() की जगह  SHA2 ().

3. "Not allowed to return a result set"

Stored function को ऐसे SELECT ... allow नहीं होता जो multiple rows return करे; अगर आप वो ही लिखते हो, तो ये error आएगा :

ERROR 1415: Not allowed to return a result set from a function.

Fix:

  • सिर्फ single-row data को variable में capture करने के लिए SELECT column INTO var FROM table WHERE ... LIMIT 1; use करो।

  • अगर multi-row data return करना है, तो उसके लिए Stored Procedure, View, या cursor use करो—stored functions में सिर्फ scalars return करने चाहिए।

Hey ! I'm Rahul founder of learnhindituts.com. Working in IT industry more than 5.5 years. I love to talk about programming as well as writing technical tutorials and blogs that can help to others .... keep learning :)

Get connected with me - LinkedIn Twitter Instagram Facebook

Your Thought ?

Please wait . . .

    Recent Blogs

    Loading ...

    0 Comment(s) found !