1

I know this question has been asked before, I have already looked up here. I have used SET NOCOUNT ON in the SQL Query still it gives me the error, Decided to post my code here so that it can be reviewed well. Can anyone help me with this?

    @property
    def sqlquery(self):
        retry_flag = True
        retry_count = 0
        conn = pyodbc.connect("Driver={SQL Server};"
                              "Server=server1;"
                              "Database=db1;"
                              "Trusted_Connection=yes;")
        cursor = conn.cursor()
        while retry_flag and retry_count < 10:
            try:
                cursor.execute('''
        SET NOCOUNT ON;
        SELECT * FROM xx.y
        WHERE y = '%s'
        AND a = '%s'
        AND b = '%s'
        AND c = '%s'
        AND d = '%s' ''' % (1stplaceholder, 2nd, 3rd, 4th, 5th))
                retry_flag = False
            except:
                print("Retry after 1 sec")
                retry_count = retry_count + 1
                time.sleep(1)
        for row in cursor:
            if cursor is None:
                return False
            else:
                return row[19]
        conn.close()

I have also referred to this question as I was getting Error pyodbc.Error: ('01000', '[01000] [Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionRead (recv()). (10054) (SQLExecDirectW)')

I have 3 sql queries similar to this in my script and after running almost 10k iterations it gave me an error pyodbc.ProgrammingError: No results. Previous SQL was not a query. So, the SQL queries are perfectly fine and so is the connection to database.

Karan M
  • 309
  • 2
  • 17
  • Try removing `SET NOCOUNT ON`. – Gordon Linoff Aug 02 '18 at 23:33
  • I did not have `SET NOCOUNT ON` before, and when I ran into the error **pyodbc.ProgrammingError: No results. Previous SQL was not a query** previously, after that I used `SET NOCOUNT ON` – Karan M Aug 02 '18 at 23:34
  • The only solution I could find was to split the iterations and run the script separately for all of them. – Karan M Aug 15 '18 at 15:04

0 Answers0