我对数据库比较陌生,所以这个问题可能有一个简单的答案(我已经搜索了几个小时)。
我想编写一个Python脚本来提取存储在SQL Server Management Studio中的SQL代码。我可以使用 pyodbc 成功连接到数据库并对数据库表运行查询,但我希望能够提取存储在过程、函数、 View 等中的 SQL 代码,而不运行它。
这似乎是相对简单的事情。我知道可以在 Powershell 中完成,但我更喜欢使用 Python。是否有某种模块或 pyodbc hack 可以做到这一点?
最佳答案
您可以在 SQL Server 中使用 sp_helptext
命令,该命令将为您逐行提供 SQL Server 对象源代码:
stored_proc_text = ""
res = cursor.execute('sp_helptext my_stored_procedure')
for row in res:
stored_proc_text += row[0]
print(stored_proc_text)
祝你好运!
关于python - 使用 pyodbc 从数据库中提取 SQL 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49733624/