Search code examples
sql-serverpython-3.xpymysql

Errors Dropping a Index in Python


I have these pymmsql index errors I created 3 indexes in my code and I want to drop them but they wouldn't drop I get these errors Code:

DbConnect = 'Micros'
myDbConn = pymssql.connect(*******,"******", "*******",DbConnect)
cursor = myDbConn.cursor()
cursor.execute("""DROP INDEX [IF EXISTS]
                Micros ON payrolldata;""")
cursor.execute("""DROP INDEX [IF EXISTS]
               MainSort ON s20data,StoreSort ON s20data;""")
myDbConn.commit()

Error:

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "C:\Python37-32\SqlVersionpr_import.py", line 840, in proceed
Micros ON payrolldata;""")
File "src\pymssql.pyx", line 465, in pymssql.Cursor.execute
pymssql.ProgrammingError: (102, b"Incorrect syntax near 'Micros'.DB-Lib error message 20018, severity 
15:\nGeneral SQL Server error: Check messages from the SQL Server\n")

Solution

  • Correct syntax would be.

    DROP INDEX IF EXISTS IndexName;
    DROP INDEX IndexName;
    

    Square brackets mean that parameter or part of statement is optional. See documentation

    https://learn.microsoft.com/en-us/sql/t-sql/statements/drop-index-transact-sql?view=sql-server-ver15