473,394 Members | 1,800 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Gather Meta Data

Hi,

I would like to prepare a data dictionary for my database (northwind).

I have framed the below SQL

SELECT
'NAME ' = a.name,
'DESCRIPTION' = b.value,
'Type ' = type_name(a.xusertype),
' ' AS 'Values',
'NULL ' = case when a.isnullable = 0 then ' ' else 'X' end,
' ' AS 'PK',
' ' AS 'FK'
FROM
syscolumns a,
sysproperties b

WHERE
a.id = 2073058421 AND --- Customers Table
a.number = 0 AND
b.id = a.id AND
b.smallid = a.colid

ORDER BY a.colid

and the output would be:

NAME DESCRIPTION Type Values NULL PK FK
-------------- --------------- ---------- ------ ----- ---- ----
CustomerID Customer ID nchar
CompanyName Company Name nvarchar
ContactName Contact Name nvarchar X
ContactTitle Contact Title nvarchar X
Address Address nvarchar X
City City nvarchar X
Region Region nvarchar X
PostalCode Postal Code nvarchar X
Country Country nvarchar X
Phone Phone # nvarchar X
Fax Fax # nvarchar X

PK and FK is where I need to print whether the column is Primary Key or
Foreign Key.

If CustomerId is defined as Primary Key then PK should have X printed.
Thats the objective.

How will I accomplish this ?
Thanks in advance,
Anu

Jul 23 '05 #1
3 1462
The following query is close to your requirements:

SELECT c.name AS ColumnName,
p.value AS ColumnDescription,
TYPE_NAME(c.xusertype) AS Type,
CASE WHEN c.isnullable=0 THEN ' ' ELSE 'X' END AS Nullable,
CASE WHEN EXISTS (
SELECT * FROM sysobjects o
INNER JOIN sysindexes i ON i.id=o.parent_obj AND i.name=o.name
INNER JOIN sysindexkeys k ON i.id=k.id AND i.indid=k.indid
WHERE o.xtype='PK' AND k.id=c.id AND k.colid=c.colid
) THEN 'X' ELSE ' ' END AS PK,
CASE WHEN EXISTS (
SELECT * FROM sysforeignkeys f
WHERE c.id=f.fkeyid AND c.colid=f.fkey
) THEN 'X' ELSE ' ' END AS FK
FROM syscolumns c
LEFT JOIN sysproperties p on p.id=c.id AND p.smallid = c.colid AND
p.name='MS_Description'
WHERE c.id = OBJECT_ID('Orders')
ORDER BY c.colid

with the following observations:

1. A column is not "defined as" primary key. It is "a part of" the
primary key (see the "Order Details" table in Northwind, which has a
compound primary key). The same goes for foreign keys (it is possible
to have a compound foreign key, although this is rarely encountered).

2. The sysproperties table should be LEFT JOIN-ed to the other tables
(because if you use an INNER JOIN, the columns that do not have a
description will be omitted). We should also filter for
p.name='MS_Description' because there may be also other properties in
the sysproperties table. Moreover, it should be noted that this table
is undocumented and we should use the fn_listextendedproperty function
(because using undocumented tables may cause problems in later versions
of SQL Server).

3. Use the OBJECT_ID function instead of using directly the ID of an
object.

4. Use ANSI-style joins, where the join condition is clearly placed in
the ON clause (instead of putting it in the WHERE clause, with the
other conditions).

5. I have no idea what you will put in the "Values" column (this column
seems very odd to me).

Razvan

Jul 23 '05 #2
Razvan, nice query. Perhaps for Anuu's value column you could have
provided the Default Value when one existed. That is the only "value"
that would seem to make any sense.

IMHO -- Mark D Powell --

Jul 23 '05 #3
Thanks, Razvan. The query is pretty simple and delivers what I need.

Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: White_Falcon | last post by:
Hello, I wonder if anyone has experience in constructing a meta-schema or a meta-model. I wish to build a meta-schema containing various data types and components which would shape up a form...
0
by: Hans Oesterholt-Dijkema | last post by:
Hi, I'm looking for a way to represent meta data using an XML Schema. However, I'm running into a small problem: 1. I want to have meaningfull XML tags 2. I want my meta data to be able to...
11
by: Barney Norris | last post by:
Hi, The W3C validator tells me this page isn't valid HTML 4.01 Strict: http://www-student.cs.york.ac.uk/~jban100/wont_validate.html The reason it gives is I've closed meta tags with a '/'...
19
by: Christian Hvid | last post by:
Hello groups. I have a series of applet computer games on my homepage: http://vredungmand.dk/games/erik-spillet/index.html http://vredungmand.dk/games/nohats/index.html...
30
by: Anon | last post by:
If Http headers specify the character encoding, what is the point of the Meta tag specifying it?
0
by: Shakil Khan | last post by:
Hi there ... My question is about Meta Data which is automatically saved with some files. For example,when an MS Office Documents is saved, it automaticaly save some extra information with the...
3
by: J1C | last post by:
How can I programatically add meta tags with javascript?
4
by: Manchild | last post by:
I recently build myself a meta-tag based search engine (http://search.floip.com) - not because I think its worth doing on its own but because there where a heap of functions I wanted to write in...
3
by: Jordan S. | last post by:
I'm looking to localize an ASP.NET Web application for English, Spanish, and French (fr-CA), and was just considering the possibility of localizing the meta tags (e.g., keywords, title, and...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.