SQL Server database specification--1, automation specification naming

Source: Internet
Author: User

One, nonsense:

As the size of the database becomes larger, the database tables are hundreds of thousands, and if you need to do operations on database table names and field names, a single good statement is done, but if you want to do all the table and field names for the entire library, it's a bit of a hassle. Therefore, we need to use SQL statements to do Batch table name field name modification operations.

Second, understand the partial system table:

1. Get all database names:

SELECT NAME  from   MASTER.. sysdatabases

2. Get all user table names:

SELECT NAME  from    sysobjects WHERE  = ' U '

3. Get all field names:

SELECT NAME  from    syscolumns WHERE  = object_id ('TableName')
Third, use the cursor implementation to modify all table names and field names:
DECLARE @tablename VARCHAR( -) DECLARE @columnname VARCHAR( -) DECLARECur_tableCURSOR   for    SELECTNAME fromsysobjectsWHERETYPE= 'U'            andNAME<> 'Sysdiagrams'OPENCur_tableFETCH NEXT  fromCur_table into @tablename  while @ @fetch_status = 0BEGIN    -----------------------------------------    DECLARECur_columnCURSOR       for        SELECTNAME fromsyscolumnsWHEREId= object_id(@tablename)        OPENCur_columnFETCH NEXT  fromCur_column into @columnname      while @ @fetch_status = 0    BEGIN        DECLARE @columnnamefirstchar     CHAR(1),                @tablenamefirstchar      CHAR(1),                @ch                      VARCHAR( -),                @ch1                     VARCHAR( -),                @uppertablename          VARCHAR( -)                SET @columnnamefirstchar = SUBSTRING(@columnname,1,1)        SET @tablenamefirstchar = SUBSTRING(@tablename,1,1)        IF ASCII(@columnnamefirstchar)between ASCII('A') and ASCII('Z')        BEGIN            SET @ch = @tablename + '.' + @columnname               SET @ch1 = LOWER(@columnnamefirstchar)+ SUBSTRING(@columnname,2,LEN(@columnname)-1)               EXECsp_rename@ch,                 @ch1,                 'column'        END                IF ASCII(@tablenamefirstchar)between ASCII('a') and ASCII('Z')        BEGIN            SET @uppertablename = UPPER(@tablenamefirstchar)+ SUBSTRING(@tablename,2,LEN(@tablename)-1)               EXECsp_rename@tablename,                 @uppertablename        END                FETCH NEXT  fromCur_column into @columnname    END CLOSECur_columndeallocateCur_column-----------------------------------------     FETCH NEXT  fromCur_table into @tablenameEND CLOSECur_tabledeallocateCur_table

After execution, the entire database's table name becomes capitalized, and the first letter of the field names becomes lowercase.

SQL Server database specification--1, automation specification naming

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.