Home > Link, Microsoft SQL Server, MSBI, Optimization, Query, Script, SQL Mentalist, SQL PraRup, SQL Query, SQL Server, Technology,, TSQL, Vishal Pawar > BI SQL # 154 : SQL Server DBA Scripts : Find columns by properties

BI SQL # 154 : SQL Server DBA Scripts : Find columns by properties

Hi Geeks,

In this article we are going to cover How to Find columns by properties.

In this post we are going to discuss following points:

  • Problem Statement of SQL Script:
  • Description of SQL Script:
  • SQL Script Output Column
  • SQL Script Code
  • SQL Script Output Screenshot
  • User Level to execute

Problem Statement of SQL Script:

Find columns by properties.

Description of SQL Script:

By this Transact-SQL script you can find all columns within a database by filtering on the column properties.

SQL Script Output Column

image

SQL Script Code

DECLARE @true VARCHAR(10)
    ,@false VARCHAR(10);

SET @false = '';-- Display value for false / not set. 
SET @true = 'X';-- Display value for true / set. 

SELECT SCH.NAME + '.' + TBL.NAME AS TableName
    ,COL.column_id AS ColumnID
    ,COL.NAME AS ColumnName
    ,STYP.NAME AS SystemType
    ,CASE 
        WHEN COL.system_type_id = COL.user_type_id
            THEN ''
        ELSE TYP.NAME
        END AS UserType
    ,CASE 
        WHEN COL.max_length = - 1
            THEN 'max'
        ELSE CONVERT(VARCHAR(20), COL.max_length)
        END AS MaxLength
    ,COL.precision AS Precision
    ,COL.scale AS Scale
    ,COL.collation_name AS CollationName
    ,DFLT.NAME AS DefaultValue
    ,CASE 
        WHEN COL.is_nullable = 0
            THEN @false
        ELSE @true
        END AS IsNullable
    ,CASE 
        WHEN COL.is_identity = 0
            THEN @false
        ELSE @true
        END AS IsIdentity
    ,CASE 
        WHEN COL.is_computed = 0
            THEN @false
        ELSE @true
        END AS IsComputed
    ,CASE 
        WHEN COL.is_xml_document = 0
            THEN @false
        ELSE @true
        END AS IsXmlDoc
    ,CASE 
        WHEN COL.is_replicated = 0
            THEN @false
        ELSE @true
        END AS IsReplicated
    ,CASE 
        WHEN COL.is_merge_published = 0
            THEN @false
        ELSE @true
        END AS IsMergeRepl
-- New Columns on 2008/2008R2; remove comment. 
--,CASE WHEN COL.is_filestream = 0 THEN @false ELSE @true END AS IsFileStream       
--,CASE WHEN COL.is_sparse = 0 THEN @false ELSE @true END AS IsSparse 
FROM sys.tables AS TBL
INNER JOIN sys.schemas AS SCH ON TBL.schema_id = SCH.schema_id
INNER JOIN sys.columns AS COL ON TBL.object_id = COL.object_id
INNER JOIN sys.types AS STYP ON COL.system_type_id = STYP.
    system_type_id
    AND COL.system_type_id = STYP.user_type_id
    AND STYP.is_user_defined = 0
INNER JOIN sys.types AS TYP ON COL.system_type_id = TYP.
    system_type_id
    AND COL.user_type_id = TYP.user_type_id
LEFT JOIN sys.objects AS DFLT ON COL.default_object_id = DFLT.
    object_id
-- Option filter to find columns by properties 
WHERE TBL.is_ms_shipped = 0 -- No system tables shipped by MS. 
    --AND NOT DFLT.name IS NULL  -- Have a default value assigned. 
    --AND STYP.name = 'uniqueidentifier'  -- Filter on specific data type. 
    --AND COL.max_length = -1  -- Lenght = max. 
    --AND COL.is_identity = 1  -- All identity columns. 
ORDER BY TableName
    ,ColumnID

SQL Script Output Screenshot

image

User Level to execute

200

    Hope you will like How to Find columns by properties.

    If you really like reading my blog and understood at least few thing then please don’t forget to subscribe my blog.

If you want daily link and analysis or interesting link go to following website which will give @ your inbox please subscribe our following link resource blog :

Link Resource Website

For More information related to BI World visit my Mentalist Blog

SQL Server Mentalist >> SQL Learning Blog

Business Intelligence Mentalist >> BI World

Infographic Mentalist >> Image worth explaining thousand Words

Microsoft Mentalist >> MVC,ASP.NET, WCF & LinQ

DBA Mentalist >>Advance SQL Server Blog

Microsoft BI Mentalist >> MS BI Development Update

Connect With me on

| FaceBook |Twitter | linkedIn| Google+ | WordPress | RSS |

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: