Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Automated detection of different tables with same name

by Win (Novice)
on May 15, 2006 at 11:41 UTC ( [id://549440]=perlquestion: print w/replies, xml ) Need Help??

Win has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.
  • Comment on Automated detection of different tables with same name

Replies are listed 'Best First'.
Re: Automated detection of different tables with same name
by marto (Cardinal) on May 15, 2006 at 12:15 UTC
    Win,

    This seems quite similar to Comparing databases - what is the best way?, did none of the advice given help you? It seems to me, based on previous discussion, that you should spend some time familiarising yourself with the MS SQL Server product more. You should look at Compare SQL Server Databases with sp_CompareDB which illustrates a stored procedure to achieve this goal. You could then call this stored procedure via Perl, as you already know how to do that, having looked at your previous posts on this subject.

    Hope this helps

    Martin

    Update. Win, to avoid any confusion please mark updates you have made to your questions after posting them.

      But that's not the Win style. Off-topic SQL questions stealth updated to try and give the thinnest veneer of some slim splinter of being possibly somewhat maybe tangentially Perl-related, that's the Winning way.

      Now what I want to know is . . . where's my pony?

        Now what I want to know is . . . where's my pony?
        Before you post questions like this, you really should do at least a basic search of the CPAN. If you'd done that, you might have discovered Acme::Pony on your own. As it is, you effectively appear to be asking us to do your search for you, as if you are somehow too lazy to use punch "search.cpan.org" into your browser's address bar yourself, or something. The thing is, it effectively costs you time, because it takes you longer to post the question (to say nothing of waiting for the answer) than to just do the search and find the answer. Next time, try doing a basic search before posting this kind of question. It'll get you faster results and keep you from looking foolish. HTH.HAND.
        'Now what I want to know is . . . where's my pony?'

        That puts me in mind of a Family Guy sketch:

        "Peter (to Meg): Remember that pony you wanted when you were 6? Well I've been waitin for a time like this.(opens closet door and a skeleton of a pony is there)
        Peter: Oh, oh god, that's right ponies, ponies like food. "

        Martin

        Of course, you could be talking about Ponie - the attempt to superglue the parrot onto its perch (Maybe it's pining for the fjords)

        Anyway, the concept of "I want my pony" was indirectly responsible for the name "Ponie" via London.pm with a few beers as catalyst.

        The Ocado UK internet grocery service was marketed using a series of spoiled brats, and I also notice that Ocado stocks the "My little pony" series. Oh well :)

        --

        Oh Lord, won’t you burn me a Knoppix CD ?
        My friends all rate Windows, I must disagree.
        Your powers of persuasion will set them all free,
        So oh Lord, won’t you burn me a Knoppix CD ?
        (Missquoting Janis Joplin)

      A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Automated detection of different tables with same name
by ptum (Priest) on May 15, 2006 at 13:57 UTC

    Well, once again you seem to have irritated some monks, and the whole pony question has been raised, which will probably keep this thread alive for days. But I think it is worth offering you some help, even if you have sometimes been accused of not listening. Heck, SOPW is often more about drive-by learning than helping the OP. :)

    I think marto raises an interesting question -- did none of the answers to your earlier question help you? If you want to get help from other monks, you'll need to demonstrate more effectively that you are listening carefully. Please forgive the mild lecture.

    If I were trying to compare two MS SQL databases, I would go about it by breaking the problem up into tiny manageable pieces:

    • Build a connection to each of the databases in question. I seem to recall that you are operating in a Windows environment, so you probably have no trouble with this step (unlike the challenges that someone in a Unix environment might face).
    • Figure out how to list all the tables in a database. Something like this code probably would do:
    • my $table_statement = "SELECT table_name FROM ${database}.information_ +schema.tables";
    • Increment the table names from the first database into a hash. Assuming you have a statement handle open, do something like this:
    • my %tablenames = (); while ((@row) = $sth->fetchrow_array) { $tablenames{$row[0]}++; }
    • Do the same thing with the table names from the second database.

    When the dust settles, you should have a hash with a bunch of table names, some of which have a value of 1, and others that have a value of 2. The tables with a 2 are the ones which appear in both databases.

    This is a pretty simple problem, and you ought to be able to adapt this solution for your specific needs. Good luck! :)


    No good deed goes unpunished. -- (attributed to) Oscar Wilde
Re: Automated detection of different tables with same name
by Fletch (Bishop) on May 15, 2006 at 12:11 UTC

    Ooh, and can I have a pony. A big shiny one with those rotating rims, because they're TEH C001Z0R!!!!!!!1!ONEONE

Re: Automated detection of different tables with same name
by blue_cowdawg (Monsignor) on May 15, 2006 at 13:54 UTC
        I was wondering whether anyone could kindly donate me a Perl script that will compare two MS SQL Databases

    Before I pony up here... let me get this straight. You want one of these kind souls who hang out here to give you the benefit of their Perl expertise to do your work for you?

    Honestly, this sounds to me like the sort of program spec that I get from paying customers who pay very good money to me to write this sort of thing.

    And my rates ain't cheap!

    Now, if you had attempted to write something yourself and were having issues getting it to work and posted a sniglet of the code that isn't working, then I think you'd get a much warmer reception my fellow monk.


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
Re: Automated detection of different tables with same name
by holli (Abbot) on May 15, 2006 at 11:56 UTC
    Yeah, and I was wondering wether anyone could kindly balance my account.

    Oh, my.


    holli, /regexed monk/
Re: Automated detection of different tables with same name
by gellyfish (Monsignor) on May 15, 2006 at 14:11 UTC

    Of course you realize that this isn't really a Perl question at all, once you have the answers to two questions: How do I find the databases on a given MS-SQL server instance? and How do I find all the user tables in a MS-SQL database?, which are both answered in documentation that you get with every installation of MS SQL Server, then all you have remaining is a rather trivial Perl problem.

    I'm not quite sure why I am giving you this as you seem determined to make no effort to help yourself, but it's Monday and hey I'd only be writing code that I actually get paid for rather than writing code that someone else is getting paid for.

    #!/usr/bin/perl # use strict; use warnings; use DBI; my $dbh = DBI->connect( 'dbi:ODBC:prism72', 'sa', 'xxxx', { RaiseError => 1, } + ); my $sth_master = $dbh->prepare("select [name] from master..sysdatabase +s"); $sth_master->execute(); my @dbs; while ( my $row = $sth_master->fetch() ) { push @dbs, $row->[0]; } $sth_master->finish(); my %tables; foreach my $db (@dbs) { my $sth_obj = $dbh->prepare(<<EOFOO); select [name] from ${db}..sysobjects where xtype = 'U' EOFOO $sth_obj->execute(); while ( my $row = $sth_obj->fetch() ) { my $table = lc $row->[0]; if ( !exists $tables{$table} ) { $tables{$table} = []; } push @{ $tables{$table} }, $db; } $sth_obj->finish(); } foreach my $table ( keys %tables ) { if ( @{ $tables{$table} } > 1 ) { print "table $table is in databases @{$tables{$table}}\n"; } }

    /J\

Re: Automated detection of different tables with same name
by wfsp (Abbot) on May 15, 2006 at 13:28 UTC
    Hi Win,

    I think some of the monks have been a bit unkind. The following script will do what you want.

    #!/usr/bin/perl use strict; use warnings; my $prog = 'Compare2MSSQLDatabases'; system $prog or die "$?";
    I'm sure if you ask MS nicely they will donate the Compare2MSSQLDatabases program.

    Best of luck.

      But one sometimes has to be cruel to be kind.

      But I doubt Bill will pony up either.

      Pony.

      PONY!

      Update: Bill. Pony. Bill the pony. Unintentional serendipitous LotR reference FTW.

Re: Automated detection of different tables with same name
by blazar (Canon) on May 15, 2006 at 13:12 UTC
    I was wondering whether anyone could kindly donate me a Perl script that will compare two MS SQL Databases

    Hmmm, this is SoPW. I see in the menu above that there's a Donate section. Maybe you'll find that more appealing...
    ;-)

Re: Automated detection of different tables with same name
by bart (Canon) on May 15, 2006 at 13:57 UTC
    You can do it in Perl... Or you could look at other tools.

    What I would think of, is find a way to do a structure dump of the databases to text files, the way MySQL can do it, and then compare them with a standard diff. That'll focus on the differences.

    I've hunted around a little, and it seems to me this guy has written a tool to dump the database structure for MS-SQL databases.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://549440]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-04-23 16:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found