How to fix ERROR 1303: Can't create stored routine from within another in MySQL

MySQLINTERMEDIATEMEDIUMUpdated Dec 25, 2025ยท 122 views

MySQL prevents creating stored procedures, functions, or triggers from within another stored routine. This error commonly occurs when using GUI tools like Navicat with incomplete syntax or when attempting recursive routine creation.

Quick fix

Fixes ERROR 1303: Can't create stored routine from within another

CREATE PROCEDURE parent_proc()
BEGIN
  -- This line causes ERROR 1303:
  CREATE PROCEDURE child_proc()
  BEGIN
    SELECT 'This will fail';
  END;
END;
  1. Verify the error occurs in a CREATE statement inside a routine
  2. If using a GUI tool, remove the CREATE PROCEDURE line
  3. Refactor to create routines outside the parent routine
  4. Avoid dynamic SQL for routine creation
mysql โ€” error
CREATE PROCEDURE parent_proc()
BEGIN
-- This line causes ERROR 1303:
CREATE PROCEDURE child_proc()
BEGIN
SELECT 'This will fail';
END;
END;
ERROR 1303: Can't create stored routine from within another

What this error means

MySQL enforces strict restrictions on stored routine operations to maintain database stability and prevent circular dependencies. Error 1303 (ER_SP_NO_RECURSIVE_CREATE) is triggered when you attempt to execute a CREATE PROCEDURE, CREATE FUNCTION, or CREATE TRIGGER statement from inside another stored routine. This is a fundamental limitation of MySQL that applies across all versions and cannot be bypassed. The error message indicates that nested routine creation is not permitted, regardless of the type of routine being created. This restriction exists because allowing nested creation would introduce complex dependency management, potential infinite loops, and unpredictable behavior in stored program execution.

Typical symptoms

  • Error 1303 appears when running CREATE PROCEDURE or CREATE FUNCTION statements inside a stored procedure
  • Navicat or other GUI tools throw the error when you include the CREATE PROCEDURE line in the procedure editor
  • Dynamic SQL with CREATE ROUTINE statements fails when executed from within a stored routine
  • Error occurs even if the new routine has a unique name that does not conflict with existing routines

Common causes

  1. Attempting to create a stored procedure from inside another stored procedure or function
  2. Using a GUI tool like Navicat and including the full CREATE PROCEDURE statement when the tool expects only the routine body
  3. Executing CREATE FUNCTION or CREATE TRIGGER statements within a stored routine
  4. Using dynamic SQL (PREPARE/EXECUTE) to construct and execute CREATE ROUTINE statements inside a stored procedure

How to fix "ERROR 1303: Can't create stored routine from within another"

1Verify the error occurs in a CREATE statement inside a routine

Check your code for any CREATE PROCEDURE, CREATE FUNCTION, or CREATE TRIGGER statements that appear inside another stored routine. Here's an example of what causes the error:

sql
CREATE PROCEDURE parent_proc()
BEGIN
  -- This line causes ERROR 1303:
  CREATE PROCEDURE child_proc()
  BEGIN
    SELECT 'This will fail';
  END;
END;

If you're using a GUI tool like Navicat, MySQL Workbench, or phpMyAdmin, check the stored procedure editor to see if you accidentally included the CREATE PROCEDURE statement.

2If using a GUI tool, remove the CREATE PROCEDURE line

When editing a stored routine in GUI tools, you should only include the routine body, not the CREATE statement. The tool handles the CREATE statement automatically.

Wrong (includes CREATE):

sql
CREATE PROCEDURE my_proc()
BEGIN
  SELECT 'This code';
END;

Correct (body only):

sql
BEGIN
  SELECT 'This code';
END;

Remove the first line and keep only the BEGIN...END block or function body. Then click Save/Apply in your GUI tool.

3Refactor to create routines outside the parent routine

If you need to create routines dynamically, create them at the session or application level, not inside another stored routine.

Before (will fail):

sql
CREATE PROCEDURE create_helper()
BEGIN
  CREATE PROCEDURE helper_proc()
  BEGIN
    SELECT 'Helper';
  END;
END;

CALL create_helper(); -- Error 1303

After (works):

sql
-- Create the helper procedure separately
CREATE PROCEDURE helper_proc()
BEGIN
  SELECT 'Helper';
END;

-- Create the parent procedure that calls the helper
CREATE PROCEDURE create_helper()
BEGIN
  CALL helper_proc();
END;

CALL create_helper(); -- Success

Separate routine creation from routine execution. All stored routines should be created at the database level before they are called.

4Avoid dynamic SQL for routine creation

If you're using prepared statements or dynamic SQL to create routines, remove that approach entirely. MySQL does not support creating stored routines via prepared statements or dynamic SQL.

Wrong (will fail):

sql
CREATE PROCEDURE dynamic_create()
BEGIN
  SET @sql = 'CREATE PROCEDURE new_proc() BEGIN SELECT 1; END';
  PREPARE stmt FROM @sql;
  EXECUTE stmt; -- Error 1303
  DEALLOCATE PREPARE stmt;
END;

Correct approach:
If you need to generate stored routines programmatically, do this at the application level using your database driver (PHP, Python, Node.js, etc.) to execute raw SQL, not from within MySQL.

javascript
// Node.js example: Create routine from application, not database
const mysql = require('mysql2/promise');

async function createRoutines() {
  const connection = await mysql.createConnection(config);
  await connection.query(`
    CREATE PROCEDURE my_proc()
    BEGIN
      SELECT 1;
    END;
  `);
  await connection.end();
}

This gives you the flexibility to generate routine definitions dynamically while respecting MySQL's architectural constraints.

5Test the fix by calling the routine

After restructuring your code, test that the parent routine works correctly:

sql
CALL your_parent_routine();

If the call succeeds without ERROR 1303, the issue is resolved. Check the MySQL error log if you encounter other issues:

sql
-- View the last 50 error log entries
SHOW ERRORS LIMIT 50;

-- Or check the log file directly (path depends on your MySQL config)
-- On Linux: /var/log/mysql/error.log
-- On Windows: C:\ProgramData\MySQL\MySQL Server 8.0\Data\hostname.err

Advanced notes

Why MySQL enforces this limitation:

MySQL prevents nested routine creation to avoid complex dependency management and to maintain the integrity of the stored program environment. Allowing routines to create or modify other routines dynamically would require maintaining a complex dependency tree and handling potential circular dependencies.

Comparison with other databases:

- PostgreSQL allows creating functions from within other functions using dynamic SQL and procedures.
- SQL Server has similar restrictions to MySQL but provides workarounds through temporary stored procedures and sp_executesql.
- Oracle allows PL/SQL blocks to create procedures at runtime using dynamic SQL.

Alternative patterns:

1. Pre-create all routines: Design your database schema so all needed routines exist before application execution.
2. Use application-level logic: Generate SQL strings in your application code and execute them at the session level, not from stored routines.
3. Use triggers for automation: Instead of creating routines dynamically, use triggers to automate routine operations on existing procedures.
4. Leverage events: For scheduled tasks that might otherwise require dynamic routine creation, use MySQL events or cron jobs calling application code.

Frequently asked questions

What does "ERROR 1303: Can't create stored routine from within another" mean?
MySQL enforces strict restrictions on stored routine operations to maintain database stability and prevent circular dependencies. Error 1303 (ER_SP_NO_RECURSIVE_CREATE) is triggered when you attempt to execute a CREATE PROCEDURE, CREATE FUNCTION, or CREATE TRIGGER statement from inside another stored routine. This is a fundamental limitation of MySQL that applies across all versions and cannot be bypassed.
How do I fix "ERROR 1303: Can't create stored routine from within another"?
MySQL prevents creating stored procedures, functions, or triggers from within another stored routine. This error commonly occurs when using GUI tools like Navicat with incomplete syntax or when attempting recursive routine creation.
What causes "ERROR 1303: Can't create stored routine from within another"?
Attempting to create a stored procedure from inside another stored procedure or function Using a GUI tool like Navicat and including the full CREATE PROCEDURE statement when the tool expects only the routine body Executing CREATE FUNCTION or CREATE TRIGGER statements within a stored routine
Is the "ERROR 1303: Can't create stored routine from within another" error serious?
It is a medium-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading