简体   繁体   English

如何使用Perl连接到SQL Server

[英]How to connect to SQL Server using Perl

I am new to Perl and am trying to create a script that connects to my Oracle SQL Server database and returns the results of a table query via email. 我是Perl的新手,正在尝试创建一个脚本,该脚本连接到我的Oracle SQL Server数据库并通过电子邮件返回表查询的结果。 I am having trouble with the database connection part. 我在数据库连接部分遇到麻烦。 Any ideas where I should start? 有什么想法我应该从哪里开始? Any example code would be greatly appreciated. 任何示例代码将不胜感激。 Thanks, 谢谢,

Below is some sample code to get you connected to Oracle using the Perl DBI module. 以下是一些示例代码,可让您使用Perl DBI模块连接到Oracle Adjust database name, username, and password as necessary. 根据需要调整数据库名称,用户名和密码。

#!/usr/bin/perl
use strict;
use DBI;

my $dbName = 'mydb';
my $username = 'username';
my $password = 'password';
my $options =  { RaiseError => 1 };
my $dbh = DBI->connect("dbi:Oracle:${dbName}", $username, $password, $options);

$dbh is a database handle that you can use to execute all the queries that you like. $dbh是一个数据库句柄,可用于执行您喜欢的所有查询。 See the DBI documentation page at CPAN for a concise description of the methods available. 有关可用方法的简要说明,请参见CPAN上的DBI文档页面

First you should be aware of TNSNAMES.ORA file, which has predefined connections in form of 首先,您应该知道TNSNAMES.ORA文件,该文件具有以下形式的预定义连接:

ORA11 =
 (DESCRIPTION = 
   (ADDRESS_LIST =
     (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.0)(PORT = 1521))
   )
 (CONNECT_DATA =
   (SERVICE_NAME = ORA12)
 )
)

(check for above connection on server host if you happen to be on different machine) (如果您碰巧在另一台计算机上,请检查服务器主机上的上述连接)

Now you can use ORA11 as db name 现在您可以使用ORA11作为数据库名称

  my $DB = DBI->connect(
    "dbi:Oracle:",
    "USER/PASSWORD\@ORA11",
    "",
    {
      # ChopBlanks => 1,
      # AutoCommit => 0,
    },
  );

or use complete connection string instead of ORA11 : 或使用完整的连接字符串代替ORA11

"USER/PASSWORD\@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.1.0)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=ORA12)))"

More connection options can be found in DBD::Oracle DBD :: Oracle中可以找到更多的连接选项

I have a few dozen scripts that perform Oracle , Sybase , and MS SQL queries; 我有几十个脚本来执行OracleSybaseMS SQL查询。 even some scripts that perform queries on multiple databases and combine results, or build a query for one database based on result of a prior query ... but after many days of trying to get Perl libraries to play well. 甚至是一些脚本,它们在多个数据库上执行查询并合并结果,或者根据先前查询的结果为一个数据库建立查询...但是经过了很多天试图使Perl库发挥良好作用。

For those of us that are real hacks, we start by using the Oracle SQL command-line client sqlplus.exe , which makes this approach easy, but far from pretty: 对于真正的黑客,我们首先使用Oracle SQL命令行客户端sqlplus.exe ,这使此方法很容易,但远非如此:

my $run_sql = 'sqlplus.exe -s DBuser/DBpwd@DBname < SQLfile.sql';
my $SQLfile = "temp.sql"; 

sub GET_EMP_LIST
{
  my $status = $_[0];
  my $sql_text = "
  set linesize 150
  set pagesize 0
  set numf 99999999999
  set feedback off
  SELECT  
    EMP.FIRST          || ',' ||
    EMP.LAST           || ',' ||
    EMP.PHONE          || ',' ||
    EMP.SALARY
  FROM 
    PERSONNEL.EMPLOYEES  EMP
  WHERE
    (EMP.STATUS = '$status')
  \;";

  open (SQL, $FileOpenWrite, "$SQLfile");
  print SQL $sql_text;
  close (SQL_TEXT);
  my @Results = "$run_SQL";
  unlink $SQLfile;
  return @Results;
}

#MAIN

@Employees = GET_EMP_LIST "Active";
for (@Employees)
{
  my $temp = chomp $_;
  $temp =~ s/\s+//g;        #get rid of white spaces
  my ($FIRST, $LAST, $PHONE, $SALARY) = split /,/, $temp;

  .... do something with it ....
}

Like I say, far from pretty, but quick and easy, and using SQL query tools, like TOAD, you can generate the SQL in a drag & drop program before you integrate into your script. 就像我说的那样,它远非美观,但又快速简便,并且可以使用SQL查询工具(如TOAD)在集成到脚本之前通过拖放程序生成SQL。

I know many folks will say this is a terrible solution, but we pull in data that includes hundreds of thousands of lines of data. 我知道很多人会说这是一个糟糕的解决方案,但是我们引入包含数十万行数据的数据。

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

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