0

我有两个表,其属性如下date(datetime),headline(varchar),text(text)

现在我想联合所有这两个表并按日期时间排序。执行此操作时出现错误:

工作表中只允许使用文本指针,不允许使用 text、ntext 或 image 列。查询处理器生成了一个查询计划,该计划需要工作表中的 text、ntext 或 image 列。

来回尝试后,我发现是导致错误的文本属性。但是该怎么办?我尝试转换为 VARCHAR 没有成功。两个表都在文本属性中使用文本格式。

此外,当删除 ORDER BY 时,一切正常。该怎么办?

下面是原始的 SQL 查询,但您可以只回复上面的简化。

SELECT     id, datetime, author, headline, intro, text, type, toppriority,
           secondpriority, comments, companyid, '1' source 
FROM     Table1
UNION ALL
SELECT     AutoID AS id, Dato AS datetime,
           ID COLLATE SQL_Latin1_General_CP1_CI_AS AS author, NULL AS headline, 
           NULL AS intro, Notat COLLATE SQL_Latin1_General_CP1_CI_AS AS text,
           CAST(NotatTypeID AS VARCHAR) AS type,
           NULL AS toppriority, NULL AS secondpriority, NULL AS comments,
           Selskabsnummer AS companyid, '2' source 
FROM     Table2
WHERE     (NotatTypeID = '5') OR (NotatTypeID = '6')
ORDER BY datetime DESC

提前致谢

4

2 回答 2

0

解决此问题的一种方法是将联合作为 sup 查询运行,然后对结果进行排序:

SELECT * FROM 
(
    SELECT     id, datetime, author, headline, intro, text, TYPE, toppriority,
            secondpriority, comments, companyid, '1' source 
    FROM     Table1
    UNION ALL
    SELECT     AutoID AS id, Dato AS datetime,
            ID COLLATE SQL_Latin1_General_CP1_CI_AS AS author, NULL AS headline, 
            NULL AS intro, Notat COLLATE SQL_Latin1_General_CP1_CI_AS AS text,
            CAST(NotatTypeID AS VARCHAR) AS TYPE,
            NULL AS toppriority, NULL AS secondpriority, NULL AS comments,
            Selskabsnummer AS companyid, '2' source 
    FROM     Table2
    WHERE     (NotatTypeID = '5') OR (NotatTypeID = '6')
) a
ORDER BY datetime DESC
于 2010-10-05T12:52:57.207 回答
0

将日期时间字段转换为索引中的某个文本字段怎么样?请注意,使用 'datetime' 和 'text' 作为字段/别名可能会非常混乱,并且是潜在问题的来源。

于 2010-10-05T13:00:47.890 回答