Perl과 MSSQL 연동해서 SELECT 쿼리 날려서 그 결과를 query_out.txt 파일에 출력하기
빨간색 글씨의 DBName, Server IP, ID, PASSWORD, 테이블명은 알맞게 수정해야 함
use DBI;
my $dbs = "dbi:ODBC:driver={SQL Server};database=DBName;server=Server IP";
my ($username, $password) = ('ID', 'PASSWORD');
my $dbh = DBI ->connect($dbs, $username, $password);
#write code here
print "MSSQL Connection Successful ^^\n";
$query = "SELECT * FROM 테이블명";
$sth = $dbh->prepare ($query) or die "prepare failed\n";
$sth->execute( ) or die "unable to execute query $query error $DBI::errstr";
open WFILE,">./query_out.txt";
while ( @first = $sth->fetchrow_array ) {
foreach $field (@first) {
print WFILE "field: $field\n";
}
}
close WFILE;
$dbh->disconnect;
system q[notepad query_out.txt];
댓글