I would like to use FME to handle an execution plan from SQL Server (I trying to get a table with a row for each field which is used within a query).
The SET SHOWPLAN_XML ON
function has to be run in it own batch. The normal way would be to us the GO
operator, however this is not part of T-SQL but is normally interpreted by the sqlcmd and osql utilities and therefore the FME SQL interpreter does not understand it. I've tried running 'SET SHOWPLAN_XML ON` in one SQLExecutor transform followed by the query in the next SQLExecutor, but each transform seems to create its own connection meaning that the Showplan setting doesn't persist from one transformer to the next.
Had a brainwave and have managed to use the python transformer to run it using pyodbc
. This can execute queries in their own batch, whilst maintaining a single connection. My Python Caller tramsformer goes like this:
import fmeobjects
import pyodbc
class FeatureCreator(object):
def __init__(self):
pass
def input(self,feature):
newFeature = fmeobjects.FMEFeature()
connectionStr = f"DRIVER={{SQL Server}};SERVER={FME_MacroValues['Server']};Trusted_Connection=yes;"
if FME_MacroValues['Database'] != '':
connectionStr += f"DATABASE={FME_MacroValues['Database']};"
connection = pyodbc.connect(connectionStr)
cursor = connection.cursor()
cursor.execute("SET SHOWPLAN_XML ON;")
cursor.commit()
cursor.execute(feature.getAttribute('query'))
results = cursor.fetchall()[0][0]
newFeature.setAttribute("showplan", results)
self.pyoutput(newFeature)
def close(self):
pass