在存储过程中显示打印消息
show printed messages inside stored procedure
使用 sqlalchemy 执行时,是否可以看到存储过程中嵌入的标准输出(或日志)T-SQL PRINT 消息?
--编辑--
pyodbc 似乎正是出于这个原因添加了一种从游标中检索消息的方法,并且它已被合并到 master 中。我仍然无法弄清楚如何让它冒泡到 sqlalchemy
https://github.com/mkleehammer/pyodbc/pull/765#
--已完结--
我试过将 echo=True 添加到引擎并将日志级别设置为调试,但我看不到任何东西
这就是我执行 usp 的方式
import sqlalchemy as sa
import logging
logging.basicConfig()
logger = logging.getLogger("sqlalchemy.engine")
logger.setLevel(logging.DEBUG)
engine = sa.create_engine(target_uri, echo=True)
statement = sa.sql.text(f"EXEC {usp_schema}.{usp_name}")
with engine.begin() as cnxn:
cnxn.execute(statement)
CREATE PROCEDURE dbo.usp_do_stuff
BEGIN
BEGIN TRAN
PRINT 'DELETE STAGE'
PRINT 'LOAD STAGE'
PRINT 'CLEAN STAGE'
PRINT 'TRANSPORT STAGE'
END TRAN
END;
https://github.com/mkleehammer/pyodbc/pull/765
最近才被合并 (2021-01-21),SQLAlchemy 的 mssql+pyodbc://
方言对此一无所知。如果您希望看到对该功能添加到 SQLAlchemy 的支持,请打开功能请求 here.
同时,解决方法是从当前主分支安装 pyodbc …
pip install git+https://github.com/mkleehammer/pyodbc.git
… 然后使用 .raw_connection()
执行 SP 并检索消息:
"""existing stored procedure:
CREATE PROCEDURE [dbo].[usp_do_stuff]
AS
BEGIN
BEGIN TRANSACTION
PRINT 'DELETE STAGE'
PRINT 'LOAD STAGE'
PRINT 'CLEAN STAGE'
PRINT 'TRANSPORT STAGE'
COMMIT TRANSACTION
END
"""
cnxn = engine.raw_connection()
crsr = cnxn.cursor()
crsr.execute("EXEC usp_do_stuff")
print(crsr.messages)
while crsr.nextset():
print(crsr.messages)
"""
[('[01000] (0)', '[Microsoft][ODBC Driver 17 for SQL Server][SQL Server]DELETE STAGE')]
[('[01000] (0)', '[Microsoft][ODBC Driver 17 for SQL Server][SQL Server]LOAD STAGE')]
[('[01000] (0)', '[Microsoft][ODBC Driver 17 for SQL Server][SQL Server]CLEAN STAGE')]
[('[01000] (0)', '[Microsoft][ODBC Driver 17 for SQL Server][SQL Server]TRANSPORT STAGE')]
"""
使用 sqlalchemy 执行时,是否可以看到存储过程中嵌入的标准输出(或日志)T-SQL PRINT 消息?
--编辑--
pyodbc 似乎正是出于这个原因添加了一种从游标中检索消息的方法,并且它已被合并到 master 中。我仍然无法弄清楚如何让它冒泡到 sqlalchemy https://github.com/mkleehammer/pyodbc/pull/765#
--已完结--
我试过将 echo=True 添加到引擎并将日志级别设置为调试,但我看不到任何东西
这就是我执行 usp 的方式
import sqlalchemy as sa
import logging
logging.basicConfig()
logger = logging.getLogger("sqlalchemy.engine")
logger.setLevel(logging.DEBUG)
engine = sa.create_engine(target_uri, echo=True)
statement = sa.sql.text(f"EXEC {usp_schema}.{usp_name}")
with engine.begin() as cnxn:
cnxn.execute(statement)
CREATE PROCEDURE dbo.usp_do_stuff
BEGIN
BEGIN TRAN
PRINT 'DELETE STAGE'
PRINT 'LOAD STAGE'
PRINT 'CLEAN STAGE'
PRINT 'TRANSPORT STAGE'
END TRAN
END;
https://github.com/mkleehammer/pyodbc/pull/765
最近才被合并 (2021-01-21),SQLAlchemy 的 mssql+pyodbc://
方言对此一无所知。如果您希望看到对该功能添加到 SQLAlchemy 的支持,请打开功能请求 here.
同时,解决方法是从当前主分支安装 pyodbc …
pip install git+https://github.com/mkleehammer/pyodbc.git
… 然后使用 .raw_connection()
执行 SP 并检索消息:
"""existing stored procedure:
CREATE PROCEDURE [dbo].[usp_do_stuff]
AS
BEGIN
BEGIN TRANSACTION
PRINT 'DELETE STAGE'
PRINT 'LOAD STAGE'
PRINT 'CLEAN STAGE'
PRINT 'TRANSPORT STAGE'
COMMIT TRANSACTION
END
"""
cnxn = engine.raw_connection()
crsr = cnxn.cursor()
crsr.execute("EXEC usp_do_stuff")
print(crsr.messages)
while crsr.nextset():
print(crsr.messages)
"""
[('[01000] (0)', '[Microsoft][ODBC Driver 17 for SQL Server][SQL Server]DELETE STAGE')]
[('[01000] (0)', '[Microsoft][ODBC Driver 17 for SQL Server][SQL Server]LOAD STAGE')]
[('[01000] (0)', '[Microsoft][ODBC Driver 17 for SQL Server][SQL Server]CLEAN STAGE')]
[('[01000] (0)', '[Microsoft][ODBC Driver 17 for SQL Server][SQL Server]TRANSPORT STAGE')]
"""