我发现使用pyodbc在python中读取文件并批量执行实际上比在外部使用SQLCMD实用程序要快(而且我不必在运行脚本的每台计算机上都安装SQLCMD!)。
这是我使用的代码(因为pyodbc似乎没有
executescript()方法):
with open(scriptPath, 'r') as inp: for line in inp: if line == 'GOn': c.execute(sqlQuery) sqlQuery = '' elif 'PRINT' in line: disp = line.split("'")[1] print(disp, 'r') else: sqlQuery = sqlQuery + lineinp.close()


