开发者

Perform alter for many tables

I have man开发者_JS百科y tables, which names start with Lookup_. How can I alter column Name in every such table?


I am not quite sure what you want to do with Name, but this is how you change it to nvarchar(20).

Declare @sql as nvarchar(500)
Declare TableCursor CURSOR FAST_FORWARD FOR
SELECT 'alter table ' + table_schema + '.'+ table_name +' alter column name nvarchar(20)' FROM INFORMATION_SCHEMA.TABLES
WHERE left(table_name, 7) = 'lookup_' and table_type = 'BASE TABLE'

OPEN TableCursor
FETCH NEXT FROM TableCursor
INTO @sql
WHILE @@FETCH_STATUS = 0
BEGIN
     EXEC (@sql)
     FETCH NEXT FROM TableCursor
     INTO @sql
END
CLOSE TableCursor
DEALLOCATE TableCursor


this looks much better

exec sp_msforeachtable 
    @command1 = 'alter table ? alter column name nvarchar(20)'
    , @whereand = 'and left(o.name, 7) = ''lookup_'' and o.type = ''U'''
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜