Blog of a Filipino Developer about C#, VB.NET, ASP.NET, Java, PHP, SQL Server, MySql and Oracle RSS 2.0
 Saturday, June 17, 2006

I have been working more and more with data recently and its a painstaking task. Alot of my time includes analysis of trades and financial information to create and calculate error free result sets.

This also includes data cleanup..

My task today was to create positional and transactional data for investments. Since the company that i am working for right now is thinking of consolidating the data into one system instead of two(theres two system that basically handles the same thing).This involves digging into one of the biggest database that i have seen and finding out to properly extract data without any idea on how they structured their database (the 3rd party vendor is selling us services to do this but the company declined since it would cost alot of money.)

So here i am digging to every column, every table, every function and every view. I realized that it would be pretty hard to manually go to every database to discover what columns it contain(since there was no db diagram provided by the vendor).

The programmer inside me thought of using SQL SMO/DMO since this would just require me tho modify one of my previous demo regarding this subject.

but... i decided not too since this would be just a one time thing and can be accomplished using sql scripts and just pasting the resultset to Excel so that i could have a suitable documentation for the database structure.

One thing to remember is that sysobjects, syscolumns and systypes are your friends. This are the database objects that we would be using for this demo.

Sysobjects contains information about each object in the database. this includes the object name, the user id of the user who created this object and many other useful information.

      select * from sysobjects;

The most useful column in this table is the xtype column. This column signifies what type of object is in the returned rowset. specifying

      select * from sysobjects where xtype = 'u' ;

will return the list of tables on the current database. Here's a list of possible values for xtype:

  • C : CHECK constraint
  • D : Default or DEFAULT constraint
  • F : FOREIGN KEY constraint
  • L : Log
  • P : Stored procedure
  • PK : PRIMARY KEY constraint (type is K)
  • RF : Replication filter stored procedure
  • S : System tables
  • TR : Triggers
  • U : User table
  • UQ : UNIQUE constraint (type is K)
  • V : Views
  • X : Extended stored procedure
  • TF : Functions

You can use syscolumns  to retrieve columns on the database. doing

      select * from syscolumns;

would return a result containing column information which you can use to determine the scale, data type, precision and etc. The xtype on the syscolumns table acts differently from the sysobjects. the xtype column here represents the datatatype of that column. running this script:

   select * from syscolumns where xtype = 167;

will return all columns that has a datatype of varchar. heres a list of possible values for this xtype column:

127 : bigint
173 :  binary
104 :  bit
175 :  char
61 :  datetime
106 :  decimal
62 :  float
34 :  image
56 :  int
60 :  money
239 :  nchar
99 :  ntext
108 :  numeric
231 :  nvarchar
59 :  real
58 :  smalldatetime
52 :  smallint
122 :  smallmoney
98 :  sql_variant
231 :  sysname
35 :  text
189 :  timestamp
48 :  tinyint
36 :  uniqueidentifier
165 :  varbinary
167 :  varchar

I got this xtypes by running : select xtype, name from systypes; which basically contains a list of available sql datatypes.

Putting it all together: So my idea behind this article was to create a list of available tables, and views with their underlying columns. The idea is that i can just join the sysobjects to the syscolumns to get the the tables columns and the link it to systypes to get the column datatype. The complete scripts are listed below as reference to how i was able to accomplish this task.

-----------------------------------------
-- List only the tables in this database
-----------------------------------------
SELECT
o.name AS [Table Name],
o.type,
c.name AS [Col Name],
s.name AS [Col Type],
c.prec,
c.scale,
c.isnullable
FROM
dbo.sysobjects AS o
INNER JOIN
dbo.syscolumns AS c
ON
c.id = o.id
INNER JOIN
dbo.systypes AS s
ON
c.xtype = s.xtype
WHERE (
o.type = 'U'
)

-----------------------------------------
-- List only the views in this database
-----------------------------------------
SELECT
o.name AS [View Name],
o.type,
c.name AS [Col Name],
s.name AS [Col Type],
c.prec,
c.scale,
c.isnullable
FROM
dbo.sysobjects AS o
INNER JOIN
dbo.syscolumns AS c
ON
c.id = o.id
INNER JOIN
dbo.systypes AS s
ON
c.xtype = s.xtype
WHERE (
o.type = 'V'
)

-----------------------------------------
-- List only the functions in this database
-----------------------------------------
SELECT
o.name AS [Funtion Name],
o.type,
c.name AS [Col Name],
s.name AS [Col Type],
c.prec,
c.scale,
c.isnullable
FROM
dbo.sysobjects AS o
INNER JOIN
dbo.syscolumns AS c
ON
c.id = o.id
INNER JOIN
dbo.systypes AS s
ON
c.xtype = s.xtype
WHERE (
o.type = 'TF'
)

hmm.. now i need to go back to work :) have fun!

Saturday, June 17, 2006 9:49:35 PM (GMT Standard Time, UTC+00:00)  #    Comments [5] -
SQL
Tracked by:
"her first audition" (her first audition) [Trackback]
"buyphentermineprozac" (buyphentermineprozac) [Trackback]
"annuncio affitti isernia" (annuncio affitti isernia) [Trackback]
"paul posey tallahassee florida" (paul posey tallahassee florida) [Trackback]
"sauna mista milano" (sauna mista milano) [Trackback]
"una sera nel parco" (una sera nel parco) [Trackback]
"Executive Office Furniture" (Executive Office Furniture) [Trackback]
"georgia bulldog" (georgia bulldog) [Trackback]
"pacifica armoire" (pacifica armoire) [Trackback]
"vecchie mature grasse donne" (vecchie mature grasse donne) [Trackback]
"cd organizers" (cd organizers) [Trackback]
"gadget audi" (gadget audi) [Trackback]
"female body builders posing" (female body builders posing) [Trackback]
"congelatore da incasso" (congelatore da incasso) [Trackback]
"cameriere in autoreggenti" (cameriere in autoreggenti) [Trackback]
"Sports Betting Rss Feed" (Sports Betting Rss Feed) [Trackback]
"albuquerque respiratory jobs va medical center" (albuquerque respiratory jobs v... [Trackback]
"tiaras and headpieces" (tiaras and headpieces) [Trackback]
"used kountry aire camper trailer" (used kountry aire camper trailer) [Trackback]
"bollente fantastico nubile" (bollente fantastico nubile) [Trackback]
"invisibile pulcino dildo" (invisibile pulcino dildo) [Trackback]
"bitches getting fuck" (bitches getting fuck) [Trackback]
"fotografico" (fotografico) [Trackback]
"how to make a sword" (how to make a sword) [Trackback]
"sms gratis cellulare" (sms gratis cellulare) [Trackback]
"teen dating forum" (teen dating forum) [Trackback]
"west virginia dui law" (west virginia dui law) [Trackback]
"poor girls for marriage" (poor girls for marriage) [Trackback]
"viaggi abruzzo" (viaggi abruzzo) [Trackback]
"Nice look" (Nice) [Trackback]
"Nice" (Anthony) [Trackback]
"Look good" (Ethan) [Trackback]
"Beatifully" (Michael) [Trackback]
"Look good" (Hannah) [Trackback]
"Beatifully" (Emma) [Trackback]
"Nice" (Olivia) [Trackback]
"Look good" (Samantha) [Trackback]
"Look good" (Ashley) [Trackback]
"Readed" (Anthony) [Trackback]
"Look good" (Matthew) [Trackback]
"Readed" (Andrew) [Trackback]
"Beatifully" (Michael) [Trackback]
"Look good" (Olivia) [Trackback]
"Smile" (Emma) [Trackback]
"Yep" (Andrew) [Trackback]
"Nice" (Daniel) [Trackback]
"Look good" (Matthew) [Trackback]
"Readed" (Jacob) [Trackback]
"Nice" (Joseph) [Trackback]
"Readed" (Matthew) [Trackback]
"Yep" (Abigail) [Trackback]
"Beatifully" (Christopher) [Trackback]
"Good news" (Matthew) [Trackback]
"Look good" (Michael) [Trackback]
"Good news" (Joseph) [Trackback]
"Yep" (Emma) [Trackback]
"Nice" (Anthony) [Trackback]
"Smile" (Hannah) [Trackback]
"Beatifully" (Samantha) [Trackback]
"Look good" (Anthony) [Trackback]
"Good news" (Isabella) [Trackback]
"Look good" (Joseph) [Trackback]
"Readed" (Madison) [Trackback]
"Nice" (Christopher) [Trackback]
"Nice" (Madison) [Trackback]
"Readed" (Samantha) [Trackback]
"Yep" (Michael) [Trackback]
"Good news" (Ashley) [Trackback]
"Good news" (Madison) [Trackback]
"Beatifully" (Christopher) [Trackback]
"Yep" (Emma) [Trackback]
"Look good" (Anthony) [Trackback]
"Beatifully" (Samantha) [Trackback]
"Beatifully" (Ashley) [Trackback]
"Good news" (Christopher) [Trackback]
"Buy cialis pharmacy online." (Buy cialis.) [Trackback]
Archive
<June 2006>
SunMonTueWedThuFriSat
28293031123
45678910
11121314151617
18192021222324
2526272829301
2345678
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2008
Keith Rull
Sign In
Statistics
Total Posts: 246
This Year: 43
This Month: 4
This Week: 0
Comments: 111
Themes
Pick a theme:
Ads
All Content © 2008, Keith Rull
DasBlog theme 'Business' created by Christoph De Baene (delarou)