2011-08-22 46 views

回答

3

我不太清楚你想用Name來做什麼,但是這是你如何將它改爲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 
+1

+1,但不要忘了'QUOTEDNAME()'。 –

+0

AndriyM在寫這個查詢時我在想如何檢查表名。 Thx提醒我 –

1

這看起來好多了

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