[英]How to loop through tables in python that come from a stored procedure in sql. With While Loop and Pyodbc?
cur = connection.cursor()
cur.execute("set nocount on exec SP")
while(cur.nextset()):
print(cur.description)
此代碼生成 10 個表,有時是 11 個。我希望能夠使用 while 循環,因為每次表的數量可能不同。 上面這段代碼的問題是它生成 9 個表而不是 10 個,因為 cur.nextset() 運行並跳過第一個表。
我能做些什么來解決這個問題? 我希望代碼在循環中工作。 我需要一個與 pyodbc 模塊一起使用的更好的函數。
有任何想法嗎?
您可以通過將nextset()
檢查移動到循環底部來避免跳過第一個結果集:
cur.execute("set nocount on exec SP")
while True:
# do stuff with the result set
if not cur.nextset():
break
print("All result sets have been processed.")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.