Sql print statements from pyodbc

How to get output from sql_query ?

 import pyodbc sql_query = "print 'Hello World'" conn = pyodbc.connect("DRIVER={SQL Server}; SERVER=myserver; DATABASE=mydatabase; UID=myusername; PWD=mypassword") cur = conn.cursor() cur.execute(sql_query) cur.commit() for row in cursor.fetchall(): print row 

So far, I think that SQL printing is out of range from regular structured answers?

http://www.easysoft.com/developer/languages/perl/sql_server_unix_tutorial.html#print_statement_status_messages has something similar to what I'm trying to do in Perl.

The closest I see is optional: http://www.python.org/dev/peps/pep-0249/#cursor-messages So, I think this just isn't implemented?

+7
source share
2 answers

Use RAISERROR on top of the PRINT. Use it with NOWAIT for immediate output. I don't know how to do this in python, but in ADO.NET you can use the InfoMessage event in SqlConnection. There may be something similar in Python.

0
source

When executing the PRINT "Hello World" query, the output is returned as console output (string), and fetchall considers the result set (object) from the request. RAISERROR is an error condition and may stop the process. I'm not saying that this is not a solution, but maybe you just want to print some feedback without creating error conditions.

I think you can achieve the result you are looking for by changing

[PRINT 'Hello World'] on [SELECT 'Hello World']

I think this will create one row of data in the result set with the text you need, which should appear in fetchall and see if you get the expected results.

Hope that an alternative that you can try will help!

0
source

All Articles