I would like to use FME to handle an execution plan from MS SQL. 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 interpreted by the sqlcmd and osql utilities and therefore the FME SQL interpreter does not understand it. I've tried running it in one SQL Executor transform followed by the query in the next SQL Executor, but each transform seems to create it's own connection. This means that the settings don't persist from one transformer to the next.
Page 1 / 1
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 transformer 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]N0]
        newFeature.setAttribute("showplan", results)
       Â
        self.pyoutput(newFeature)
       Â
    def close(self):
        pass
Â