添加“WHERE”子句后的数据转换错误以及SQL中的筛选器中的特定数据

时间:2015-11-04 02:38:22

标签: sql-server data-conversion

目前我遇到一个数据对话错误

"Msg 8114, Level 16, State 5, Line 1 Error converting data type nvarchar to bigint." 

我知道这个错误是由于数据类型不兼容造成的。但是,只有当我的WHERE子句(过滤条件)仅仅是特定用户而不是其他人时才会发生这种情况。我想知道出了什么问题。我尝试使用CAST并将bigint转换为nvarchar,但它没有帮助。

不兼容的数据类型在“survey_result.SR_ANSWER”之间为NVARCHAR,“survey_answer.SA_ID”在第一次连接中为BIGINT。我将附加代码,成功结果(另一个最终用户数据)和错误。

非常感谢任何帮助或建议。

SELECT  
        survey_result.SR_SRHRESULTID,
        survey_header.SH_CODE AS SURVEY_CODE,
        survey_header.SH_DESCRIPTION AS SURVEY_NAME,
        survey_result_header.SRH_DATE SURVEY_DATE,
        survey_result.SR_DISTRIBUTOR AS DISTRIBUTOR_CODE,
        dist.DI_NAME DISTRIBUTOR_NAME,
        survey_result_header.SRH_RESPONDENT AS OUTLET_CODE,
        ol.OL_NAME AS OUTLET_NAME,
        survey_result.SR_SURVEYOR AS SALESPERSON_CODE,
        ui.UI_GIVENNAME AS SALESPERSON_NAME,
        survey_question.SQ_INDEX AS QUESTION_NO,
        survey_question.SQ_DESCRIPTION AS QUESTION,
        survey_answer.SA_DESCRIPTION AS RESULT
FROM SURVEYRESULT survey_result LEFT JOIN SURVEYANSWER survey_answer 
on survey_result.SR_SQID =survey_answer.SA_SQID AND survey_result.SR_ANSWER = survey_answer.SA_ID
JOIN SURVEYQUESTION survey_question on survey_question.SQ_ID = survey_result.SR_SQID
JOIN SURVEYHEADER survey_header on survey_question.SQ_SHID = survey_header.SH_ID
JOIN SURVEYRESULTHEADER survey_result_header on survey_result_header.SRH_SHID = survey_result.SR_SHID
AND survey_result_header.SRH_RESULTID = survey_result.SR_SRHRESULTID
AND survey_result_header.SRH_DISTRIBUTOR = survey_result.SR_DISTRIBUTOR
JOIN DISTRIBUTOR dist on dist.DI_DISTRICODE = survey_result.SR_DISTRIBUTOR
JOIN OUTLET ol on ol.OL_OUTLETCODE = survey_result_header.SRH_RESPONDENT
AND ol.OL_DISTRICODE = survey_result.SR_DISTRIBUTOR
JOIN USERINFO ui on ui.UI_USERNAME = survey_result.SR_SURVEYOR
WHERE survey_result_header.SRH_DATE >='2015-10-01' AND survey_result_header.SRH_DATE <='2015-10-31'
AND survey_question.SQ_TYPE IN('1','2','8')
AND survey_header.SH_CODE IN ('MONTALL','TONTALL')
AND survey_result.SR_SURVEYOR ='E08-128' /*this line cause error*/

1 个答案:

答案 0 :(得分:0)

尝试更改此内容:

survey_result.SR_ANSWER = survey_answer.SA_ID 

到这个

survey_result.SR_ANSWER = CAST(survey_answer.SA_ID AS VARCHAR(30)) 

并查看错误是否消失。

如果确实消失了你应该:

  1. 查找导致问题的数据(字段survey_answer.SA_ID中的非数字值)。您可以使用SELECT * FROM survey_answer WHERE ISNUMERIC(SA_ID) = 0找到它
  2. 了解这会对您的查询产生什么影响(即这与survey_result.SR_ANSWER中的数据永远不会匹配)
  3. 从长远来看,您需要通过以下方式解决您的表格设计问题:

    1. 更新survey_answer.SA_ID中的数据,使其 数字
    2. 将字段更改为数字数据类型(非nvarchar)
    3. 更改任何应用程序插入数据,以便您无法插入非数字数据