Groups | Blog | Home
all groups > sql server (alternate) > september 2003 >

sql server (alternate) : Suppressing duplicate rows


monisha NO[at]SPAM ggn.hcltech.com
9/14/2003 11:38:51 PM
Hi,

I need to get a list of columns and corresponding datatypes for the
given table name. I am using the query:

select
a.name,
b.type_name
from
syscolumns a,
master.dbo.spt_datatype_info b,
systypes c,
sysobjects d
where
a.xusertype = c.xusertype and
b.ss_dtype = c.xtype and
a.ID = (SELECT OBJECT_ID(<tablename>)) and
a.ID = d.ID and
d.ID = (SELECT OBJECT_ID(<tablename>))

This returns multiple sets of data for each column. This is because
the same column name is repeated in multiple tables in primary
key-foreign key relationships. Is there a way in which I can get only
Simon Hayes
9/15/2003 10:25:30 AM

[quoted text, click to view]

Assuming you have SQL7 or greater, then using the INFORMATION_SCHEMA views
is often easier (and they're documented, unlike spt_datatype_info):

use pubs
go

select COLUMN_NAME, DATA_TYPE
from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME = 'titles'
go

The data type lengths etc are also available through that view, if you need
them.

Simon

Erland Sommarskog
9/15/2003 9:57:55 PM
Monisha (monisha@ggn.hcltech.com) writes:
[quoted text, click to view]

Involving the completely undocumented spt tables in a query is not
recommendable.

Here is a query that works with out spt_datatype_info:

select c.name, t.name
from syscolumns c
join systypes t on c.xtype = t.xtype
where c.id = object_id('accountstats')
and t.xusertype <= 255
order by c.colid

I should not that the cut-off value of 255 is not documented, but
something I concluded by inspecting systypes table in a database
that I have.


--
Erland Sommarskog, SQL Server MVP, sommar@algonet.se

Books Online for SQL Server SP3 at
AddThis Social Bookmark Button