Replace the Data Types of fields in all user data tables in the database in batches with char and varchar

Source: Internet
Author: User
Tags sql server management studio
Solve the problem: if the field type is char, it always occupies the specified byte length (many white spaces at the end). The varchar data type length is one Chinese character, which occupies 2 bytes, we recommend that you use nvarchar for all fields whose content is stored in Chinese. Operation Description: Open SQLServerManagementStudio, select the database for which you want to update the field type, and click Create query menu.

Solve the problem: if the field type is char, it always occupies the specified byte length (many white spaces at the end). The varchar data type length is one Chinese character, which occupies 2 bytes, we recommend that you use nvarchar for all fields whose content is stored in Chinese. Operation Description: Open SQL Server Management Studio, select the database for which you want to update the field type, and click Create query menu.

Solve the problem: if the field type is char, it always occupies the specified byte length (many white spaces at the end). The varchar data type length is one Chinese character, which occupies 2 bytes, we recommend that you use nvarchar for all fields whose content is stored in Chinese.

Operation Description: Open SQL Server Management Studio, select the database for which you want to update the field type, click the new query menu, and execute the following statement.

Code details:

-- Replace all user data tables in the database with scripts whose field data types are char and varchar to nvarchar in batches

Declare @ cllength nvarchar (8) -- defines the field length variable

Open mycursor1 -- open the cursor

-- Declare all Constraints List cursor corresponding to the specified field in the read data table

-- 2 execute the update field statement cyclically

Close mycursor1 -- close the cursor

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.