简体   繁体   中英

Sql Command Union not working for Colum Type Images

Sql Union Command giing this error "Msg 421, Level 16, State 1, Line 1 The image data type cannot be selected as DISTINCT because it is not comparable." when i am trying this query

Select Img from test_table Where userid = 3 
Union
Select TOP 12 Img from MultiImages

Please tell me how to overCome with this problem

You have to convert it to nvarchar first. You have not mentioned what version of SQL you are using but for SQL Server 2005 and SQL Server 2008 (and later) you can use this:

SELECT CAST([Img] AS NVARCHAR(MAX))
FROM test_table 
WHERE userid = 3 
UNION 
SELECT TOP 12 CAST([Img] AS NVARCHAR(MAX)) 
FROM MultiImages

Else if you have SQL 2000 you can not use max so you have to specify how large the varchar would be eg

SELECT CAST([Img] AS NVARCHAR(4000))
    FROM test_table 
    WHERE userid = 3 
    UNION 
    SELECT TOP 12 CAST([Img] AS NVARCHAR(4000)) 
    FROM MultiImages

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM