未加星标

Comparison of the VARCHAR(max) and VARCHAR(n) SQL Server Data Types

字体大小 | |
[数据库(mssql) 所属分类 数据库(mssql) | 发布者 店小二03 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏
Problem

You are developing a SQL Server application and at some point in your code you receive the error "String or binary data would be truncated". After digging into the code you discovered that this error message is caused because a table has a VARCHAR(1000) column and you need to insert more than 1000 characters. Someone on your team suggest using the VARCHAR(max) data type, but you are unsure about the implications of the this data type. Read this tip to learn what you should know about the SQL Server VARCHAR(Max) vs. VARCHAR(n) data types.

Solution

In order to decide between using VARCHAR(Max) or VARCHAR(n) we have to understand that even when both data types look the same, they have some major differences pertaining to storage, behavior and of course their intended usage.

The VARCHAR(Max) SQL Server Data Type

The VARCHAR(Max) as well as NVARCHAR(max) and VARBINARY(max) data types were first introduced in SQL Server 2005 to replace the large object (LOB) data types TEXT, NTEXT and IMAGE respectively. All of these data types can store up to 2GB of data except NVARCHAR(max) that can store 1GB of Unicode text. As you may guess, the maximum storage for these data types is the same as the ones being replaced. I suggest reading the SQL Server differences of char, nchar, varchar and nvarchar data types tip in case you need a further explanation on the differences between VARCHAR and NVARCHAR data types.

A common question amongst beginners is: if the VARCHAR(max) data type can store up to 2GB why can't we declare a column or variable of type VARCHAR(10000)? Why are we limited to either declare a VARCHAR(8000) or VARCHAR(max)? The reason behind this is that VARCHAR(n) is intended to store the data in the row page. SQL Server stores data in 8KB pages (8,192 bytes), but the page header occupies the first 96 bytes of each data page leaving 8,096 bytes for data, row overhead, and row offsets, thats why you cannot declare a value greater than VARCHAR(8000).

Advantages

One of the advantages of the VARCHAR(max) data type as a replacement of TEXT data type is that we can declare local variables to manipulate LOBs and even declare VARCHAR(max) parameters on functions and stored procedures. This is something that cannot be done with the TEXT data type. Furthermore the VARCHAR(max) data type can be used inside string functions such as REPLACE, CHARINDEX or LEFT instead of using READTEXT, WRITETEXT, and UPDATETEXT commands to manipulate LOBs.

The following list includes the functions that support the VARCHAR(max) data type:

STRING_SPLIT STRING_ESCAPE CONCAT PATINDEX CHARINDEX LTRIM and RTRIM UPPER and LOWER LEN LEFT and RIGHT REPLACE REPLICATE REVERSE SUBSTRING STUFF Updating Data on VARCHAR(max) Columns

Something that not many people know is that the UPDATE statement allows us to append and replace data in VARCHAR(max), NVARCHAR(max) and VARBINARY(max) columns without using string functions. There is an implicit WRITE function in the UPDATE statement for columns of VARCHAR(max), NVARCHAR(max) and VARBINARY(max) data type with the following syntax.

UPDATE MyTable SET
Lob_column_name.WRITE (expression,Offset,Length)

Parameter

Type

Description

expression

VARCHAR(max)

NVARCHAR(max)

VARBINARY(max)

The value that is copied to Lob_column_name. If expression is set to NULL, Length is ignored, and the value in Lob_column_name is truncated at the specified Offset.

Offset

Bigint

The starting position in the value of Lob_column_name at which expression is written. If Offset is NULL, the update operation appends expression at the end of Lob_column_name ignoring the value of Length parameter.

Length

Bigint

The length of the section in the column, starting from Offset, which is replaced by expression. If Length is NULL, the update operation removes all data from Offset to the end of the Lob_column_name value.

Limitations

There is an obvious limitation on VARCHAR(max) columns which is that these columns cannot be indexed. If you think you need to place an index on a VARCHAR(max) column I suggest that you may need to review the table design.

Sample Usage

Lets create a new database to do some testing.

USE [Employees];
GO
CREATE TABLE Sellers
(
SellerID INT IDENTITY(1, 1) PRIMARY KEY ,
Name VARCHAR(255) ,
Percentage NUMERIC(3, 2) ,
Commissions MONEY
)
GO
INSERT INTO dbo.Sellers
SELECT 'John Doe', 0.25, 0
UNION ALL
SELECT 'Linda Smith', 0.1, 0
GO

On the Sales database create a table named Transactions to store sales information.

USE [master]
GO
CREATE DATABASE [TestDB]
CONTAINMENT = NONE
ON PRIMARY
( NAME = N'TestDB', FILENAME = N'E:\mssql\TestDB.mdf' ,
SIZE = 10MB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
LOG ON
( NAME = N'TestDB_log', FILENAME = N'E:\MSSQL\TestDB_log.ldf',
SIZE = 10240KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
GO

The following script will create a test table with one VARCHAR(Max) column and an identity column, and then insert one row with 1,000,000 characters.

USE [TestDB]
GO
CREATE TABLE TestTable
(
ID INT IDENTITY(1, 1) ,
TestData VARCHAR(MAX) ,
PRIMARY KEY CLUSTERED ( ID )
)
GO
DECLARE @str VARCHAR(MAX) = 'a'

本文数据库(mssql)相关术语:熊片数据库 mssql数据库 oracle数据库 pubmed数据库 access数据库 万方数据库

主题: SQLSQL ServerRYRIMTISUUBUF2G
分页:12
转载请注明
本文标题:Comparison of the VARCHAR(max) and VARCHAR(n) SQL Server Data Types
本站链接:http://www.codesec.net/view/480456.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 数据库(mssql) | 评论(0) | 阅读(37)