为什么进行选择计数时转换失败?

时间:2019-09-17 18:24:50

标签: sql

进行选择计数时,我无法转换为唯一标识符。

select count (rs.column)
from table1 sw
join table2 sl
on sl.column = sw.column
join table3 sps
on sps.column = sw.column
join table4 ps 
on ps.column = sps.column
join table5 ec
on ec.column = ps.column 
join db.table1 rs
on rs.column = sw.column 
where ec.column in 
( 
)

1 个答案:

答案 0 :(得分:1)

您的一个联接将UNIQUEIDENTIFIER列连接到非UNIQUEIDENTIFIER列,并且该列(非唯一标识符)包含一些无法转换为UNIQUEIDENTIFIER的数据,或者where子句尝试将一个UNIQUEIDENTIFIER列与您的in列表中的值进行比较,该值无法转换为UNIQUEIDENTIFIER

例如:

select * from
  (select newid() as a) a
  inner join (select 'x' as b) b
  on a.a = b.b

or

select * from
  (select newid() as a) a
  where
    a.a in ('x','y')
  

消息8169的16级状态2线1   从字符串转换为uniqueidentifier时转换失败。

https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=5ce0d5a3ad87b481a85a74e73495c061