这是我到目前为止所做的:
SELECT
account, ' ', message_type,
MAX(message_no) as max_message_no, message_text
FROM
(SELECT
account, message_type, message_no, message_text
FROM
messages m
INNER JOIN
receivables r ON m.account = r.aracct
WHERE
m.account IN (SELECT r.aracct AS account
FROM receivables r
WHERE r.balance <> 0)
AND m.message_type = 'N') t1
GROUP BY
t1.account, t1.message_type, t1.message_text
我不断收到错误消息:
text,ntext和image数据类型无法进行比较或排序,除非使用IS NULL或LIKE运算符。
我基本上想要每个帐户中的最大message_no
,并且我希望它显示message_text
,但它不会允许它,因为它是text
数据类型。
答案 0 :(得分:4)
TEXT
,NTEXT
和IMAGE
已弃用,应替换为相应的类型VARCHAR(MAX)
,NVARCHAR(MAX)
和VARBINARY(MAX)
。您可以使用
TEXT
- 列
CAST(t1.message_text AS VARCHAR(MAX))
在您的子选择中可能足以执行此操作:
SELECT account, ' ', message_type, MAX(message_no) as max_message_no, message_text
FROM
(
SELECT account, message_type, message_no, CAST(message_text AS VARCHAR(MAX)) AS message_text
FROM messages m INNER JOIN receivables r ON m.account = r.aracct
WHERE m.account IN (SELECT r.aracct AS account FROM receivables r WHERE r.balance <> 0)
AND m.message_type = 'N'
) t1
GROUP BY t1.account, t1.message_type, t1.message_text
答案 1 :(得分:0)
您无法比较文字,图片,数据类型。如果您想使用它们,请将它们更改为Varchar或Nvarchar ..
下面的一些例子
create table #t
(
yy text
)
insert into #t
select '1'
--fails
select * from #t
where yy='1'
/****
Msg 402, Level 16, State 1, Line 1
The data types text and varchar are incompatible in the equal to operator.**/
--fails
select yy from #t
group by yy
/****
Msg 306, Level 16, State 2, Line 1
The text, ntext, and image data types cannot be compared or sorted, except when using IS NULL or LIKE operator.
***/
---Workaround
--Try changing the datatypes involved to Varchar or Nvarchar
select * from #t
where cast(yy as varchar(5))=1
select cast(yy as varchar(10)) from #t
group by cast(yy as varchar(10))
答案 2 :(得分:0)
所以您查询的问题是您将其在主部分中投射为
SELECT account, ' ', message_type, MAX(message_no) as max_message_no, message_text
FROM
(
SELECT account, message_type, message_no, CAST(message_text AS VARCHAR(MAX)) AS message_text
FROM messages m INNER JOIN receivables r ON m.account = r.aracct
WHERE m.account IN (SELECT r.aracct AS account FROM receivables r WHERE r.balance <> 0)
AND m.message_type = 'N'
) t1
GROUP BY t1.account, t1.message_type, CAST(message_text AS VARCHAR)