Compare results of different databases using Perl (MySQL, Oracle, and sqlserver)

Source: Internet
Author: User
#1. SQL .txt contains multiple SQL statements: Select * From Testa select * From testb select * From testcselect * From testd select * From teste select * From testf #2. the Perl script is as follows. The execution results will be placed in the CSV file corresponding to the table using strict; Use warnings; Use dBi; Main (); sub main {my $ dbh_oracle; my $ dbh_mysql; open (in, "SQL .txt") or die "$ !, Opening SQL .txt \ n "; open (fail,"> 00fail.csv ") or die 'unable to create diff file for 00fail.csv $! '; While (<in>) {chomp; # Oracle $ dbh_oracle = DBI-> connect ("DBI: oracle: host = xxx. XX. XX. xxx; SERVICE_NAME = xxx; ", 'xxx', 'xxx') | die" cannot connect to Oracle: $! \ N "; my @ Oracle = getresult ($ dbh_oracle, $ _); # MySQL $ dbh_mysql = DBI-> connect ('dbi: mysql: Database = xxxx; host = xxx. XX. XX. XXX', 'xxx', 'xxx') | die "cannot connect to MySQL: $! \ N "; my @ MySQL = getresult ($ dbh_mysql, $ _); # sqlserver # my $ dbh_sqlservr = DBI-> connect (" DBI: Ado: driver = {SQL Server}; server = localhost; database = xxxx; ", 'xxx', 'xxx') | die" cannot connect to MySQL: $! \ N "; # My @ sqlserver = getresult ($ dbh_sqlservr, $ _); my @ output = Split //; my $ outputname = $ output [-1]; compare (\ @ Oracle, \ @ MySQL, $ outputname);} $ dbh_oracle-> disconnect (); $ dbh_mysql-> disconnect (); close in; close fail ;} sub getresult {my $ DBH = shift; my $ SQL = shift; my @ A; my $ something = $ DBH-> prepare ($ SQL); $ th-> execute; my $ numfields = $ something-> {'num _ of_fields '}; while (my $ ref = $ something-> fetchrow_ar Rayref) {my $ line = ""; for (my $ I = 0; $ I <$ numfields; $ I ++) {$ line. = $ Ref [$ I] If (defined $ Ref [$ I]); $ line. = "," ;}# print out "\ n"; push @ A, $ line ;}$ something-> finish; return @ ;} sub compare {My ($ a_ref, $ B _ref, $ outname) = @ _; my % ta; my @ onlya; my @ onlyb; my @ same; foreach (@ $ a_ref) {chomp; $ Ta {$ _} + = 1;} My % COUNT = % ta; foreach (@ $ B _ref) {chomp; if (exists ($ Ta {$ _}) & $ Ta {$ _}> 0) {$ Ta {$ _}-= 1;} else {push @ onlyb, $ _ ;}} my $ counta; foreach (Keys % Ta) {my $ TMP = $ _; if ($ Ta {$ _}> = 0) {# Only A for (1 .. $ Ta {$ _}) {push @ onlya, $ TMP ;}# same for (1 .. ($ count {$ _}-$ Ta {$ _}) {push @ same, $ TMP ;}}} if ($ # onlyb> 0 or $ # onlya> 0) {print fail "$ outname \ n" if ($ # onlyb> 0 or $ # onlya> 0 ); open (Out, "> export outname.csv") or die 'unable to create diff file for export outname.csv $! '; Print out "only in Oracle: \ n"; print out $ _. "\ n" foreach @ onlya; print out "only in MySQL: \ n"; print out $ _. "\ n" foreach @ onlyb; print out "same: \ n"; print out $ _. "\ n" foreach @ same; close out ;}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.