简体   繁体   English

如何使用Perl检索临时表的SQL字段名称?

[英]How can I retrieve SQL field names of a temp table using Perl?

Below is the code I'm using to run the query, parse the result set, and parse the rows (respectively) 下面是我用来运行查询,解析结果集和解析行的代码(分别)

$exec_ret = $DBS->SQLExecSQL($STMT);

while ($DBS->SQLFetch() == *PLibdata::RET_OK)
{
      $rowfetch = $DBS->{Row}->GetCharValue($colname[$i]);
}

Can I grab the column/field name of a temp table using similar syntax? 我可以使用类似的语法获取临时表的列/字段名称吗? $colname[$i] is predefined at the top to hold the column/field names. $colname[$i]在顶部预定义以保存列/字段名称。 This is hard-coded right now, but I would rather automate it by pushing values into $colname inside of a loop that runs before the rows are parsed. 这是现在的硬编码,但我宁愿通过将值推入到解析行之前运行的循环内的$colname来自动化它。

What module are you using for database access? 您使用什么模块进行数据库访问? I don't recognize the method names. 我不认识方法名称。

If you're using DBI , you can get the column names from the statement handle after executing it: 如果您正在使用DBI ,则可以在执行后从语句句柄中获取列名:

my $sth = $dbh->prepare($STMT);
$sth->execute;
my $columns = $sth->{NAME_uc};

while (my $row = $sth->fetch) {
  for my $i (0 .. $#$row) {
    print "$columns->[$i]: $row->[$i]\n";
  }
  print "\n";
}

There are 3 versions of the column names: NAME gives the column names as the database returns them, NAME_lc converts them to all lower case, and NAME_uc converts them to all upper case. 列名有3个版本: NAME在数据库返回时给出列名, NAME_lc将它们转换为全部小写, NAME_uc将它们转换为全部大写。 If you care about database independence, I suggest you avoid NAME and use one of the other two. 如果您关心数据库独立性,我建议您避免使用NAME并使用其他两个中的一个。

尝试运行SHOW TABLE yourtable并将其视为SELECT。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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