0

I have a SQL Server procedure which runs in SQL Server Management Studio (SSMS) without any problem and every time it generates the same table.

I want to run the same procedure using python and pyodbc. In order to do that I wrote a python function run_procedure(my_procedure) which runs the procedure.

The problem is, every time I run the python function, I get a different result!

The procedure:

DROP PROCEDURE IF EXISTS procedure_create_tbl_date_range;
GO

CREATE PROCEDURE procedure_create_tbl_date_range
AS
   DROP TABLE IF EXISTS tbl_date_range;


   DECLARE @dateFrom date;
   DECLARE @dateTo date;

   SET @dateFrom = '2021-12-12';
   SET @dateTo   = '2022-12-12';

   create table tbl_date_range (dates_ date);

   WHILE(@dateFrom < @dateTo)
   BEGIN
      SELECT @dateFrom = DATEADD(day, 1,@dateFrom)
      INSERT INTO tbl_date_range 
      SELECT @dateFrom
   END;

GO

The python function:

import pyodbc
import pandas as pd

def run_procedure(my_procedure):
    conn = pyodbc.connect('Driver={SQL Server};'
                        'Server=.\SQLEXPRESS;'
                        'Database=my_db;'
                        'Trusted_Connection=yes'
                        )

    cur = conn.cursor()
    try:
        res = cur.execute(my_procedure)
        conn.commit()
    except Exception as e:
        print(e)
    finally:
        cur.close()
        del cur
        conn.close()

Now almost each time I run the function below, the query SELECT max(dates_) as max_date_ FROM tbl_date_range returns a different result!

run_procedure("EXEC procedure_create_tbl_date_range")

First try:

enter image description here

Second try:

enter image description here

Does anyone know, why the result is different?!

Amir Py
  • 121
  • 1
  • 4
  • This is not possible given what you have stated. Are you sure you are connecting to the right database? Is the above code your *actual* code? – Charlieface Jun 01 '22 at 21:47
  • Hi, thanks for your response. Yes: I am connecting to the right database. My actual code has only two differences: 1. I have another function which uses `pd.read_sql` to run the query stated in the question. both of these two python functions use the same connection string. 2. I have changed the database name. But I am sure I am using the same database while running both the procedure and the query. – Amir Py Jun 02 '22 at 05:30
  • For a procedure which doesn't return data you shouldn't use `read_sql` because it might not wait for all resultsets. Also what happens if you put `SET NOCOUNT ON;` at the top of the procedure – Charlieface Jun 02 '22 at 09:07
  • First of all thanks a lot:) the `SET NOCOUNT ON;` solved the problem:) Yesterday I had to run all my 20 procedures manually! If you haven't solved it, I would have to do it every week! Second: Even when I run the `run_procedure(my_procedure)`, and wait for 10 seconds and then run the query directly in Azure Data Explorer, it still shows me wrong answer! Thanks you again. It was a great help... How we go further? You would write the answer and I approve it? – Amir Py Jun 02 '22 at 10:09

0 Answers0