コード例 #1
0
def extract_most_recent_date(ti):
    try:

        conn = OdbcHook().get_conn()
        cursor = conn.cursor()
        most_recent_date = ti.xcom_pull(key='most_recent_date',
                                        task_ids=['catchup_date'])
        print(most_recent_date)
        sql = None
        if not np.array(most_recent_date
                        ) or not most_recent_date or most_recent_date is None:
            sql = 'SELECT MAX(minDate.Date) AS Date FROM(SELECT TOP 10 *  FROM [dbo].[MV_Amarok_DailyProdWellData] ORDER BY Date) AS minDate'
            cursor.execute(sql)
        else:
            sql = 'SELECT MAX(minDate.Date) AS Date FROM(SELECT TOP 10 * FROM [dbo].[MV_Amarok_DailyProdWellData] where Date > ? ORDER BY Date) AS minDate'
            cursor.execute(sql, most_recent_date)

        print(sql)
        row = cursor.fetchone()
        if row is not None:
            ti.xcom_push(key='most_recent_date',
                         value=row.Date.strftime('%Y-%m-%d %H:%M:%S.%f')[:-3])
        print(row.Date)
        cursor.close()

    except pyodbc.Error as err:
        print('Failed to read data from table', err)
    finally:
        if conn:
            conn.close()
            print('ODBC connection is closed')