修剪整个数据库中的空间

时间:2015-05-26 04:33:40

标签: sql database sql-server-2012 trim

如何从特定数据库中修剪所有表格列值中的空格?

必须LTrim&数据库中所有表的行列值内的RTrim个空格。我怎样才能做到这一点?

5 个答案:

答案 0 :(得分:2)

尝试以下

String properCase (String inputVal) {
// Empty strings should be returned as-is.

if (inputVal.length() == 0) return "";

// Strings with only one character uppercased.

if (inputVal.length() == 1) return inputVal.toUpperCase();

// Otherwise uppercase first letter, lowercase the rest.

return inputVal.substring(0,1).toUpperCase()
    + inputVal.substring(1).toLowerCase();
}

答案 1 :(得分:1)

你可以写成:

sed -i '/^[ \t]*<tomcat-users>/,/^[ \t]*<\/tomcat-users>/c\<tomcat-users>\n<role rolename="admin"\/>\n<role rolename="analyst"\/>\n<user username="bpmAdmin" password="password1!" roles="admin,analyst"\/>\n<\/tomcat-users>' tomcat-users.xml

答案 2 :(得分:1)

请尝试使用以下脚本通过指定表名

来修剪空间
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

答案 3 :(得分:1)

Godd ..我差点出汗:D

这将循环遍历数据库中的所有表格,并生成&amp;执行查询以修剪其中的所有列

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个字符

答案 4 :(得分:1)

这解决了我的问题:

    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