UNION数据类型不匹配

时间:2016-10-12 12:39:05

标签: sql ms-access

在访问数据库中运行UNION查询。我已将每个变量定义为int,因为存在数据类型不匹配错误提示。这还没有解决问题。每个变量都具有1或0的值,并且没有空值。有任何想法吗?

SELECT
CInt(qryGB.BM∞) AS [BM∞],
CInt(qryGB.PM∞) AS [PM∞],
CInt(qryGB.P∞) AS [P∞],
CInt(qryGB.[RAG_B<0]) AS [RAG_B<0],
CInt(qryGB.[RAG_P<0]) AS [RAG_P<0],
CInt(qryGB.[RAG_C<0]) AS [RAG_C<0],
CInt(qryGB.[B<0]) AS [B<0],
CInt(qryGB.[P<0]) AS [P<0],
CInt(qryGB.[C<0]) AS [C<0],
CInt(qryGB.[P-1]) AS [P-1],
CInt(qryGB.[C-1]) AS [C-1],
CInt(qryGB.P0) AS [P0],
CInt(qryGB.C0) AS [C0],
CInt(qryGB.[P+1]) AS [P+1],
CInt(qryGB.[P+2]) AS [P+2],
CInt(qryGB.[P+3]) AS [P+3]
FROM qryGB

UNION ALL SELECT
CInt(qryTMD.BM∞) AS [BM∞],
CInt(qryTMD.PM∞) AS [PM∞],
CInt(qryTMD.P∞) AS [P∞],
CInt(qryTMD.[RAG_B<0]) AS [RAG_B<0],
CInt(qryTMD.[RAG_P<0]) AS [RAG_P<0],
CInt(qryTMD.[RAG_C<0]) AS [RAG_C<0],
CInt(qryTMD.[B<0]) AS [B<0],
CInt(qryTMD.[P<0]) AS [P<0],
CInt(qryTMD.[C<0]) AS [C<0],
CInt(qryTMD.[P-1]) AS [P-1],
CInt(qryTMD.[C-1]) AS [C-1],
CInt(qryTMD.P0) AS [P0],
CInt(qryTMD.C0) AS [C0],
CInt(qryTMD.[P+1]) AS [P+1],
CInt(qryTMD.[P+2]) AS [P+2],
CInt(qryTMD.[P+3]) AS [P+3]
FROM qryTMD;

2 个答案:

答案 0 :(得分:1)

检查任何列中没有任何空值。

对于空值,访问SQL有点奇怪(注意标准SQL空值开始时很奇怪!)。例如,您不能将null转换为数据类型:

SELECT DISTINCT CINT( NULL ) AS null_cast_to_int FROM AnyPopulatedTable;

“无效使用空”错误。

因此,所有Access SQL空值的类型相同但类型是什么?:

SELECT DISTINCT TYPENAME ( NULL ) AS type_name FROM  AnyPopulatedTable;

没有错误并返回'Null'!!

答案 1 :(得分:0)

我能想到的一件事是在CInt()期间进行UNION转换可能会搞砸了。在执行UNION之前,我会尝试在子查询中进行转换。类似的东西:

SELECT
a.[BM∞],
a.[PM∞],
a.[P∞],
a.[RAG_B<0],
a.[RAG_P<0],
a.[RAG_C<0],
a.[B<0],
a.[P<0],
a.[C<0],
a.[P-1],
a.[C-1],
a.[P0],
a.[C0],
a.[P+1],
a.[P+2],
a.[P+3]
FROM
    (SELECT
    CInt(qryGB.[BM∞]) AS [BM∞],
    CInt(qryGB.[PM∞]) AS [PM∞],
    CInt(qryGB.[P∞]) AS [P∞],
    CInt(qryGB.[RAG_B<0]) AS [RAG_B<0],
    CInt(qryGB.[RAG_P<0]) AS [RAG_P<0],
    CInt(qryGB.[RAG_C<0]) AS [RAG_C<0],
    CInt(qryGB.[B<0]) AS [B<0],
    CInt(qryGB.[P<0]) AS [P<0],
    CInt(qryGB.[C<0]) AS [C<0],
    CInt(qryGB.[P-1]) AS [P-1],
    CInt(qryGB.[C-1]) AS [C-1],
    CInt(qryGB.[P0]) AS [P0],
    CInt(qryGB.[C0]) AS [C0],
    CInt(qryGB.[P+1]) AS [P+1],
    CInt(qryGB.[P+2]) AS [P+2],
    CInt(qryGB.[P+3]) AS [P+3]
    FROM qryGB) as a
UNION ALL SELECT
b.[BM∞],
b.[PM∞],
b.[P∞],
b.[RAG_B<0],
b.[RAG_P<0],
b.[RAG_C<0],
b.[B<0],
b.[P<0],
b.[C<0],
b.[P-1],
b.[C-1],
b.[P0],
b.[C0],
b.[P+1],
b.[P+2],
b.[P+3]
FROM
    (SELECT
    CInt(qryTMD.[BM∞]) AS [BM∞],
    CInt(qryTMD.[PM∞]) AS [PM∞],
    CInt(qryTMD.[P∞]) AS [P∞],
    CInt(qryTMD.[RAG_B<0]) AS [RAG_B<0],
    CInt(qryTMD.[RAG_P<0]) AS [RAG_P<0],
    CInt(qryTMD.[RAG_C<0]) AS [RAG_C<0],
    CInt(qryTMD.[B<0]) AS [B<0],
    CInt(qryTMD.[P<0]) AS [P<0],
    CInt(qryTMD.[C<0]) AS [C<0],
    CInt(qryTMD.[P-1]) AS [P-1],
    CInt(qryTMD.[C-1]) AS [C-1],
    CInt(qryTMD.[P0]) AS [P0],
    CInt(qryTMD.[C0]) AS [C0],
    CInt(qryTMD.[P+1]) AS [P+1],
    CInt(qryTMD.[P+2]) AS [P+2],
    CInt(qryTMD.[P+3]) AS [P+3]
    FROM qryTMD) as b