How to check the table columns thru T-sql command

  • Hi,

    I want to check the columns that are present in one particular table.

    What is the T-Sql command to check the columns for a table.

  • Check what?

    Existence of columns? data? integrity? statistics? relations? or...?

  • If you just want a list of column names you can use FMTONLY

    SET FMTONLY ON;

    GO

    SELECT *

    FROM MyTable

    go

    SET FMTONLY OFF;

    [font="Verdana"]Markus Bohse[/font]

  • Another way...

    SELECT * FROM INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = 'MyTable'

    Ryan Randall

    Solutions are easy. Understanding the problem, now, that's the hard part.

  • The OP wrote a vague question. If he/she doesn't take the effort to clear doubts, why do you keep guessing?

  • Robert (4/21/2008)


    The OP wrote a vague question. If he/she doesn't take the effort to clear doubts, why do you keep guessing?

    I'm guessing that wasn't the right side of bed that you climbed out of this morning! 😛

    --------
    [font="Tahoma"]I love deadlines. I like the whooshing sound they make as they fly by. -Douglas Adams[/font]

  • Robert (4/21/2008)


    The OP wrote a vague question. If he/she doesn't take the effort to clear doubts, why do you keep guessing?

    Hi Robert

    There are a few reasons. Some people find it difficult to describe what they want, and giving them something can help draw it out of them. Also, the suggestion might be useful for others who stumble on this thread.

    Ryan Randall

    Solutions are easy. Understanding the problem, now, that's the hard part.

  • Hi Robert,

    Sorry! My question is want to check the column names for a single table.

    I got the answer for my question.

    The answer is sp_columns 'table name'

    Thanks for your advice.

  • You can also use this:

    select

    name

    from sys.columns

    where object_id =object_id('my_table')

    and name ='my_column'

  • Its fun to see all the different methods about retrieving the information.

    😀

  • Or this

    SELECT c.[NAME]

    FROM sysColumns c

    INNER JOIN sysObjects o

    ON c.[ID] =o.[ID]

    WHERE o.[NAME] = 'tblName'

  • or this

    exec sp_help tablename

    ......next

    ---------------------------------------------------------------------

  • Since we're discussing the various methods, here's a related excerpt from BOL:

    The core of the SQL Server 2005 system catalogs is a set of views that show metadata that describes the objects in an instance of SQL Server. Metadata is data that describes the attributes of objects in a system. SQL Server-based applications can access the information in the system catalogs by using the following:

    * Catalog views. We recommended this access method.

    * Information schema views.

    * OLE DB schema rowsets.

    * ODBC catalog functions.

    * System stored procedures and functions.

    We recommend using catalog views to access metadata for the following reasons:

    * All metadata is made available as catalog views.

    * Catalog views present metadata in a format that is independent of any catalog table implementation, therefore catalog views are not affected by changes in the underlying catalog tables.

    * Catalog views are the most efficient way to access core server metadata.

    * Catalog views are the general interface to catalog metadata and provide the most direct way to obtain, transform, and present customized forms of this metadata.

    * Catalog view names and the names of their columns are descriptive. Query results match what might be expected by a user who has a moderate knowledge of the feature that corresponds to the metadata that is being queried.

    Ryan Randall

    Solutions are easy. Understanding the problem, now, that's the hard part.

Viewing 13 posts - 1 through 12 (of 12 total)

You must be logged in to reply to this topic. Login to reply