Data in a formatted table in SQL Server

Source: Internet
Author: User
Tags sql table name
server| data in the database development, due to the arbitrary user input information, may be generated in the table string type of
Data at both ends of the existence of spaces, or the case of inconsistencies, to the future data application process caused unnecessary trouble. This is a simple use of a stored procedure to solve these problems.

/* Collation System Data * *
CREATE PROCEDURE Pro_clearupdata
As
--The first part of the collation string data to remove the space between the two ends
DECLARE @tableName varchar (50)--table name
DECLARE @columnName varchar (50)--Column name
Declare cur_find cursor FOR select So.name,sc.name
From Syscolumns SC, sysobjects so, Systypes St
where So.name <> ' dtproperties '
and St.xtype=sc.xtype
and st.name= ' varchar '
and Sc.id=so.id
and so.xtype= ' u '
--Find all user tables that contain varchar type fields
Open Cur_find
FETCH NEXT from Cur_find into @tableName, @columnName
While @ @fetch_status =0
Begin
--Remove both ends of field space
EXEC (' update ' + @tableName + ' Set ' + @columnName + ' =ltrim (RTrim (' + @columnName + '))
FETCH NEXT from Cur_find into @tableName, @columnName
End
Close Cur_find
Deallocate Cur_find
Go


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.