SQL SERVER :- T-SQL to find column from Entire Database

Hi

Today there was need of finding one column from entire SQL server Database and drop it from all tables to get it done used below Queries

SELECT tab.Name AS Tablename,


SCHEMA_NAME(schema_id)
AS Schemaname,

col.name AS Columnname


FROM
sys.tables
AS tab


INNER
JOIN
sys.columns col ON tab.OBJECT_ID = col.OBJECT_ID


WHERE col.name LIKE
‘%pk_Cr_ID%’


ORDER
BY Schemaname, Tablename;

The above query has given the details of all tables which contents the specified column

After that the command is

ALTER TABLE <Tablename> DROP COLUMN <Columnname>

Has removed the column from all tables of entire database.

Thanks

Prashant Deshpande

Advertisements

Leave a comment

Filed under Uncategorized

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