简体   繁体   English

SQL Server:如何在表的所有varchar列上执行Rtrim

[英]SQL Server: How to perform Rtrim on all varchar columns of a table

I have over 30 columns in my table (sql server 2008). 我的表中有超过30列(sql server 2008)。 Columns type are varchar(x). 列类型为varchar(x)。 I know that in every column there is two extra spaces at the end of column value. 我知道在每列中,列值末尾有两个额外的空格。 How to use rtrim function for all columns and save this modification into this existing table? 如何对所有列使用rtrim函数并将此修改保存到此现有表中?

Edit: is there a way to do it using stored procedure or cursor where I don't have to manually declare all columns? 编辑:有没有办法使用存储过程或游标,我不必手动声明所有列?

For a generic approach, you can use a script like this to generate the statement for you, for a given table (useful if you have many columns!): 对于通用方法,您可以使用这样的脚本为您生成给定表的语句(如果您有许多列,则非常有用):

DECLARE @SQL VARCHAR(MAX)
DECLARE @TableName NVARCHAR(128)
SET @TableName = 'YourTableName'

SELECT @SQL = COALESCE(@SQL + ',[', '[') + 
              COLUMN_NAME + ']=RTRIM([' + COLUMN_NAME + '])'
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = @TableName
    AND DATA_TYPE = 'varchar'

SET @SQL = 'UPDATE [' + @TableName + '] SET ' + @SQL
PRINT @SQL

That will just print the SQL statement out. 这只会打印出SQL语句。 You can either then copy + run the statement, or just EXECUTE(@SQL) . 您可以复制+运行语句,也可以只EXECUTE(@SQL) This is untested, so just try it out on a test table first :) 这是未经测试的,所以只需先在测试表上试试:)

UPDATE xxx
  SET col1 = RTRIM(col1),
      col2 = RTRIM(col2),
      col3 = RTRIM(col3),
      ...

We can have stored procedure to trim specific table under specific schema . 我们可以使用存储过程来修剪specific schema下的specific schema specific table If we have different schema names other than default dbo schema, it is better to use this SP by passing schema name and table name. 如果我们具有除默认dbo架构之外的其他架构名称,则最好通过传递架构名称和表名来使用此SP。 This performs both LTRIM and RTRIM . 这将执行LTRIMRTRIM This SP would check char , nchar , varchar , nvarchar columns. 此SP将检查charncharvarcharnvarchar列。

CREATE PROCEDURE [dbo].[TrimAllColumnsOfTable] @SchemaName Varchar(100),@TableName Varchar(100)
AS
BEGIN

DECLARE @SQL VARCHAR(MAX)
SELECT @SQL = COALESCE(@SQL + ',[', '[') +
              COLUMN_NAME + ']=LTRIM(RTRIM([' + COLUMN_NAME + ']))'
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = @SchemaName AND TABLE_NAME = @TableName AND DATA_TYPE Like '%char%'

SET @SQL = 'UPDATE [' + @SchemaName + '].[' + @TableName + '] SET ' + @SQL

EXEC (@SQL)

END

USAGE: [TrimAllColumnsOfTable] 'SchemaName','TableName' 用法: [TrimAllColumnsOfTable] 'SchemaName','TableName'

It is perfect... But remember to put also the where clause: 这是完美的...但记得也把where子句:

COLUMNPROPERTY(OBJECT_ID(TABLE_SCHEMA+'.'+TABLE_NAME),COLUMN_NAME,'IsComputed') = 0

Ohterwise you will get an error if the table has a computed column of "%char%" type! 如果表具有"%char%"类型的计算列,则会出现错误!

The accepted answer works well. 接受的答案很有效。 I ran into an issue with a temp table being named the same name. 我遇到了一个临时表被命名为同名的问题。 You can add 你可以加

and TABLE_SCHEMA = 'dbo'

And that will get rid of collision on table names. 这将消除表名称的冲突。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM