简体   繁体   中英

Trying to extract column names from an SQL query

I've got a very large and nasty set of queries from which I want to extract column names, they will all be of the format <table>.<column>

I'm new to regex and can't seem to see where my pattern match is failing, it won't return anything.

# Read file in and parse out all column names, they must be of the
# form <table>.<column> 
$data_file="C:\\VW_DEP_MTHLY_PROJTD_UNPROJTD_STK_FACT.sql";
open SQL_FILE, $data_file or die "Could not open file: $data_file";

while (<SQL_FILE>) {
    if((/ \w+\.\w+/)) {
        print $1;
    }
}

close SQL_FILE;

You can imagine part of the query like this:

    SELECT PHARMACY_FACT.REC_TYP_ID,
         PHARMACY_FACT.PACK_ID,
         ("VARCHAR"(DATE_PART('YEAR'::"VARCHAR", ASSET.MTHLY_CYC_END_DT)) || LPAD("VARCHAR"(ASSET.IMS_CYC_WK_NBR), 2, '0'::"VARCHAR")) AS IMS_CYC_WK_NBR,
         ASSET.WKLY_CYC_END_DT,
         PHARMACY_FACT.TAX_PCT_RT,
         CASE WHEN ((CASE WHEN (PHARMACY_FACT.INDIR_PUR_AMT NOTNULL) THEN PHARMACY_FACT.INDIR_PUR_AMT WHEN (0 NOTNULL) THEN '0'::"NUMERIC"
    ELSE NULL::"NUMERIC"
    END + CASE WHEN (PHARMACY_FACT.DIR_PUR_AMT NOTNULL) THEN PHARMACY_FACT.DIR_PUR_AMT WHEN (0 NOTNULL) THEN '0'::"NUMERIC"
    ELSE NULL::"NUMERIC"
    END) <> '0'::"NUMERIC") THEN (CASE WHEN (PHARMACY_FACT.INDIR_PUR_AMT NOTNULL) THEN PHARMACY_FACT.INDIR_PUR_AMT WHEN (0 NOTNULL) THEN '0'::"NUMERIC"
    ELSE NULL::"NUMERIC"
    END + CASE WHEN (PHARMACY_FACT.DIR_PUR_AMT NOTNULL) THEN PHARMACY_FACT.DIR_PUR_AMT WHEN (0 NOTNULL) THEN '0'::"NUMERIC"
    ELSE NULL::"NUMERIC"
    END)
    ELSE NULL::"NUMERIC"
    END AS UNPRJ_TOT_PUR_AMT
    ...

In order for $1 to be set, you need to "capture" some part of the regex by putting it in parentheses.

while (<SQL_FILE>) {
    if (/ (\w+\.\w+)/) {
        print $1;
    }
}

Looks like you had the parens outside of the match operator rather than inside it.

your code will find only first column name in each line. You can split each line by spaces and parse primitives

use strict;

my $data_file='/tmp/sosql';
open my $sql_file, $data_file or die "Could not open file: $data_file";

while (my $line = <$sql_file>) {
    foreach my $word (split(/\s/,$line)) {
        if ($word =~ /(\w+\.\w+)/) {
            print $1."\n";
        }
    }
}

close $sql_file;

You have to capture the column name in a group using parenthesis and loop on all column within eacch line:

while (<SQL_FILE>) {
    while(/(\w+\.\w+)/g) {
        print $1,"\n";
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM