分类目录归档:Sql Server

The text, ntext, and image data types cannot be compared or sorted, except when using IS NULL or LIKE operator.

Error Message

Server: Msg 306, Level 16, State 1, Line 1
The text, ntext, and image data types cannot be compared
or sorted, except when using IS NULL or LIKE operator.

Causes

NTEXT data types are used for variable-length of Unicode data, TEXT data types are used for variable-length non-Unicode data while IMAGE data types are used for variable-length binary data.

One way of getting this error is when including a column of TEXT, NTEXT or IMAGE data type in the ORDER BY clause. To illustrate, here’s a script that will generate this error message:

CREATE TABLE [dbo].[BookSummary] (
    [BookSummaryID]     INT NOT NULL IDENTITY(1, 1),
    [BookName]          NVARCHAR(200),
    [Author]            NVARCHAR(100),
    [Summary]           NTEXT
)
SELECT * FROM [dbo].[BookSummary]
ORDER BY [Summary]
Msg 306, Level 16, State 2, Line 2
The text, ntext, and image data types cannot be compared or sorted,
except when using IS NULL or LIKE operator.

Another way of getting this error is including a column of TEXT, NTEXT or IMAGE data type as part of a GROUP BY clause, as can be seen in the following script:

SELECT [Summary], COUNT(*)
FROM [dbo].[BookSummary]
GROUP BY [Summary]
Msg 306, Level 16, State 2, Line 3
The text, ntext, and image data types cannot be compared or sorted,
except when using IS NULL or LIKE operator.

Note that ntext, text and image data types will be removed in a future version of SQL Server and usage of these data types should be avoided. When using SQL Server 2005 or later, use nvarchar(max), varchar(max) and varbinary(max), respectively, instead.

Solution / Workaround:

To work around this error, the TEXT or NEXT column needs to be converted to VARCHAR or NVARCHAR when used in either the ORDER BY clause or the GROUP BY clause of a SELECT statement.

In the first example, using SQL Server 2000, the NTEXT column can be converted to NVARCHAR(4000) in the ORDER BY clause to avoid the error and generate the result desired:

SELECT * FROM [dbo].[BookSummary]
ORDER BY CAST([Summary] AS NVARCHAR(4000))

Using SQL Server 2005 or SQL Server 2008 (or later), instead of NVARCHAR(4000), the NTEXT column can be converted to NVARCHAR(MAX):

SELECT * FROM [dbo].[BookSummary]
ORDER BY CAST([Summary] AS NVARCHAR(MAX))

As for the second example, using SQL Server 2000, the same can be done with the NTEXT column in the GROUP BY clause to avoid the error:

SELECT CAST([Summary] AS NVARCHAR(4000)) AS [Summary], COUNT(*)
FROM [dbo].[BookSummary]
GROUP BY CAST([Summary] AS NVARCHAR(4000))

Using SQL Server 2005 or SQL Server 2008 (or later), instead of NVARCHAR(4000), the NTEXT column can be converted to NVARCHAR(MAX):

SELECT CAST([Summary] AS NVARCHAR(MAX)) AS [Summary], COUNT(*)
FROM [dbo].[BookSummary]
GROUP BY CAST([Summary] AS NVARCHAR(MAX))

To totally avoid getting this error message, if using SQL Server 2005 or SQL Server 2008, it is suggested that any TEXT, NTEXT or IMAGE data types be converted to VARCHAR(MAX), NVARCHAR(MAX) and VARBINARY(MAX), respectively.

SQL Server Transact-SQL 编程

T-SQL语句用于管理SQL Server数据库引擎实例,创建和管理数据库对象,以及查询、插入、修改和删除数据。
? 变量
1、 局部变量(Local Variable)
局部变量是用户可以自定义的变量,它的作用范围是仅在程序内部,在程序中通常用来储存从表中查询到的数据或当做程序执行过程中的暂存变量。使用局部变量必须以@开头,而且必须用declare命令后才能使用。
基本语法:
声明变量

declare @变量名 变量类型 [@变量名 变量类型]

为变量赋值

set @变量名 = 变量值;
select @变量名 = 变量值;

示例:
–局部变量

declare @id char(10)--声明一个长度的变量id
declare @age int --声明一个int类型变量age
select @id = 22 --赋值操作
set @age = 55 --赋值操作
print convert(char(10), @age) + '#' + @id
select @age, @id
go

简单hello world示例

declare @name varchar(20);
declare @result varchar(200);
set @name = 'jack';
set @result = @name + ' say: hello world!';
select @result;

继续阅读SQL Server Transact-SQL 编程

Sql Server设置端口

设置SQL Server服务器:

  1. “开始” → “程序” → “Microsoft SQL Server 2005” → “配置工具” → “SQL Server Configuration Manager”(确认“SQL Server Management Studio”已关闭)

  2. “SQL Server 2005 服务”中停止服务“SQL Server (SQLEXPRESS)”(默认是启动状态)

  3. “SQL Server 2005 网络配置” → “MSSQLSERVER 的协议”,启动“TCP/IP”(默认是禁用状态),然后双击“TCP/IP”进入属性设置,在“IP 地址”里,确认“IPAll”中的“TCP 端口”为1433

  4. “SQL Server 2005 服务”中启动服务“SQL Server (MSSQLSERVER )”(默认是停止状态)

  5. 关闭“SQL Server Configuration Manager”(此时可以启动“SQL Server Management Studio”,并用帐户sa、密码123登录,SQL Server服务器设置正确的话应该能登录成功)