大家好,我试图在for循环中创建一个while循环。代码将为我生成一堆SQL Drop索引语句,因此我不必键入除表名之外的任何内容。 excel文件将包含完成Drop Index语句所需的所有信息。这将节省我的打字时间。
预期输出 运行一个循环中的代码,该循环将询问我一个新的表名,然后运行代码并询问我另一个表名或完成。
以下是与
实际配合使用的代码from openpyxl import load_workbook
wb = load_workbook(filename='UnusedIndexes.xlsx')
ws = wb['Indexes1']
ws.cell(row=2, column=2)
ws.cell(row=2, column=2).value
tablename = input("What Table Do you Want: ")
for i in range(1,200,1):
if ws.cell(row=i, column=1).value == tablename:
print("IF EXISTS (SELECT * FROM sys.indexes WHERE NAME = N'[", ws.cell(row = i, column = 2).value, "]')\nDROP INDEX [" + ws.cell(row = i, column = 2).value + "] ON "
+ "[dbo].[" + tablename + "]\nGO\n" )
以下是我尝试让while循环工作的代码。
from openpyxl import load_workbook
wb = load_workbook(filename='UnusedIndexes.xlsx')
ws = wb['Indexes1']
ws.cell(row=2, column=2)
ws.cell(row=2, column=2).value
tablename = input("What Table Do you Want: ")
finished = 'n'
while finished == 'n':
for i in range(1,200,1):
if ws.cell(row=i, column=1).value == tablename:
print("IF EXISTS (SELECT * FROM sys.indexes WHERE NAME = N'[", ws.cell(row = i, column = 2).value, "]')\nDROP INDEX [" + ws.cell(row = i, column = 2).value + "] ON "
+ "[dbo].[" + tablename + "]\nGO\n" )
else:
finished = input("Would you like to enter a new tablename(y/n): ")
if finished == 'y':
break

Excel文件格式
你可以在A中使用任何文本(字符串)值。只需要在For循环
中保留Print语句的功能答案 0 :(得分:0)
弄清楚我自己的问题。我的while循环有点傻。如果有人有兴趣,这里是完整的代码。
from openpyxl import load_workbook
wb = load_workbook(filename='UnusedIndexes.xlsx')
ws = wb['Indexes1']
ws.cell(row=2, column=2)
ws.cell(row=2, column=2).value
finished = 'y'
while finished != 'n':
tablename = input("Enter a tablename for the indexes: ")
print("This is the tablename: " + tablename)
for i in range(1, 200, 1):
if ws.cell(row=i, column=1).value == tablename:
print("IF EXISTS (SELECT * FROM sys.indexes WHERE NAME = N'[", ws.cell(row=i, column=2).value, "]')\nDROP INDEX ["
+ ws.cell(row=i, column=2).value + "] ON " + "[dbo].[" + tablename + "]\nGO\n")
finished = input("Would you like to do another table(y/n): ")