- 
                Notifications
    You must be signed in to change notification settings 
- Fork 130
Create and Run stored procedures
        Sicong edited this page Aug 25, 2021 
        ·
        1 revision
      
    - Create a stored procedure in migrations
 Inyourapp/migrationscreate a custom migration file.
from django.db import migrations
class Migration(migrations.Migration):
    dependencies = [
        ('mytestapp', '0001_initial'),
    ]
    operations = [
        migrations.RunSQL("""CREATE PROCEDURE mySP
                             ...
                          """
       ),
    ]$ python manage.py migrate
- The stored procedure can be called with a raw SQL query.
from django.db import connection
def run_mySP():
    with connection.cursor() as cursor:
        cursor.execute("EXEC mySP;")
        result = cursor.fetchall()
    return result