【问题标题】:How to create and recreate a index in Pymmsql in Python?如何在 Python 中的 Pymmsql 中创建和重新创建索引?
【发布时间】:2020-07-22 21:16:12
【问题描述】:

我正在尝试在 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()
           

【问题讨论】:

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


    【解决方案1】:

    除非索引已经存在,否则您不能使用 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
    

    【讨论】:

    • 那么我如何创建和删除它,然后在每次运行此代码时重新创建它?
    • SQL 中是否存在 IF*?
    • 我现在有这个错误 'cursor.execute("""DROP INDEX [IF EXISTS] Micros on payrolldata;""") File "src\pymssql.pyx", line 465, in pymssql. Cursor.execute pymssql.ProgrammingError: (102, b"'Micros'.DB-Lib 错误消息 20018 附近的语法不正确,严重性 15:\n一般 SQL Server 错误:检查来自 SQL Server 的消息\n")'
    • 为什么方括号中有IF EXISTS?这是来自文档的约定,表明它是可选的。如果包含,则不应将其包围在 [ ] 中。
    • 文件“C:\Python37-32\SqlVersionpr_import.py”,第 472 行,继续 cursor.execute("""DROP INDEX IF EXISTS on payrolldata;""") 文件 "src\ pymssql.pyx",第 468 行,在 pymssql.Cursor.execute pymssql.OperationalError: (156, b"关键字'IF'附近的语法不正确。DB-Lib 错误消息 20018,严重性 15:\n一般 SQL Server 错误:检查消息从 SQL Server\n") 同样的错误
    猜你喜欢
    • 1970-01-01
    • 2016-08-25
    • 2016-02-04
    • 1970-01-01
    • 1970-01-01
    • 2021-10-01
    • 1970-01-01
    • 2015-08-10
    • 2021-06-29
    相关资源
    最近更新 更多