对查询的多个结果集的处理方法涉及使用在结果集间移动的另一个循环包装读取循环。
必须先准备好返回多个结果集的 SQL 语句,然后才能执行它们。prepare 方法为该语句返回一个句柄。使用该句柄执行语句,然后检索关于结果集和每个结果集的行的元信息。
#!/usr/local/bin/perl -w # use DBI; use strict; my $database = "demo"; my $data_src = "DBI:SQLAnywhere:SERVER=$database;DBN=$database"; my $uid = "DBA"; my $pwd = "sql"; my $sel_stmt = "SELECT ID, GivenName, Surname FROM Customers ORDER BY GivenName, Surname; SELECT * FROM Departments ORDER BY DepartmentID"; my %defaults = ( AutoCommit => 0, # Require explicit commit or rollback. PrintError => 0 ); my $dbh = DBI->connect($data_src, $uid, $pwd, \%defaults) or die "Cannot connect to $data_src: $DBI::errstr\n"; &db_query($sel_stmt, $dbh); $dbh->rollback; $dbh->disconnect; exit(0); sub db_query { my($sel, $dbh) = @_; my($row, $sth) = undef; $sth = $dbh->prepare($sel); $sth->execute; do { print "Fields: $sth->{NUM_OF_FIELDS}\n"; print "Params: $sth->{NUM_OF_PARAMS}\n\n"; print join("\t\t", @{$sth->{NAME}}), "\n\n"; while($row = $sth->fetchrow_arrayref) { print join("\t\t", @$row), "\n"; } print "---end of results---\n\n"; } while (defined $sth->more_results); $sth = undef; } __END__ |
![]() |
使用DocCommentXchange讨论此页。
|
版权 © 2012, iAnywhere Solutions, Inc. - SQL Anywhere 12.0.1 |