Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Delete column from multiple (hundreds) of tables Expand / Collapse
Author
Message
Posted Wednesday, February 20, 2013 8:16 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: 2 days ago @ 9:32 AM
Points: 299, Visits: 1,195
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

use sample
declare @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'


Post #1422110
Posted Wednesday, February 20, 2013 8:45 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 10:17 AM
Points: 5,472, Visits: 23,533
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.


If everything seems to be going well, you have obviously overlooked something.

Ron

Please help us, help you -before posting a question please read

Before posting a performance problem please read
Post #1422126
Posted Wednesday, February 20, 2013 9:19 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: 2 days ago @ 9:32 AM
Points: 299, Visits: 1,195
bitbucket-25253 (2/20/2013)
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.


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?

exec sp_msforeachtable
'alter table ''?'' drop column Active'

I'm reading up on the @commandorder parameters now.

Post #1422147
Posted Wednesday, February 20, 2013 10:17 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Saturday, April 12, 2014 11:40 AM
Points: 2,795, Visits: 8,297
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.

use MyDatabase
set nocount on
select '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 <> 'DLTest' and schema_id = 1) o
order by o.name




Post #1422189
Posted Friday, February 22, 2013 12:58 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 2:24 AM
Points: 3,591, Visits: 5,100
You can also EXEC the @SQL returned by this:

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





My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1422898
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse