5 replies
by brian Accepted solution
by tony hopkinson Accepted solution
But all you need is a cursor to select the tables
and the build an alter table satemnent and exec it.
Some thing like
Declare @TableName VarChar(255)
Declare @QueryString VarChar(255)
Declare TableNameCursor Cursor For Select Name From SysObjects Where Type = 'U'
Open TableNameCursor
Fetch Next From TableNameCursor Into @TableName
While @@Fetch_Status = 0
Begin
Select @QueryString = 'Alter Table ' + @TableName + 'Add Column SomeDate DateTime null'
Exec(@QueryString)
Fetch Next From TableNameCursor Into @TableName
End
Close TableNameCursor
DeAllocate TableNameCursor
Should do the job.
Note this will do all the tables in a database, doesn't check to see if it's already been done, etc.
Might want to back up the db before you start playing with it.. :p
by brian Accepted solution
by tony hopkinson Accepted solution
Management studio would be easier, you'd have to write some code to run it otherwise.
A little tip while you are working on it.
Change
Exec(@QueryString)
To
Print @QueryString
then you'll see each command it would run with exec.
oh and if you've done some of them
you acn join SysObjects to Syscolumns and find all teh tables that already have a SomeDate Column, or with an outer join and test for null, that don't...
Worth spending a bit of time twiddling with this anyway, the techniques (cursors, exec() come in real handy from time to time.
by cmiller5400 Accepted solution
This discussion is closed to new replies.
Trending Software posts
Sort by Trending