我有10000列空格。我想删除所有10000列名称中的所有空格。
输入:the first column name
。
输出:thefirstcolumnname
我尝试了什么:
select 'EXEC sp_rename '''+mytable+'.['+column_name+']'',''['+column_name+']'','''+replace(column_name,' ','')+''''
from information_schema.columns
where column_name like '% %'
然而,它不起作用。我怀疑我需要column_name
进行一些更改,但我不确定如何。
错误:Invalid column name 'table name'.
这意味着什么?
答案 0 :(得分:1)
这就是你想要的:
SELECT 'EXEC sp_rename '''+QUOTENAME(TABLE_CATALOG)+'.'+QUOTENAME(TABLE_SCHEMA)+'.'+QUOTENAME(TABLE_NAME)+'.'+QUOTENAME(column_name)+''','''+REPLACE(column_name, ' ', '')+''',''COLUMN'''
FROM information_schema.columns
WHERE column_name LIKE '%'
AND TABLE_NAME = 'your table name here';
答案 1 :(得分:1)
这是生成这些查询的另一种方法。复制(并查看)并运行第三列的内容。
SELECT
ta.name, co.name
,replace(replace(replace(replace('EXECUTE sp_rename ''<schemaName>.<tableName>.[<columnName>]'', ''<fixedColumnName>'', ''column'''
,'<schemaName>', schema_name(ta.schema_id))
,'<tableName>', ta.name)
,'<columnName>', co.name)
,'<fixedColumnName>', replace(co.Name, ' ', ''))
from sys.tables ta
inner join sys.columns co
on co.object_id = ta.object_id
where co.name like '% %'
order by
ta.name, co.name
答案 2 :(得分:1)
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 |
+------------+---------------+