Trim 整个数据库内的空间
Trim Spaces inside whole Database
如何在特定数据库的所有 table 列值中 trim 空格?
数据库中所有 table 的行列值内必须有 LTrim
和 RTrim
空格。我怎样才能做到这一点?
你可以这样写:
DECLARE @SQL AS VarChar(MAX)
SET @SQL = ''
SELECT @SQL = @SQL + 'UPDATE ' + IT.TABLE_SCHEMA + '.['
+ IT.TABLE_NAME + ']'
+ 'SET '+IC.COLUMN_NAME
+ ' = LTRIM(RTRIM(' + IC.COLUMN_NAME+')) ' + CHAR(13)
FROM INFORMATION_SCHEMA.TABLES IT
JOIN INFORMATION_SCHEMA.COLUMNS IC ON IT.TABLE_NAME = IC.TABLE_NAME
AND IT.TABLE_SCHEMA = IC.TABLE_SCHEMA
EXEC (@SQL)
请尝试使用以下脚本通过指定 table 名称
trim space
DECLARE @SQL VARCHAR(MAX)
DECLARE @TableName NVARCHAR(100)
SET @TableName = 'TableName'
SELECT @SQL = COALESCE(@SQL + ',[', '[') +
COLUMN_NAME + ']=RTRIM([' + COLUMN_NAME + '])'
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = @TableName
--AND DATA_TYPE = 'varchar' /*If any condition in my case its varchar*/
SET @SQL = 'UPDATE [' + @TableName + '] SET ' + @SQL
PRINT @SQL
天哪..做这件事我都快出汗了:D
这将遍历数据库中的所有表,并生成并执行查询 trim 其中的所有列
DECLARE @tablename as nvarchar(100)=''
Declare @sql varchar(max) = ''
DECLARE tempcursor CURSOR FOR
SELECT TABLE_NAME
FROM information_schema.tables
where TABLE_TYPE = 'BASE TABLE'
OPEN tempcursor
FETCH NEXT FROM tempcursor
INTO @tablename
WHILE @@FETCH_STATUS = 0
BEGIN
if exists (select c.name from sys.columns c inner join sys.tables t on c.object_id = t.object_id
where t.name = @tablename and collation_name is not null)
BEGIN
set @sql = @sql + 'update '+ @tablename + ' SET '
select @sql = @sql + c.name + '= LTRIM(RTRIM(' + c.name + ')) ,'
from sys.columns c
inner join sys.tables t on c.object_id = t.object_id
where t.name = @tablename
and collation_name is not null
and is_ansi_padded = 1
and is_computed = 0
SET @sql = LEFT(@sql, LEN(@sql) - 1)
END
FETCH NEXT FROM tempcursor
INTO @tablename
END
CLOSE tempcursor;
DEALLOCATE tempcursor;
print @sql
EXEC (@sql)
记住 SQL 查询字符串变量(即 @sql)的 8000 个字符的限制
试试下面一个
DECLARE @SQL AS VarChar(MAX)
SET @SQL = ''
SELECT @SQL = @SQL + 'UPDATE T SET T.'+IC.COLUMN_NAME +
' = LTRIM(RTRIM(' + IC.COLUMN_NAME+'))
FROM '+ IT.TABLE_SCHEMA + '.[' + IT.TABLE_NAME +
'] AS T ;' + CHAR(13)
FROM INFORMATION_SCHEMA.TABLES IT
JOIN INFORMATION_SCHEMA.COLUMNS IC ON IT.TABLE_NAME = IC.TABLE_NAME
AND IT.TABLE_SCHEMA = IC.TABLE_SCHEMA AND IT.TABLE_TYPE = 'BASE TABLE'
and IC.DATA_TYPE in( 'varchar','char','nvarchar','nchar')
Exec (@SQL)
这解决了我的问题:
DECLARE @SQL AS VarChar(MAX)
SET @SQL = ''
SELECT @SQL += 'UPDATE ' + IT.TABLE_SCHEMA + '.'
+ QUOTENAME(IT.TABLE_NAME) +
+ 'SET '+IC.COLUMN_NAME
+ ' = LTRIM(RTRIM(' + IC.COLUMN_NAME+')) ' + CHAR(13)
FROM INFORMATION_SCHEMA.TABLES IT
JOIN INFORMATION_SCHEMA.COLUMNS IC ON IT.TABLE_NAME = IC.TABLE_NAME
AND IT.TABLE_SCHEMA = IC.TABLE_SCHEMA AND IC.DATA_TYPE in ('nvarchar','char','varchar','nchar')
EXEC (@SQL)
参考: Deepshikha的回答..谢谢Deepshikha
如何在特定数据库的所有 table 列值中 trim 空格?
数据库中所有 table 的行列值内必须有 LTrim
和 RTrim
空格。我怎样才能做到这一点?
你可以这样写:
DECLARE @SQL AS VarChar(MAX)
SET @SQL = ''
SELECT @SQL = @SQL + 'UPDATE ' + IT.TABLE_SCHEMA + '.['
+ IT.TABLE_NAME + ']'
+ 'SET '+IC.COLUMN_NAME
+ ' = LTRIM(RTRIM(' + IC.COLUMN_NAME+')) ' + CHAR(13)
FROM INFORMATION_SCHEMA.TABLES IT
JOIN INFORMATION_SCHEMA.COLUMNS IC ON IT.TABLE_NAME = IC.TABLE_NAME
AND IT.TABLE_SCHEMA = IC.TABLE_SCHEMA
EXEC (@SQL)
请尝试使用以下脚本通过指定 table 名称
trim spaceDECLARE @SQL VARCHAR(MAX)
DECLARE @TableName NVARCHAR(100)
SET @TableName = 'TableName'
SELECT @SQL = COALESCE(@SQL + ',[', '[') +
COLUMN_NAME + ']=RTRIM([' + COLUMN_NAME + '])'
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = @TableName
--AND DATA_TYPE = 'varchar' /*If any condition in my case its varchar*/
SET @SQL = 'UPDATE [' + @TableName + '] SET ' + @SQL
PRINT @SQL
天哪..做这件事我都快出汗了:D
这将遍历数据库中的所有表,并生成并执行查询 trim 其中的所有列
DECLARE @tablename as nvarchar(100)=''
Declare @sql varchar(max) = ''
DECLARE tempcursor CURSOR FOR
SELECT TABLE_NAME
FROM information_schema.tables
where TABLE_TYPE = 'BASE TABLE'
OPEN tempcursor
FETCH NEXT FROM tempcursor
INTO @tablename
WHILE @@FETCH_STATUS = 0
BEGIN
if exists (select c.name from sys.columns c inner join sys.tables t on c.object_id = t.object_id
where t.name = @tablename and collation_name is not null)
BEGIN
set @sql = @sql + 'update '+ @tablename + ' SET '
select @sql = @sql + c.name + '= LTRIM(RTRIM(' + c.name + ')) ,'
from sys.columns c
inner join sys.tables t on c.object_id = t.object_id
where t.name = @tablename
and collation_name is not null
and is_ansi_padded = 1
and is_computed = 0
SET @sql = LEFT(@sql, LEN(@sql) - 1)
END
FETCH NEXT FROM tempcursor
INTO @tablename
END
CLOSE tempcursor;
DEALLOCATE tempcursor;
print @sql
EXEC (@sql)
记住 SQL 查询字符串变量(即 @sql)的 8000 个字符的限制
试试下面一个
DECLARE @SQL AS VarChar(MAX)
SET @SQL = ''
SELECT @SQL = @SQL + 'UPDATE T SET T.'+IC.COLUMN_NAME +
' = LTRIM(RTRIM(' + IC.COLUMN_NAME+'))
FROM '+ IT.TABLE_SCHEMA + '.[' + IT.TABLE_NAME +
'] AS T ;' + CHAR(13)
FROM INFORMATION_SCHEMA.TABLES IT
JOIN INFORMATION_SCHEMA.COLUMNS IC ON IT.TABLE_NAME = IC.TABLE_NAME
AND IT.TABLE_SCHEMA = IC.TABLE_SCHEMA AND IT.TABLE_TYPE = 'BASE TABLE'
and IC.DATA_TYPE in( 'varchar','char','nvarchar','nchar')
Exec (@SQL)
这解决了我的问题:
DECLARE @SQL AS VarChar(MAX)
SET @SQL = ''
SELECT @SQL += 'UPDATE ' + IT.TABLE_SCHEMA + '.'
+ QUOTENAME(IT.TABLE_NAME) +
+ 'SET '+IC.COLUMN_NAME
+ ' = LTRIM(RTRIM(' + IC.COLUMN_NAME+')) ' + CHAR(13)
FROM INFORMATION_SCHEMA.TABLES IT
JOIN INFORMATION_SCHEMA.COLUMNS IC ON IT.TABLE_NAME = IC.TABLE_NAME
AND IT.TABLE_SCHEMA = IC.TABLE_SCHEMA AND IC.DATA_TYPE in ('nvarchar','char','varchar','nchar')
EXEC (@SQL)
参考: Deepshikha的回答..谢谢Deepshikha