1
我想知道是否有一種方法使用pyodbc循環表的記錄,如果它看到一個空白(即'')字段將該值更改爲null。 我有一個工作的python腳本,將列出表中的值。從這裏不確定如何遍歷行並確定列是否填充空格,以及是否將值更改爲null。它基本上是ssis清理的東西,但是不想經歷使用ssis的麻煩。想嘗試使用Python 的東西在這裏是代碼循環通過記錄和更新SQL表,如果存在使用Pyodbc或熊貓
import pyodbc
import time
start_time = time.time()
conn = (
r'Driver={SQL Server};'
r'Server=<enter Server here>;'
r'Database=<enter Database here;'
r'Trusted_Connection=yes;'
)
cnxn = pyodbc.connect(conn)
cursor = cnxn.cursor()
# Create SQL Statement to get rows
sql = """
Select UserOrderField2 From TestAccountPD
where UserOrderField2 = ''
"""
# put all into rows variable
rows = cursor.execute(sql).fetchall()
for row in rows:
print(row)
cursor.close
cnxn.close()
print("---%s seconds ---" % (time.time() - start_time))
使用本機SQL。 'UPDATE TestAccountPD SET UserOrderField2 = NULL WHERE UserOrderField2 =''' – mjsqu