RE: Query metadata (data dictionary) with perl
by Steve Howard other posts by this author
Feb 8 2007 7:26PM messages near this date
Query metadata (data dictionary) with perl
|
Balasan: RE: Query metadata (data dictionary) with perl
I'll see if I can give you a start with this, and tell you where to look for the rest of it.
There may be several ways to get the table names, column names, etc. but one standardized wa
y that is fully support by SQL are the INFORMATION_SCHEMA views. A quick and dirty I wrote t
o query the INFORMATION_SCHEMA.TABLES in SQL 2005 is below. I included a couple of little tr
icks I like to use so I can associate the column names from the statement handle meta-data w
ith the columns themselves. The main thing you need to be able to do what you are asking is
to be able to access the INFORMATION_SCHEMA, though, so that is the main thing you can conce
ntrate on.
use DBI;
my $dbh = DBI-> connect("dbi:ODBC:sql2k5demo", 'changed', 'changed') or die $DBI::errstr;
$dbh-> do('use adventureworks') || die "Can't use adventureworks: $DBI::errstr\n";
my $sth = $dbh-> prepare(qq{SELECT * from INFORMATION_SCHEMA.TABLES}) || die "Can't prepare s
elect of tables: $DBI::errstr\n";
$sth-> execute() || die "Can't execute select of tables: $DBI::errstr\n";
# get the information about the tables:
# here is a trick you can use to get the names of columns as you do this:
my @columns = @{$sth-> {NAME}};
# thanks to the last statement, your column names are now stored in @columns
#demo this:
print join(",\t", @columns) . "\n";
# now that we have the column names, we can associate the column names to the data by bindin
g
# the columns into a hash using the column names as hash keys:
my %data;
$sth-> bind_columns(undef, \@data{@columns});
# fetch the data:
while ($sth-> fetch)
{
# get the rows by the column names:
print join(",\t", @data{@columns}) . "\n";
}
Look in SQL Server Books online, and go to the index tab. Type in INFORMATION_SCHEMA, and yo
u will see a list of INFORMATION_SCHEMA views in the left pane. These will tell you pretty m
uch anything you need to know about a database. I think the main views you might want to loo
k at are the INFORMATION_SCHEMA.TABLES and the INFORMATION_SCHEMA.COLUMNS views. Look over t
he other view to get the constraint information and key information.
Hope this helps,
Steve Howard
From: perl-win32-database-bounces@[...].com [mailto:perl-win32-database-bounces@[...].com] O
n Behalf Of Patrix Diradja
Sent: Thursday, February 08, 2007 3:30 PM
To: Milis perl-win32-database
Subject: Query metadata (data dictionary) with perl
Dear my friends....
Would you be so kind to give a sample of perl script to query (select or whatever) the data
dictionary of a database.
I use MS SQL Server 2005.
And the field information I need are:
Field Name
Field Type
Field Size
Key(primary key or foreign key)
Mandatory/Optional (is_Nullable)
Description.
And how can I insert/update the 'Desciption' field?
Thank you very much.
________________________________
Sekarang dengan penyimpanan 1GB
http://id.mail.yahoo.com/<http://sg.rd.yahoo.com/mail/id/footer/def/*http:/id.mail.yahoo.com
/>
Thread:
Patrix Diradja
Steve Howard
Patrix Diradja
Patrix Diradja
|