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


What are the DBCC Commands in SQL Server?

October 18, 2012

DBCC Commands in SQL Server are the Database Console Commands which are grouped into 4 categories:

  1. Maintenance
  2. Miscellaneous
  3. Informational
  4. Validation

1. Maintenance

  • DBCC CLEANTABLE
  • DBCC INDEXDEFRAG
  • DBCC DBREINDEX
  • DBCC SHRINKDATABASE
  • DBCC DROPCLEANBUFFERS
  • DBCC SHRINKFILE
  • DBCC FREEPROCCACHE
  • DBCC UPDATEUSAGE

2. Miscellaneous

  • DBCC dllname (FREE)
  • DBCC HELP
  • DBCC FREESESSIONCACHE
  • DBCC TRACEOFF
  • DBCC FREESYSTEMCACHE
  • DBCC TRACEON

3. Informational

  • DBCC INPUTBUFFER
  • DBCC SHOWCONTIG
  • DBCC OPENTRAN
  • DBCC SQLPERF
  • DBCC OUTPUTBUFFER
  • DBCC TRACESTATUS
  • DBCC PROCCACHE
  • DBCC USEROPTIONS
  • DBCC SHOW_STATISTICS

4.Validation

  • DBCC CHECKALLOC
  • DBCC CHECKFILEGROUP
  • DBCC CHECKCATALOG
  • DBCC CHECKIDENT
  • DBCC CHECKCONSTRAINTS
  • DBCC CHECKTABLE
  • DBCC CHECKDB

How to delete the rows with out affecting log?

September 21, 2012

To delete the rows in large tables (audit/log related in general) we use the simple delete statement with criteria. But this approach would be affecting the transaction log growth. If we delete 10 million rows from a table then the transaction log will grow nearly 10 GB or more.

To avoid this we can use the delete command with batch processing as mentioned below:

DECLARE @BatchCount INT
SET @BatchCount = 100000

WHILE @BatchCount <> 0
BEGIN
    DELETE TOP (@BatchCount)
    FROM [dbo].[TableName]
    SET @BatchCount = @@rowcount
END  

The above query batch will reduce the log file head ache and will delete the rows batch by batch.

 


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



How to Grant the Execute permission of xp_cmdshell to a user?

July 23, 2012

To Grant the Execute permission of xp_cmdshell to a user you have to execute the following command:

GRANT exec ON sys.xp_cmdshell TO [User]
GO

 



Follow

Get every new post delivered to your Inbox.

Join 113 other followers

%d bloggers like this: