sqldjangostored-proceduresdjango-mssql

Calling a Stored Procedure using django-mssql


I'm trying to call a stored procedure on a multi-database project I am working with. I have 2 of my stored procedures working, but this particular stored procedure is not working and returns an error:

Traceback (most recent call last):
File "d:\Projects\Django\AAAVirtualEnv\lib\site-packages\django\core\handlers\exception.py", line 39, in inner
response = get_response(request)
File "d:\Projects\Django\AAAVirtualEnv\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "d:\Projects\Django\AAAVirtualEnv\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "d:\Projects\Django\AAAVirtualEnv\lib\site-packages\django\views\decorators\csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "d:\Projects\Django\AAAVirtualEnv\lib\site-packages\django\views\generic\base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "d:\Projects\Django\AAAVirtualEnv\lib\site-packages\rest_framework\views.py", line 483, in dispatch
response = self.handle_exception(exc)
File "d:\Projects\Django\AAAVirtualEnv\lib\site-packages\rest_framework\views.py", line 443, in handle_exception
self.raise_uncaught_exception(exc)
File "d:\Projects\Django\AAAVirtualEnv\lib\site-packages\rest_framework\views.py", line 480, in dispatch
response = handler(request, *args, **kwargs)
File "d:\Projects\Django\HRSys\hrs\hrs_api\views.py", line 79, in post
result_set = cursor.fetchall()
File "d:\Projects\Django\AAAVirtualEnv\lib\site-packages\django\db\utils.py", line 101, in inner
return func(*args, **kwargs)
File "d:\Projects\Django\AAAVirtualEnv\lib\site-packages\django\db\utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "d:\Projects\Django\AAAVirtualEnv\lib\site-packages\django\utils\six.py", line 685, in reraise
raise value.with_traceback(tb)
File "d:\Projects\Django\AAAVirtualEnv\lib\site-packages\django\db\utils.py", line 101, in inner
return func(*args, **kwargs)
File "d:\Projects\Django\AAAVirtualEnv\lib\site-packages\sqlserver_ado\dbapi.py", line 719, in fetchall
return self._fetch()
File "d:\Projects\Django\AAAVirtualEnv\lib\site-packages\sqlserver_ado\dbapi.py", line 671, in _fetch
self._raiseCursorError(FetchFailedError, 'Attempting to fetch from a closed connection or empty record set')
File "d:\Projects\Django\AAAVirtualEnv\lib\site-packages\sqlserver_ado\dbapi.py", line 488, in _raiseCursorError
eh(self.connection, self, errorclass, errorvalue)
File "d:\Projects\Django\AAAVirtualEnv\lib\site-packages\sqlserver_ado\dbapi.py", line 103, in standardErrorHandler
raise errorclass(errorvalue)
django.db.utils.Error: Attempting to fetch from a closed connection or empty record set

this is how I call the stored procedure:

cursor = connections['backend'].cursor()
    try:
        cursor.callproc('[dbo].[GetLogonControlJSON]', ['10011', 'encrypted', '127.0.0.1'])
        result_set = cursor.fetchall()
    finally:
        cursor.close()

I did this too, but still I get the same error:

cursor = connections['backend'].cursor()
try:
    cursor.execute('EXEC [dbo].[GetLogonControlJSON] @LogonID=%s, @Password=%s, @IP=%s' % ('10011', 'encrypted', '127.0.0.1'))
    result_set = cursor.fetchall()
finally:
    cursor.close()

Please, help me. :)

Thank you!


Solution

  • You should use execute() method instead of callproc() in this case.

    The error message tells you everything:

    Attempting to fetch from a closed connection or empty record set

    Take a look at the documentation (second example): Stored Procedures