Showing posts with label tablename. Show all posts
Showing posts with label tablename. Show all posts

Monday, March 12, 2012

Query to get all user tables with columns

Hi,

I tried to create a simple view as follows

CREATE VIEW V_ALL_USERTABLE_COLUMNS
AS
(
SELECT
OBJ.NAME as TableName,
COL.NAME as ColName,
TYP.NAME AS TYPE

FROM
SYSOBJECTS OBJ,
SYSCOLUMNS COL,
SYSTYPES TYP

WHERE
OBJ.TYPE = 'U'
AND OBJ.ID = COL.ID
AND COL.TYPE = TYP.TYPE
)

Combined with consistent naming conventions I will use this view to
easily find foreign keys; a la

SELECT *
FROM V_ALL_USERTABLE_COLUMNS
WHERE ColName LIKE ('%user_id')

There is something wrong with my view definition that I don't get
though; it doesn't return all the columns. I have a table with the
following definition

CREATE TABLE [dbo].[c_messages]
(
[cid] [int] IDENTITY (1, 1) NOT NULL ,
[touser_id] [int] NULL ,
[tosession_id] [char] (32) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
,
[fromuser_id] [int] NOT NULL ,
[message] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT
NULL ,
[message_read] [bit] NOT NULL ,
[logout] [bit] NULL
) ON [PRIMARY]
GO

The problem is that the select I used to define the view doesn't
return the touser_id column. I have sort of a sneaking suspicion that
the problem has to do with joining syscolumns.type to systypes.type,
but I don't know what to do instead (I'd really like to include the
type; it's useful if I ever changed the type of a primary key and want
to check that I also changed all the foreign keys).

Any help would be appreciated!Use the information schema rather than the system tables:

SELECT * FROM information_schema.columns

This format is much easier to use.

Your original query should work if you join on XTYPE rather than TYPE
but this is not recommended. In general you should avoid referencing
system tables directly.

--
David Portas
SQL Server MVP
--

Query to get a list of field/column names with the tablm name.

Thanks for reading my question.

I need some help on a query which will provide me with the following information.

TableName ColumName

Table1 Column1

Table1 Column2

Table1 Column3

Table2 Column1

Table2 Column2

Table2 Column3

Table2 Column1

Table3 Column1

Table3 Column2

Thanks.

I got it...

SELECT dbo.sysobjects.name AS TableName, dbo.syscolumns.name AS

ColumnName, dbo.systypes.name AS DataType, dbo.syscolumns.length,

dbo.syscolumns.xprec, dbo.syscolumns.xscale,

dbo.syscolumns.colid AS ColumnSort, dbo.sysindexkeys.colid

FROM dbo.sysobjects INNER JOIN

dbo.syscolumns ON dbo.sysobjects.id =

dbo.syscolumns.id INNER JOIN

dbo.systypes ON dbo.syscolumns.xusertype =

dbo.systypes.xusertype LEFT OUTER JOIN

dbo.sysindexkeys ON dbo.syscolumns.colid =

dbo.sysindexkeys.colid AND dbo.syscolumns.id = dbo.sysindexkeys.id

WHERE (dbo.sysobjects.xtype = 'U')

|||

Thanks for following up your own post. It prevents others from wasting their time trying to help you after you have solved the problem, and it helps others when you share your solution.

|||

use information_schema.columns

Code Snippet

select table_name,column_name from information_schema.columns

order by table_name,ordinal_position

|||

select Table_catalog as Databasename, table_name, column_name from INFORMATION_SCHEMA.COLUMNS

|||

DON'T write your query against the SYSTEM tables, the schema might change without any notice. Always trust with information_schema.xxxx views.

I do like to play with system tables, but when it come to solid solutions, we have to stick with few coditions. Smile|||

Sorry for that unnecessary answer from me. Arnie/Mani and Team are really quick to answer any question. When i open this thread nobody was answered. But when i completed typing there were two reply... Really quick buddies...

g8 keep it up

Madhu

|||

Madhu,

Sometimes that happens this time of day. For slower responses, and a longer 'window of opportunity, try about four hours later in the day.