sqlipythonipython-magic

IPython SQL Magic - Generate Query String Programmatically


I'm generating SQL programmatically so that, based on certain parameters, the query that needs to be executed could be different (i.e., tables used, unions, etc). How can I insert a string like this: "select * from table", into a %%sql block? I know that using :variable inserts variable into the %%sql block, but it does so as a string, rather than sql code.


Solution

  • The answer was staring me in the face:

    query="""
    select 
    *
    from
    sometable
    """
    %sql $query