[英]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.