﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2005 / T-SQL (SS2K5)  / Delete column from multiple (hundreds) of tables / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Mon, 17 Jun 2013 21:41:04 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Delete column from multiple (hundreds) of tables</title><link>http://www.sqlservercentral.com/Forums/Topic1422110-338-1.aspx</link><description>You can also EXEC the @SQL returned by this:[code="sql"]DECLARE @SQL nvarchar(max) = ''SELECT @SQL = @SQL + CHAR(10) + '     BEGIN TRY         ALTER TABLE [' + TABLE_NAME + ']        DROP COLUMN Active       END TRY      BEGIN CATCH         PRINT ''' + TABLE_NAME + '''      END CATCH;'FROM information_schema.columns WHERE COLUMN_NAME= 'Active'PRINT @SQL[/code]</description><pubDate>Fri, 22 Feb 2013 00:58:07 GMT</pubDate><dc:creator>dwain.c</dc:creator></item><item><title>RE: Delete column from multiple (hundreds) of tables</title><link>http://www.sqlservercentral.com/Forums/Topic1422110-338-1.aspx</link><description>I use this to add the same column to a group of tables. You can modify it to remove a column. I choose "Results to text", then copy-paste the results to a new window to review it and run. It has a "GO" between each line, so it should keep running if some operations fail if the column is not found.[code="sql"]use MyDatabaseset nocount onselect 'use MyDatabase' SELECT 'ALTER TABLE ' + o.name + ' ADD PROMO_ROLLOFF_DATE varchar(25) NULL   go ' FROM (select name from sys.objects where (name   like 'DLRT%')			and name &amp;lt;&amp;gt; 'DLTest' and schema_id = 1) o order by o.name[/code]</description><pubDate>Wed, 20 Feb 2013 10:17:44 GMT</pubDate><dc:creator>homebrew01</dc:creator></item><item><title>RE: Delete column from multiple (hundreds) of tables</title><link>http://www.sqlservercentral.com/Forums/Topic1422110-338-1.aspx</link><description>[quote][b]bitbucket-25253 (2/20/2013)[/b][hr]You may want to look at the procedure (in the Master DB) titled[sys].[sp_MSforeachtable].Review the T-SQL to determine if it will perform as you want it to, or if it does not prehaps a simple modification will suffice.[/quote]Oh, that's awesome. Thanks. Based on your experience, if it runs into a table that doesn't have the column I'm trying to get rid of, will it stop and throw an error, or continue the loop? [code="sql"]exec sp_msforeachtable'alter table ''?'' drop column Active'[/code]I'm reading up on the @commandorder parameters now.</description><pubDate>Wed, 20 Feb 2013 09:19:14 GMT</pubDate><dc:creator>erikd</dc:creator></item><item><title>RE: Delete column from multiple (hundreds) of tables</title><link>http://www.sqlservercentral.com/Forums/Topic1422110-338-1.aspx</link><description>You may want to look at the procedure (in the Master DB) titled[sys].[sp_MSforeachtable].Review the T-SQL to determine if it will perform as you want it to, or if it does not prehaps a simple modification will suffice.</description><pubDate>Wed, 20 Feb 2013 08:45:36 GMT</pubDate><dc:creator>bitbucket-25253</dc:creator></item><item><title>Delete column from multiple (hundreds) of tables</title><link>http://www.sqlservercentral.com/Forums/Topic1422110-338-1.aspx</link><description>Hi,I have a rather large number of tables made using a (fairly) standard template. Due to a compatibility issue with another program, I need to drop one of the columns from all tables. I'm working on the syntax for it, but my brain is not cooperating. Can anyone help me finish off the code, or point me to a better way to do it?Thanks[code="sql"]use sampledeclare @SQL = nvarchar(max)declare @tblnm = nvarchar(64)set @SQL = 'alter table ['+@tblnm+'drop column Active'select @tblnm = from information_schema.columns where column_name = 'active'[/code]</description><pubDate>Wed, 20 Feb 2013 08:16:41 GMT</pubDate><dc:creator>erikd</dc:creator></item></channel></rss>