I agree with Visakh's suggestion, but if you absolutely need it:CREATE PROCEDURE [dbo].[usp_TrimColsTest]( @TName VARCHAR(500)) ASDECLARE @sql NVARCHAR(MAX)='UPDATE ' + QUOTENAME(ISNULL(PARSENAME(@Tname,2),'dbo')) + '.' + QUOTENAME(PARSENAME(@Tname,1)) + ' SET 'SELECT @sql=@sql+REPLACE('?=LTRIM(RTRIM(?)),', '?', QUOTENAME(column_name))FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME=PARSENAME(@Tname,1) AND TABLE_SCHEMA=ISNULL(PARSENAME(@Tname,2),'dbo')AND DATA_TYPE LIKE '%char' AND COLUMNPROPERTY(OBJECT_ID(@Tname),COLUMN_NAME,'IsComputed')=0SET @sql=LEFT(@sql,LEN(@sql)-1) -- trim trailing comma from SQL statementEXEC(@sql)
This updates all character columns in a table with one UPDATE statement, except text/ntext data types (which you should not be using anymore anyway).