操作数类型冲突: <type> 与 <type> TSQL 联合选择不兼容



运行类似的sql查询

select a, b 
from ab
union all
select b, a
from ba

生成错误,如:

{"Operand type clash: int is incompatible with datern Operand type clash: ntext is incompatible with int"}
{"Operand type clash: ntext is incompatible with bitrnOperand type clash: ntext is incompatible with bit"}
{"Operand type clash: ntext is incompatible with intrnOperand type clash: ntext is incompatible with int"}

等等。。

我是tsql的新手,不知道两个select语句中的列的顺序应该相同。像这样:

select a, b 
from ab
union all
select a, b
from ba

我以为sql server会因为名称而自动关联列,但我错了。我花了几个小时来解决这个问题,并搜索上面的错误,但没有关于Union All需要按正确顺序排列列的结果。我甚至不知道将术语Union添加到我的搜索中,因为我认为它与我的问题无关。所以我添加这个是为了拯救其他人。

最新更新