数据类型文本和文本在等于运算符中不兼容

时间:2018-06-14 19:01:00

标签: sql sql-server-2008 tsql casting

我有一系列表格,其中包含我无法更改的字段。第一个表有一个字段,我有列表编号(标题为list_no type int),另一个字段有一个名为criteria type text.的字段

出于某种原因,您无法与标准进行比较,因此我必须将其转换为varchar(max).这是我的代码示例。这不是最有效编写的代码,但我需要快速转换数据并且我很难。

select distinct CAST(list_no as varchar(max)) as v_lists
into #temp
from table_list 
where created_by = 'user1' 

select cast(v_lists as text) as t_text into #temp2 from #temp

select * 
from table_keys
where criteria = (select cast(t_text as text) from #temp2)

基本上,条件是一系列文本字段,其中list_no可能会也可能不会显示为字符串的一部分。我能够成功地将其转换为文本字段,但我仍然收到此错误:

Msg 402, Level 16, State 1, Line 4
The data types text and text are incompatible in the equal to operator.

我很困惑 - 为什么不这样做?我不能一次做这个,因为在第一个场景中我有大约25个列表。但我必须为大约10个用户运行此代码,而有些用户有数百个列表。所以一次编码一个是不切实际的。

请指教。

0 个答案:

没有答案