DATABASE

SQL Server 2008 : Transact-SQL Programming - The max Specifier

5/24/2011 3:59:25 PM
In SQL Server 2000, the most data that could be stored in a varchar, nvarchar, or varbinary column was 8,000 bytes. If you needed to store a larger value in a single column, you had to use the large object (LOB) data types: text, ntext, or image. The main disadvantage of using the LOB data types is that they cannot be used in many places where varchar or varbinary data types can be used (for example, as local variables, as arguments to SQL Server string manipulation functions such as REPLACE, and in string concatenation operations).

SQL Server 2005 introduced the max specifier for varchar and varbinary data types. This specifier expands the storage capabilities of the varchar and varbinary data types to store up to 231-1 bytes of data, which is the same maximum size of text and image data types. The main difference is that these large value data types can be used just like the smaller varchar, nvarchar, and varbinary data types. The large value data types can be used in functions where LOB objects cannot (such as the REPLACE function), as data types for Transact-SQL variables, and in string concatenation operations. They can also be used in the DISTINCT, ORDER BY, and GROUP BY clauses of a SELECT statement as well as in aggregates, joins, and subqueries.

The following example shows a local variable being defined using the varchar(max) data type:

declare @maxvar varchar(max)
go

However, a similar variable cannot be defined using the text data type:

declare @textvar text
go

Msg 2739, Level 16, State 1, Line 2
The text, ntext, and image data types are invalid for local variables.declare
@maxvar varchar(max)



The remaining examples in this section make use of the following table to demonstrate the differences between a varchar(max) column and text column:

create table maxtest (maxcol varchar(max),
textcol text)
go
-- populate the columns with some sample data
insert maxtest
select replicate('1234567890', 1000), replicate('1234567890', 1000)
go

In the following example, you can see that the substring function works with both varchar(max) and text data types:

select substring (maxcol, 1, 10),
substring (textcol, 1, 10)
from maxtest
go
maxcol textcol
---------- ----------
1234567890 1234567890

However, in this example, you can see that while a varchar(max) column can be used for string concatenation, the text data type cannot:

select substring('xxx' + maxcol, 1, 10) from maxtest
go

----------
xxx1234567

select substring('xxx' + textcol, 1, 10) from maxtest
go

Msg 402, Level 16, State 1, Line 1
The data types varchar and text are incompatible in the add operator.

With the introduction of the max specifier, the large value data types are able to store data with the same maximum size as the LOB data types, but with the ability to be used just as their smaller varchar, nvarchar, and varbinary counterparts. It is recommended that the max data types be used instead of the LOB data types because the LOB data types will be deprecated in future releases of SQL Server.

Other  
  •  SQL Server 2008 : T-SQL Tips and Tricks (part 3) - Generating T-SQL Statements with T-SQL & De-Duping Data with Ranking Functions
  •  SQL Server 2008 : T-SQL Tips and Tricks (part 2) - Using CONTEXT_INFO & Working with Outer Joins
  •  SQL Server 2008 : T-SQL Tips and Tricks (part 1) - Date Calculations & Sorting Results with the GROUPING Function
  •  SQL Server 2008 : General T-SQL Performance Recommendations
  •  SQL Server 2008 : General T-SQL Coding Recommendations (part 2) - Avoid SQL Injection Attacks When Using Dynamic SQL & Comment Your T-SQL Code
  •  SQL Server 2008 : General T-SQL Coding Recommendations (part 1) - Provide Explicit Column Lists & Qualify Object Names with a Schema Name
  •  SQL Server 2008 : Advanced Stored Procedure Programming and Optimization - Using Extended Stored Procedures
  •  SQL Server 2008 : Advanced Stored Procedure Programming and Optimization - Installing and Using .NET CLR Stored Procedures
  •  SQL Server 2008 : Advanced Stored Procedure Programming and Optimization - Using Dynamic SQL in Stored Procedures
  •  SQL Server 2008 : Advanced Stored Procedure Programming and Optimization - Stored Procedure Performance
  •  
    Video
    Top 10
    SG50 Ferrari F12berlinetta : Prancing Horse for Lion City's 50th
    The latest Audi TT : New angles for TT
    Era of million-dollar luxury cars
    Game Review : Hearthstone - Blackrock Mountain
    Game Review : Battlefield Hardline
    Google Chromecast
    Keyboards for Apple iPad Air 2 (part 3) - Logitech Ultrathin Keyboard Cover for iPad Air 2
    Keyboards for Apple iPad Air 2 (part 2) - Zagg Slim Book for iPad Air 2
    Keyboards for Apple iPad Air 2 (part 1) - Belkin Qode Ultimate Pro Keyboard Case for iPad Air 2
    Michael Kors Designs Stylish Tech Products for Women
    REVIEW
    - First look: Apple Watch

    - 3 Tips for Maintaining Your Cell Phone Battery (part 1)

    - 3 Tips for Maintaining Your Cell Phone Battery (part 2)
    Popular Tags
    Video Tutorail Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Exchange Server Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe Photoshop CorelDRAW X5 CorelDraw 10 windows Phone 7 windows Phone 8 Iphone