没有SELECT DISTINCT存在,但我得到:文本数据类型不能被选为DISTINCT,因为它不具有可比性

时间:2014-05-02 15:57:32

标签: sql-server

所以我的查询中没有SELECT DISTINCT,但我收到The text data type cannot be selected as DISTINCT because it is not comparable错误。同样的查询已运行多年没有错误,突然间它无法正常工作。我最喜欢将列的数据类型更改为其中一个表中的文本,但仍然...查询中没有DISTINCT,所以为什么会出错。

以下是查询:

SELECT
            s.dept_abrev + ' ' + s.crs_num + ' ' + s.section_code AS course,
            app.approval_id,
            app.role_id,
            r.role_name,
            app.section_id,
            d.delivery_method,
            app.approve,
            app.reject,
            app.notes,
            x.xl_code,
            x.xl_cap,
            s.section_cap,
            rt.role_priority,
            p.person_id,
            p.f_name + ' ' + p.l_name AS approver_name,
            p.email,
            ip.person_id AS inst_person_id, 
            ip.f_name + ' ' + ip.l_name AS instructor_name,
            'RCDE' AS who
        FROM
            cas_Approval app
            INNER JOIN cas_Sections s ON (s.section_id = app.section_id) 
                AND s.section_status_id IN (3,4)
            INNER JOIN cas_Contract AS c ON (c.section_id = app.section_id)
            INNER JOIN cas_Instructor AS i ON (i.instructor_id = c.instructor_id)
            INNER JOIN cas_Person AS ip ON (i.person_id = ip.person_id)
            INNER JOIN cas_Role r ON (app.role_id = r.role_id)
            LEFT OUTER JOIN cas_XLCode x ON (x.xl_id = s.xl_id)
            INNER JOIN cas_DeliveryMethod d ON (d.delivery_id = s.delivery_id)
            INNER JOIN cas_RoleTypes rt ON (rt.role_type_id = r.role_type_id)
            INNER JOIN cas_PersonToRole AS ptr ON r.role_id = ptr.role_id 
            INNER JOIN cas_Person AS p ON ptr.person_id = p.person_id
            INNER JOIN cas_semesters sem ON s.semester_id = sem.semester_id
                AND sem.is_template = 0
        WHERE
            app.approve IS NULL
            AND (SELECT COUNT(reject) FROM cas_Approval WHERE section_id = s.section_id OR section_id = s.parent_section_id) = 0
            AND app.reject IS NULL 
            AND s.semester_id = 27
            AND ip.person_id <> p.person_id

        UNION
        SELECT
            c.dept_abrev + ' ' + c.crs_num + ' ' + c.sectionCode AS course,
            app.approval_id,
            app.role_id,
            r.role_name,
            app.section_id,
            (CASE c.instructMethod 
                WHEN 'P' THEN 'Traditional'
                WHEN 'I' THEN 'Online'
                WHEN 'R' THEN 'Broadcast'
            END) AS delivery_method,
            app.approve,
            app.reject,
            app.notes,
            c.xlCode AS xl_code,
            NULL AS xl_cap,
            c.courseCap AS section_cap,
            rt.role_priority,
            p.person_id,
            p.f_name + ' ' + p.l_name AS approver_name,
            p.email,
            ip.person_id AS inst_person_id,
            insFirstName + ' ' + insLastName AS instructor_name,
            'USU-E' AS who
        FROM
            cas_Approval app
            INNER JOIN cas_usuECourses c ON (c.id = app.eastId) 
                AND c.status IN ('P','A','S')
            INNER JOIN cas_Role r ON (app.role_id = r.role_id)
            INNER JOIN cas_RoleTypes rt ON (rt.role_type_id = r.role_type_id)
            INNER JOIN cas_PersonToRole AS ptr ON r.role_id = ptr.role_id 
            INNER JOIN cas_Person p ON ptr.person_id = p.person_id
            INNER JOIN cas_semesters sem ON c.termCode = sem.banner_term_code
                AND sem.is_template = 0
            LEFT OUTER JOIN cas_person ip ON c.insAnumber = ip.a_num
        WHERE
            app.approve IS NULL
            AND (SELECT COUNT(reject) FROM cas_Approval WHERE eastId = c.id) = 0
            AND app.reject IS NULL 
            AND sem.semester_id = 27
        ORDER BY
            p.person_id,
            app.role_id,
            course

3 个答案:

答案 0 :(得分:4)

UNION子句导致DISTINCT。

博客文章描述您的确切问题: SQL unions and the text data type

答案 1 :(得分:1)

TEXT不能与DISTINCT(以及其他一些功能)一起使用。在任何情况下,TEXT都是to be deprecated,应该替换为VARCHAR(MAX)这样的类型,它允许不同。

CREATE Table SomeTable
(
  TextCol TEXT,
  VarCharMaxCol VARCHAR(MAX)
);
GO

--Err: The text data type cannot be selected as DISTINCT because it is not comparable.
SELECT DISTINCT TextCol FROM SomeTable;
-- Works
SELECT DISTINCT VarCharMaxCol FROM SomeTable;

答案 2 :(得分:1)

文本字段是针对无法进行比较或排序的数据而设计的。例如,图书馆永远不会放置华氏451&#39;旁边的“贝尔罐子”#39;因为一个人开始了#34;这是一个...&#34;而另一个开始&#34;这是一个q ...&#34;。

快速解决您的问题:使用UNION ALL。长期解决方法是不使用text

SELECT CAST('' AS text) UNION SELECT CAST('' AS text)&lt; - 错误

SELECT CAST('' AS text) UNION ALL SELECT CAST('' AS text)&lt; - 没有错误