Home > Link, Microsoft SQL Server, MSBI, Optimization, Query, Script, SQL Mentalist, SQL PraRup, SQL Query, SQL Server, Technology,, TSQL, Vishal Pawar > BI SQL # 234 : SQL Server DBA Scripts : Get Distinct Value for each column from Table

BI SQL # 234 : SQL Server DBA Scripts : Get Distinct Value for each column from Table

Hi Folks,

In this article we are going to cover How to Get Distinct Value for each column from Table.

In this post we are going to discuss following points:

  • Problem Statement of SQL Script:
  • Description of SQL Script:
  • Input Parameter of SQL Script
  • SQL Script Code
  • User Level to execute

Problem Statement of SQL Script:

How to Get Distinct Value for each column from Table?

Description of SQL Script:

This script will give Distinct Value for each column from Table .

Input Parameter of SQL Script

1.Table Name (@TableName)

2.Schema Name(@SCHEMANAME)

SQL Script Code

CREATE PROC USPGetDistinctValueforeachColumn @TableName VARCHAR(
        40)
    ,@SCHEMANAME VARCHAR(40)
AS
BEGIN
    DECLARE @TOTAL_ROW INT

    IF OBJECT_ID('tempdb..#Temp_COUNT') IS NOT NULL
        DROP TABLE #Temp_COUNT;

    SELECT COLUMN_NAME
    INTO #TEMP_COUNT
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_NAME = @TableName
        AND TABLE_SCHEMA = @SCHEMANAME

    SELECT @TOTAL_ROW = COUNT(*)
    FROM #TEMP_COUNT

    ALTER TABLE #TEMP_COUNT ADD ID INT IDENTITY (
        1
        ,1
        )

    CREATE TABLE DEST (ID_No INT iDENTITY(1, 1))

    --SELECT @TOTAL_ROW 
    DECLARE @ROW_NUMBER INT = 1

    WHILE @ROW_NUMBER <= @TOTAL_ROW
    BEGIN
        DECLARE @COLUMN_NAME VARCHAR(30)

        SELECT @COLUMN_NAME = COLUMN_NAME
        FROM #TEMP_COUNT
        WHERE ID = @ROW_NUMBER

        EXEC (
                'ALTER TABLE DEST ADD ' + @COLUMN_NAME + 
                ' VARCHAR(MAX)'
                )

        IF @ROW_NUMBER = 1
        BEGIN
            EXEC (
                    'INSERT INTO DEST(' + @COLUMN_NAME + 
                    ')
SELECT DISTINCT ' + @COLUMN_NAME + 
                    ' FROM ' + @SCHEMANAME + '.' + @TableName
                    )
        END

        IF @ROW_NUMBER <> 1
        BEGIN
            CREATE TABLE COLL (UQID INT IDENTITY(1, 1)
                )

            EXEC (
                    'ALTER TABLE COLL ADD ' + @COLUMN_NAME + 
                    ' VARCHAR(MAX)'
                    )

            EXEC (
                    'INSERT INTO COLL(' + @COLUMN_NAME + 
                    ') SELECT DISTINCT ' + @COLUMN_NAME + 
                    ' FROM ' + @SCHEMANAME + '.' + @TableName
                    )

            DECLARE @CNT INT

            SELECT @CNT = COUNT(*)
            FROM COLL

            PRINT @CNT

            DECLARE @NUM INT = 1

            WHILE @NUM <= @CNT
            BEGIN
                EXEC (
                        'UPDATE DEST SET ' + @COLUMN_NAME + 
                        ' =  (SELECT ' + @COLUMN_NAME + 
                        ' FROM COLL WHERE UQID = ' + @NUM + 
                        ') WHERE DEST.ID_No = ' + @NUM
                        )

                SET @NUM = @NUM + 1
            END

            DROP TABLE COLL
        END

        SET @ROW_NUMBER = @ROW_NUMBER + 1
    END

    SELECT *
    FROM DEST

    DROP TABLE #TEMP_COUNT

    DROP TABLE DEST
END

User Level to execute

    300

    Hope you will like How to Get Distinct Value for each column from Table.

    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: