Question

In SQL transformers, is it possible to use Set SHOWPLAN_XML ON?

  • 11 December 2020
  • 1 reply
  • 3 views

Badge +4

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.


1 reply

Badge +4

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][0]
        newFeature.setAttribute("showplan", results)
        
        self.pyoutput(newFeature)
        
    def close(self):
        pass

 

Reply