SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Delete column from multiple (hundreds) of tables


Delete column from multiple (hundreds) of tables

Author
Message
sqldriver
sqldriver
SSC Eights!
SSC Eights! (972 reputation)SSC Eights! (972 reputation)SSC Eights! (972 reputation)SSC Eights! (972 reputation)SSC Eights! (972 reputation)SSC Eights! (972 reputation)SSC Eights! (972 reputation)SSC Eights! (972 reputation)

Group: General Forum Members
Points: 972 Visits: 2518
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'


bitbucket-25253
bitbucket-25253
SSCertifiable
SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)

Group: General Forum Members
Points: 7809 Visits: 25280
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
sqldriver
sqldriver
SSC Eights!
SSC Eights! (972 reputation)SSC Eights! (972 reputation)SSC Eights! (972 reputation)SSC Eights! (972 reputation)SSC Eights! (972 reputation)SSC Eights! (972 reputation)SSC Eights! (972 reputation)SSC Eights! (972 reputation)

Group: General Forum Members
Points: 972 Visits: 2518
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.
homebrew01
homebrew01
SSCarpal Tunnel
SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)

Group: General Forum Members
Points: 4823 Visits: 9108
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





dwain.c
dwain.c
SSCertifiable
SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)

Group: General Forum Members
Points: 7263 Visits: 6431
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!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search