Finding Column Name from all the tables

November 15, 2012

In some situation the SQL Developers/Administrators required the column name from all the tables in a database. The below query batch will help to find all such columns in the database:

Declare @SearchString nvarchar(100)

SET  @SearchString=‘Search String should be entered here’

CREATE TABLE #Results (TableName nvarchar(370), ColumnName nvarchar(3630))

SET NOCOUNT ON

DECLARE @TableName nvarchar(256), @ColumnName nvarchar(128),

 @SearchString2 nvarchar(110) 

 SET  @TableName =    

 SET @SearchString2 =

 QUOTENAME(‘%’ + @SearchString + ‘%’,””)

WHILE @TableName IS NOT NULL   

BEGIN      

  SET @ColumnName =      

  SET @TableName =  (

    SELECT MIN(QUOTENAME(TABLE_SCHEMA) + ‘.’ +

    QUOTENAME(TABLE_NAME)) FROM INFORMATION_SCHEMA.TABLES

    WHERE

    TABLE_TYPE = ‘BASE TABLE’

    AND QUOTENAME(TABLE_SCHEMA) + ‘.’ + QUOTENAME(TABLE_NAME) > @TableName

    AND OBJECTPROPERTY(

      OBJECT_ID(QUOTENAME(TABLE_SCHEMA) + ‘.’ + QUOTENAME(TABLE_NAME)),

        ‘IsMSShipped’) = 0)

  WHILE (@TableName IS NOT NULL) AND (@ColumnName IS NOT NULL)     

  BEGIN

    SET @ColumnName = (

      SELECT MIN(QUOTENAME(COLUMN_NAME))

      FROM INFORMATION_SCHEMA.COLUMNS

      WHERE TABLE_SCHEMA = PARSENAME(@TableName, 2)

        AND TABLE_NAME = PARSENAME(@TableName, 1)

      AND DATA_TYPE IN (‘char’, ‘varchar’, ‘nchar’, ‘nvarchar’)

      AND QUOTENAME(COLUMN_NAME) > @ColumnName)

      IF @ColumnName IS NOT NULL           

      BEGIN

      INSERT INTO #Results

      EXEC

      (

        ‘SELECT ”’ + @TableName + ‘.’ + @ColumnName + ”’, LEFT(‘ + @ColumnName +

          ‘, 3630) FROM ‘ + @TableName + ‘ (NOLOCK) ‘ +

        ‘ WHERE ‘ + @ColumnName + ‘ LIKE ‘ + @SearchString2

      )            

      END      

    END    

  END

  SELECT TableName, ColumnName FROM #Results


SQL Query to display the columns and data types in all the tables

August 6, 2012

SQL Query to display the columns and data types in all the tables

The below mentioned script will display the details in the order of TableName, ColPosition, ColumnName, DataType and Length:

SELECT
IT.TABLE_NAME AS ‘TableName’,
ORDINAL_POSITION AS ‘ColPosition’,
COLUMN_NAME AS ‘ColumnName’,
DATA_TYPE AS ‘DataType’,
CHARACTER_MAXIMUM_LENGTH AS ‘Length’
from INFORMATION_SCHEMA.COLUMNS as IC
inner join INFORMATION_SCHEMA.TABLES as IT on  IT.TABLE_NAME=IC.TABLE_NAME and IT.TABLE_TYPE=’BASE TABLE’
ORDER BY
TableName,
ColPosition


Query to display the size of a table/database in SQL Server

July 12, 2012

The following query will determine the size of a particular table you want to know in a database:

Use <Database Name>

sp_spaceused ‘<Table Name>’

If we use the following queries, the whole database size will be displayed:

Use <Database Name>

sp_spaceused

 


How to find the duplicate rows in a table

July 3, 2012

To find the duplicate rows in a table we can use the following SQL query:

SELECT
Column1,Column2,COUNT(PK_Column)
FROM
<Table Name>
GROUP BY
Column1,Column2
HAVING
COUNT(ColumnWithDuplicateValues) > 1

 


%d bloggers like this: