如何在Python中的Pymmsql中创建和重新创建索引?

时间:2020-07-22 21:16:12

标签: sql-server python-3.x pymssql

我正在尝试在Python中创建和重新创建索引,但是当我使用它时出现错误。

Error:
  File "src\pymssql.pyx", line 468, in pymssql.Cursor.execute
  pymssql.OperationalError: (7999, b"Could not find any index named 'Micros' for table 
  'payrolldata'.DB-Lib error message 20018, severity 16:\nGeneral SQL Server error: Check messages 
  from the SQL Server\n")

代码:

with pymssql.connect ("127.0.0.1","arcdbadmin", "@rcT3chn010g13$","Micros") as myDbConn:
   with myDbConn.cursor(as_dict=True) as cursor:
       cursor.execute("""create index Micros on payrolldata(stono,payrollid,busdate) WITH(DROP_EXISTING = ON);""")
       myDbConn.commit()
           

1 个答案:

答案 0 :(得分:0)

除非索引已存在,否则您不能使用 WITH(DROP_EXISTING = ON)

您可以先drop index if exists

drop index if exists Micros on payrolldata

如果需要,请先。在较旧的版本上,您可以运行

if exists (select * from sys.indexes where name = 'Micros' and object_id('payrolldata') = object_id)
begin
  drop index micros on payrolldata
end