sql server - Is it possible to Add column to multiple table simultaneously? -


i using sql server. want add single column named [datecreated] multiple tables. possible single statement add column tables in database?

i stumble upon answer joe steffaneli in suggested query in turn returns rows consisting alter table statements. query follows :

select 'alter table ' + quotename(s.name) + '.' + quotename(t.name) + ' add [datemodified] datetime'     sys.columns c         inner join sys.tables t             on c.object_id = t.object_id         inner join sys.schemas s             on t.schema_id = s.schema_id         left join sys.columns c2             on t.object_id = c2.object_id                 , c2.name = 'datemodified'     c.name = 'datecreated'         , t.type = 'u'         , c2.column_id null /* datemodified column not exist */  

is there way can execute returned rows? sorry english.

you need this. check script want before running (adds non null column default value of getdate())!

declare @dynsql nvarchar(max)  set @dynsql = ''  select @dynsql = @dynsql + ' alter table ' + quotename(schema_name(schema_id))+ '.' + quotename(name)  +  ' add [datecreated] datetime not null default getdate()'  sys.tables type='u' , object_id not in (select object_id sys.columns name='datecreated')   exec (@dynsql) 

Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -