MapInfo Pro

 View Only
  • 1.  Using a column alias with a query of the source table

    Posted 07-12-2023 07:44
    Edited by Ryan Cook 07-12-2023 11:42

    Hello!

    I have the user choosing a table (populates a string variable) then choosing a column from that table (populates an alias variable). Let call the table "Incidents", and the column "Node". 

    I later run a select query against Incidents, into "_CROPPED". I then want to run an insert thus:

    Insert into NewTable (Node, Obj) Select UserCol, Obj from _CROPPED

    But obviously my UserCol alias returns an error, as UserCol is "Incidents.Node" not "_CROPPED.Node". Of course, I could simply select the usercolumn in the query that forms _CROPPED, but I seem to remember something like this bothering me before. Is it possible to 'transfer' a column alias from one table to another? 

    ------------------------------
    Ryan Cook
    ORH LTD
    ------------------------------



  • 2.  RE: Using a column alias with a query of the source table

    Employee
    Posted 07-31-2023 01:48

    Hi Ryan

    Sorry for a bit late reply but I've been off on vacation.

    I'd recommend storing the table and the column names into two string variables for a start.

    An Alias variable is a combination of a table and a column name and can be built when you need it. So you can create two Alias variables, one for the dynamic table using the String variable:

    Dim aBase As Alias

    aBase = UserTab & "." & UserCol

    And another using the fixed table name _CROPPED:

    Dim aCropped As Alias

    aCropped = "_CROPPED." & UserCol

    I hope this helps



    ------------------------------
    Peter Horsbøll Møller
    Principal Presales Consultant | Distinguished Engineer
    Precisely | Trust in Data
    ------------------------------



  • 3.  RE: Using a column alias with a query of the source table

    Posted 08-16-2023 05:38

    Hi Peter, no problem - I have been on vacation too! Right, per your message above, wouldn't aBase = UserTab & "." & UserCol simply become "Incidents.Usercol.Incidents.Usercol", seeing as Usercol is a variable which is populated by the user choosing a column via an input statement (and therefore already a combination of table.col.

    I suppose I might be being primitive with how I am populating the Usercol variable. I'm basically doing it this way:

     Pncol = Input("Project Node Number Column", "_Column:", "", "Column", "", "TableInfo(Scriptcontext(2), TAB_INFO_NAME)=" + ProjNodes)

    Regardless of whether Pncol is an alias or string, it is table.col and not just col. Which is fine, until you want to reference the column with a different table...



    ------------------------------
    Ryan Cook
    ORH LTD
    ------------------------------



  • 4.  RE: Using a column alias with a query of the source table
    Best Answer

    Employee
    Posted 08-16-2023 08:22

    Gotcha!

    I didn't realize that you were using the Input() function to get to the name of the column.

    I'm still using my own dialogs to get the user to select a table and column name. In this way, I end up with two strings, one for the table and one for the column.

    This allows me to mix these and use the column variable to point to the same column in another table.

    To get around your issue, I recommend that you give the user-selected column an alias when you select into the query _CROPPED:

    Pncol = Input("Project Node Number Column", "_Column:", "", "Column", "", "TableInfo(Scriptcontext(2), TAB_INFO_NAME)=" + ProjNodes)

    Select Pncol As "__UserColumn" From sUserTable Into _CROPPED

    This allows you to refer to that hardcoded column later on:

    Insert into NewTable (Node, Obj) Select _UserColumn, Obj from _CROPPED

    If you are interested, I have some custom dialogs that let you ask the user to select a table and a column: DLGSelectTableAndColumn

    You pass it some parameters controlling the types of tables and types of columns and then when the user has selected his table and column, you get these values using two functions (DLGSTCGetTableName() and DLGSTCGetColumnName()) in the same module.

    You can find this in my repository on GitHub. There are four different dialogs to pick from.

    I hope this helps

    Peter



    ------------------------------
    Peter Horsbøll Møller
    Principal Presales Consultant | Distinguished Engineer
    Precisely | Trust in Data
    ------------------------------



  • 5.  RE: Using a column alias with a query of the source table

    Posted 08-16-2023 09:18

    Brilliant Peter, I suspected there was a logical way to think about the problem, I just couldn't find the angle. Hardcoding an alias via a select Query is perfect. 

    Thanks again Peter!

    Ryan



    ------------------------------
    Ryan Cook
    ORH LTD
    ------------------------------