[英]How to use a Perl variable in a SQL script (using backticks)
我已经看到了各种示例,但是我无法使用DBI。 如何在外部sql脚本中使用perl变量? 例如,我想要的perl脚本中的代码是:
$text = 'Germany';
@sql_output = `sqlplus -s user/pass\@databasename <<!
@/pathtofile/test.sql;
QUIT;
!`;
print @sql_output;
sql脚本为:
SELECT DISTINCT City FROM Customers
Where Country = '$text'
(仅作为示例,我使用的代码来自w3schools.com sql教程http://www.w3schools.com/SQl/trysql.asp?filename=trysql_select_distinct )
我通过搜索发现的一个例子有:
@/pathtofile/test.sql $text;
但是当我用代码尝试时,它不起作用。 这是将perl变量合并到外部sql脚本中的正确方法吗?
要么
这一项工作,基本上流程如下
试试吧,让我知道。
test.pl
#!/usr/bin/perl
$text = 'Germany1';
open(HEADER,"SQLQuery.sql");
while(<HEADER>) { $_ =~ s/\$text/$text/g; $output .= $_; }
close(HEADER);
print "$output";
open (MYFILE, '>SQLQueryTemp.sql');
print MYFILE $output;
close(MYFILE);
#SQLQueryTemp.sql contains the $text replaced with 'Germany'
#@sql_output = `sqlplus -s user/pass\@databasename <<!
#@/pathtofile/SQLQueryTemp.sql;
#QUIT;
#!`;
#print @sql_output;
SQLQuery.sql
SELECT DISTINCT City FROM Customers
Where Country = '$text'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.