你知道在sql server中有一个快速的方法(通过transact-sql)我可以修剪所有的数据库字符串字段.
没有游标.复制并粘贴输出.也适用于SQL 2000,它没有varchar(max).如果需要,可以很容易地将其扩展为在每个UPDATE的末尾添加GO行.
SELECT SQL FROM ( SELECT t.TABLE_CATALOG , t.TABLE_SCHEMA , t.TABLE_NAME , 0 SORT , 'UPDATE ' + QUOTENAME(t.TABLE_CATALOG) + '.' + QUOTENAME(t.TABLE_SCHEMA) + '.' + QUOTENAME(t.TABLE_NAME) SQL FROM INFORMATION_SCHEMA.TABLES t JOIN INFORMATION_SCHEMA.COLUMNS c ON t.TABLE_CATALOG = c.TABLE_CATALOG AND t.TABLE_SCHEMA = c.TABLE_SCHEMA AND t.TABLE_NAME = c.TABLE_NAME WHERE t.TABLE_TYPE = 'BASE TABLE' AND c.DATA_TYPE IN ('char','nchar','varchar','nvarchar') GROUP BY t.TABLE_CATALOG , t.TABLE_SCHEMA , t.TABLE_NAME UNION ALL SELECT x.TABLE_CATALOG , x.TABLE_SCHEMA , x.TABLE_NAME , CASE WHEN x.COLUMN_NAME_MIN = y.COLUMN_NAME THEN 1 ELSE 2 END SORT , CASE WHEN x.COLUMN_NAME_MIN = y.COLUMN_NAME THEN 'SET ' ELSE ' , ' END + y.SQL SQL FROM ( SELECT t.TABLE_CATALOG , t.TABLE_SCHEMA , t.TABLE_NAME , MIN(c.COLUMN_NAME) COLUMN_NAME_MIN FROM INFORMATION_SCHEMA.TABLES t JOIN INFORMATION_SCHEMA.COLUMNS c ON t.TABLE_CATALOG = c.TABLE_CATALOG AND t.TABLE_SCHEMA = c.TABLE_SCHEMA AND t.TABLE_NAME = c.TABLE_NAME WHERE t.TABLE_TYPE = 'BASE TABLE' AND c.DATA_TYPE IN ('char','nchar','varchar','nvarchar') GROUP BY t.TABLE_CATALOG , t.TABLE_SCHEMA , t.TABLE_NAME ) x JOIN ( SELECT t.TABLE_CATALOG , t.TABLE_SCHEMA , t.TABLE_NAME , c.COLUMN_NAME , QUOTENAME(c.COLUMN_NAME) + ' = LTRIM(RTRIM(' + QUOTENAME(c.COLUMN_NAME) + '))' SQL FROM INFORMATION_SCHEMA.TABLES t JOIN INFORMATION_SCHEMA.COLUMNS c ON t.TABLE_CATALOG = c.TABLE_CATALOG AND t.TABLE_SCHEMA = c.TABLE_SCHEMA AND t.TABLE_NAME = c.TABLE_NAME WHERE t.TABLE_TYPE = 'BASE TABLE' AND c.DATA_TYPE IN ('char','nchar','varchar','nvarchar') ) y ON x.TABLE_CATALOG = y.TABLE_CATALOG AND x.TABLE_SCHEMA = y.TABLE_SCHEMA AND x.TABLE_NAME = y.TABLE_NAME ) x ORDER BY x.TABLE_CATALOG , x.TABLE_SCHEMA , x.TABLE_NAME , x.SORT , x.SQL
你的问题有点模糊,但这就是你所追求的吗?
UPDATE mytable SET mycolumn= LTRIM(RTRIM(mycolumn))
这将删除'mytable'表中'mycolumn'列中所有值的前导和尾随空格.