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]
Sunday, June 18, 2006 10:58:46 AM (GMT Standard Time, UTC+00:00)
> "One thing to remember is that sysobjects, syscolumns and systypes are your friends."

No, they aren't. information_schema views are.
Thursday, June 22, 2006 4:25:10 PM (GMT Standard Time, UTC+00:00)
aren't the information views calling this sys objects?

the article explained why i went to this path... just for personnal use.
Saturday, July 08, 2006 7:54:26 AM (GMT Standard Time, UTC+00:00)
You're right, the INFORMATION_SCHEMA views are just calling into the sys* tables, but they're better for several reasons:

1) They're actually part of the SQL-92 standard, whereas the sys* tables are internal and unsupported SQL Server specific tables. MS will continue to support the INFO_SCHEMA views (in the SQL Server docs, and just by the fact that they're part of the SQL-92 standard), but has said the sys* tables are subject to change and are unsupported.

2) The INFO_SCHEMA views are a lot easier to use. No need to translate xtype or systype, since the INFO_SCHEMA views handle that. Also a lot fewer joins are needed for the same reason.

3) Most important - every time you mention or post code using the sys* tables, you'll get a flurry of angry geeks telling you to use the INFO_SCHEMA views. No one wants that!

4) Less important, but still interesting - since the INFO_SCHEMA views are a SQL-92 standard, they'll likely work on other databases. When you post INFO_SCHEMA queries, you'll enrich the life of a MySQL coder, allowing him / her to spend more time playing Dungeons and Dragons.
Monday, July 31, 2006 11:06:14 PM (GMT Standard Time, UTC+00:00)
@jon- 3) Most important - every time you mention or post code using the sys* tables, you'll get a flurry of angry geeks telling you to use the INFO_SCHEMA views. No one wants that! >> you're right jon, i'm starting to get hate mail already! :P :) thanks for clarifying...true, i guess i overlooked the idea about cross-compatibility between different db servers via the sql-92 standard
Thursday, August 24, 2006 1:58:08 PM (GMT Standard Time, UTC+00:00)
PARE KO...musta na

Just a comment on this one. It is not recommended to look into the system objects directly (although I sometimes do). What you do is use the INFORMATION_SCHEMA views which were created for that purpose. SQL Server modifies these system tables every time you make changes on other objects. Any wrong (mistake...he...he...) actions done on the system tables will cause misbehavior on your objects (such as accientally deleting an item in the syscomments table which contains the descriptions of programmable objects and the likes)
bass_player
Comments are closed.
On this page
Archive
<October 2008>
SunMonTueWedThuFriSat
2829301234
567891011
12131415161718
19202122232425
2627282930311
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: 254
This Year: 51
This Month: 0
This Week: 0
Comments: 111
Themes
Pick a theme:
Ads
All Content © 2008, Keith Rull
DasBlog theme 'Business' created by Christoph De Baene (delarou)