create table t1 ([col] int,[another col] int,[yet another col] int);
create table t2 ([also a col] int,[col] int);
select 'exec sp_rename ''' + QUOTENAME(table_name) + '.' + QUOTENAME(column_name) + ''',''' + replace(column_name,' ','') + ''',''column'''
from information_schema.columns
where table_catalog = 'dmarkovitz'
and column_name like '% %'
exec sp_rename '[t1].[another col]','anothercol','column'
exec sp_rename '[t1].[yet another col]','yetanothercol','column'
exec sp_rename '[t2].[also a col]','alsoacol','column'
select table_name,column_name
from information_schema.columns
where table_catalog = 'dmarkovitz'
and table_name in ('t1','t2')
+------------+---------------+
| table_name | column_name |
+------------+---------------+
| t1 | col |
+------------+---------------+
| t1 | anothercol |
+------------+---------------+
| t1 | yetanothercol |
+------------+---------------+
| t2 | alsoacol |
+------------+---------------+
| t2 | col |
+------------+---------------+
什麼不行?你的查詢產生一個字符串(或者實際上是每行有一個字符串的結果集)。你曾經執行過這個字符串嗎? –
@GordonLinoff我試過了。錯誤信息已更新。 –
如果只有100列,爲什麼不手動做每一個? –