How to get SQL server returned message?

泪湿孤枕 提交于 2020-01-06 04:04:49

问题


If I use SQL Management Studio. When I create/drop tables, or do any updates, I can see messages like:

"Command(s) completed successfully."
"Cannot drop the table 'my_table', because it does not exist or you do not have permission."

But if I use python to execute the same query:

connection = pypyodbc.connect('my connect options...') 
cursor = connection.cursor() 
my_result = cursor.execute('my create/drop table sql...') 
my_result = connection.commit()

I can see nothing from "my_result", "cursor" or "connection". Is the message generated by sql management studio? If not, and it is a sql server engine returned message, how to get this message?


回答1:


The messages displayed in the messages tab in SSMS are passed as InfoMessage events on the connection. Here's the C# API doco

https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.infomessage.aspx

I'm not a python user so can't really help beyond pointing you at the documentation.

Edit: This answer might help you Getting SQL Server messages using ADO and win32com



来源:https://stackoverflow.com/questions/37734835/how-to-get-sql-server-returned-message

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!