将 varchar 转换为 smallint 时出现转换错误

Posted

技术标签:

【中文标题】将 varchar 转换为 smallint 时出现转换错误【英文标题】:Conversion error while converting varchar to smallint 【发布时间】:2016-09-08 00:50:36 【问题描述】:

主要资格:Varchar(20)

Qualification_Title:Varchar(100)

查询:

select distinct Main_Qualification,
               Main_Qualification + '  '+ Qualification_Title As Main_Qual_Title
from dbo.IM_EN_Main_Qualifcation_with_title_vw
where College_Year = @College_Year
and Qualification_Title not Like '%CANX%'
order by Main_Qualification + '  '+ Qualification_Title

当我执行它时出现错误:

将 varchar 值“20PA”转换为数据类型 smallint 时转换失败

dbo.IM_EN_Main_Qualifcation_with_title_vw:

SELECT     dbo.IM_EN_Main_Qualification_vw.College_Year, dbo.IM_EN_Main_Qualification_vw.Learner_ID, 
           dbo.IM_EN_Main_Qualification_vw.Main_Qualification_Code, 
           dbo.IM_EN_Main_Qualification_vw.IS_AS_Level_Umbrella, 
           dbo.IM_EN_Main_Qualification_vw.IS_A2_Level_Umbrella, 
           dbo.IM_EN_Main_Qualification_vw.Main_Qualification, 
           dbo.IM_LU_Qualifications_vw.Qualification_Title, 
           dbo.IM_LU_Qualifications_vw.Qualification_Mgmnt_Code_1, 
           dbo.IM_LU_Qualifications_vw.Section
,dbo.IM_LU_Qualifications_vw.Qualification_Mgmnt_Code_2
,dbo.IM_EN_Main_Qualification_vw.Code
FROM         dbo.IM_EN_Main_Qualification_vw INNER JOIN
             dbo.IM_LU_Qualifications_vw ON 
             dbo.IM_LU_Qualifications_vw.Qualification_Code = dbo.IM_EN_Main_Qualification_vw.Main_Qualification_Code AND 
             dbo.IM_LU_Qualifications_vw.Qualification_Year = dbo.IM_EN_Main_Qualification_vw.College_Year

[dbo].[IM_EN_Main_Qualification_vw](我们从这个视图得到 Main_Qualification):

SELECT  e.College_Year, 
e.Learner_ID,
a.Code AS Main_Qualification_Code,
MAX(b.IS_AS_Level_Umbrella) AS IS_AS_Level_Umbrella,
MAX(b.IS_A2_Level_Umbrella) AS IS_A2_Level_Umbrella,
-- Not took MAX belkow because there were two people doing both AS and A2 Levels. 
-- This will give them AS Level as Main Qualification.
MAX(CASE    WHEN b.IS_AS_Level_Umbrella = 'Yes' THEN 'AS Level' 
WHEN b.IS_A2_Level_Umbrella = 'Yes' THEN 'A2 Level'
ELSE a.Code END) AS Main_Qualification
,a.Code
FROM    IM_EN_ENROLMENTS_VW e
INNER JOIN ( 
-- Join to the query that gets the qual with the maximum GLH
SELECT A.COLLEGE_YEAR,
A.LEARNER_ID,
MAX(A.CODE) AS CODE,
A.GLH
FROM   (-- Get GKH of all Quals and join this to highest 
-- GLH the student has beteewn all Quals
SELECT   COLLEGE_YEAR,
LEARNER_ID,
CODE,
SUM(GLH) AS GLH
FROM     DBO.IM_EN_ENROLMENTS_VW
WHERE    (TYPE_OF_RECORD = 'Q')
AND (QUALIFICATION_MGMNT_CODE_1 NOT IN ('KS','TUT'))
AND (QUALIFICATION_MGMNT_CODE_3 NOT IN ('AD'))
AND (Code NOT LIKE ('%/N%'))
AND (Completion_Stat_Q19 <> '4')
GROUP BY COLLEGE_YEAR,LEARNER_ID,CODE) AS A
INNER JOIN (
-- Get the MAX GLH of all Quals
SELECT   COLLEGE_YEAR,
LEARNER_ID,
MAX(GLH) AS MAXGLH
FROM     (
-- Get GKH of all Quals
SELECT  COLLEGE_YEAR,
LEARNER_ID,
CODE,
SUM(GLH) AS GLH
FROM    DBO.IM_EN_ENROLMENTS_VW
WHERE    (TYPE_OF_RECORD = 'Q')
AND (QUALIFICATION_MGMNT_CODE_1 NOT IN ('KS','TUT'))
AND (QUALIFICATION_MGMNT_CODE_3 NOT IN ('AD'))
AND (Code NOT LIKE ('%/N%'))
AND (Completion_Stat_Q19 <> '4')
GROUP BY COLLEGE_YEAR,LEARNER_ID,CODE) AS V
GROUP BY COLLEGE_YEAR,LEARNER_ID) AS B
ON A.COLLEGE_YEAR = B.COLLEGE_YEAR
AND A.LEARNER_ID = B.LEARNER_ID
AND A.GLH = B.MAXGLH
AND A.COLLEGE_YEAR = B.COLLEGE_YEAR
group by  A.COLLEGE_YEAR,
A.LEARNER_ID,
A.GLH) AS a ON e.College_year = a.College_year AND e.Learner_ID = a.Learner_ID
LEFT JOIN (
-- Get AS and A2 Level Learners  
SELECT  College_Year, 
Learner_Id, 
Code,
dbo.IS_AS_Umbrella(College_Year, Code) AS IS_AS_Level_Umbrella,
dbo.IS_A2_Umbrella(College_Year, Code) AS IS_A2_Level_Umbrella
FROM    IM_EN_ENROLMENTS_VW 
WHERE   (dbo.IS_AS_Umbrella(College_Year, Code) = 'Yes' OR dbo.IS_A2_Umbrella(College_Year, Code) = 'Yes')
AND Type_Of_Record = 'Q') AS b ON e.College_Year = b.College_year
        AND e.Learner_ID = b.Learner_ID
GROUP BY 
e.College_Year, 
e.Learner_ID,
a.Code

IM_LU_Qualifications_vw(从这里获取 Qualification_Title):

SELECT  RTRIM(dbo.PRphprovisionHeader.PRPH_Code) AS Qualification_Code, 
                CASE WHEN LEFT(PRPH_Code, 5) = 'S1500' OR PRPH_Code LIKE '15%' THEN 'Yes' ELSE 'No' END AS Is_AS_level, 
                RTRIM(dbo.PRPIProvisionInstance.PRPI_Title) AS Qualification_Title,
                ------------------------------
                RTRIM(PRIL_Title) as Course_funding_Title, 
                PRPIProvisionInstance.PRPI_Status,
                ------------------------------ 
                dbo.PRILILR.PRIL_Year AS Qualification_Year, 
                RTRIM(dbo.PRPHProvisionHeader.PRPH_ML1) AS Qualification_Mgmnt_Code_1, 
                dbo.IM_LU_Departments_vw.Department, 
                RTRIM(dbo.PRPHProvisionHeader.PRPH_ML2) AS Qualification_Mgmnt_Code_2, 
                CASE WHEN RTRIM(dbo.PRPIProvisionInstance.PRPI_Available_Enr) = 0 THEN 'N' ELSE 'Y' END AS Available, 
                dbo.IM_LU_Sections_vw.Section, 
                RTRIM(dbo.PRPHProvisionHeader.PRPH_ML3) AS Qualification_Mgmnt_Code_3, 
                RTRIM(dbo.PRPIProvisionInstance.PRPI_FT_PT) AS Qualification_FT_PT, 
                ISNULL(RTRIM(dbo.PRCOCoordinators.PRCO_Tutor), '') AS Coordinator_Code, 

                ISNULL(PRFSFeesTuition.PRFS_Fee_Amount, 0) AS Tuition_Fees, 

                /********* TUI24+FC ************ */
                ISNULL(FullCost_24plus.PRFS_Fee_Amount, 0) AS FullCost_24plus_Fees,

                ---------------
                ISNULL(PRFSFeesUniform.PRFS_Fee_Amount, 0) AS Uniform_Fees,
                ISNULL(PRFSFeesKit.PRFS_Fee_Amount, 0) AS Kit,
                --------------
                ISNULL(PRFSFeesMaterials.PRFS_Fee_Amount, 0) AS Material_Fees,
                ------------------------Full Cost Material_Fees-----------------------------------------------------------
                ISNULL(FullCostMaterialsFees.PRFS_Fee_Amount, 0) AS FullCost_Material_Fees,

                /********* CRB ************ */
                ISNULL(CRB_Fees.PRFS_Fee_Amount, 0) AS CRB,
                ---------------------------------------------------------------------------------------------------------- 
                ISNULL(PRFSFeesExam.PRFS_Fee_Amount, 0) AS Exam_Fees,
                ISNULL(PRFSFeesTrainingCredit.PRFS_Fee_Amount, 0) AS Training_Credit_TF, 
                ISNULL(PRFSFeesUserFee5.PRFS_Fee_Amount, 0) AS Police_Check_Fees, 
                ISNULL(PRFSFeesOverseas.PRFS_Fee_Amount, 0) AS Overseas_Fees, 
                RTRIM(ISNULL(dbo.PRTTTutors.PRTT_Tutor, '')) AS Tutor, 
                dbo.PERSstaff.PERS_Forename AS Course_Leader_Forename, 
                dbo.PERSstaff.PERS_Surname AS Course_Leader_Surname, 
                RTRIM(dbo.PERSstaff.PERS_Department_ML) AS Tutor_Department, 
                IM_LU_Departments_vw.Department AS Tutor_Department_Description, 
                dbo.PRPIProvisionInstance.PRPI_GLH_A32 AS Qualification_GLH, 
                -------------------- Columns swapped around ------------  
                dbo.PRILILR.PRIL_Hours_per_Week AS GLH_per_week,
                dbo.PRILILR.PRIL_Annual_End_Date As Annual_End_Date,
                dbo.PRILILR.PRIL_Annual_GLH AS Annual_GLH,
                --dbo.PRPIProvisionInstance.PRPI_Hours_per_Week AS GLH_per_week,
                -------------------------------------------------------------- 
                dbo.PRPIProvisionInstance.PRPI_Length_Weeks AS Weeks_in_year, 
                dbo.PRPIProvisionInstance.PRPI_Start_Date_A27 AS Qualification_Start_Date, 
                dbo.PRPIProvisionInstance.PRPI_Exp_End_Date_A28 AS Qualification_End_Date, 
                dbo.PRILILR.PRIL_Annual_Start_Date as Annual_Start_Date,
                -------------------- Columns swapped around ------------  
                --dbo.PRILILR.PRIL_Aim_A09 AS Qual_Aim,
                dbo.PRPIProvisionInstance.PRPI_Aim AS Qual_Aim,
                dbo.PRPIProvisionInstance.PRPI_Title AS Aim_Title,
                --+++++++++++++++++++++++++++++++++++
                ------------------- Columns swapped around ------------  
                ISNULL(AV.FullLevel2Percent, 0)  AS Full_Level2_Percentage, 
                ISNULL(AV.FullLevel3Percent, 0)  AS Full_Level3_Percentage, 
                --ISNULL(dbo.LAAV_ALL_VALUES.LEVEL2_PERCENTAGE, 0) AS Full_Level2_Percentage, 
                --ISNULL(dbo.LAAV_ALL_VALUES.LEVEL3_PERCENTAGE, 0) AS Full_Level3_Percentage, 

                -------------------- Columns swapped around ------------  
                [dbo].[Core_LARS_LearningDelivery].NotionalNVQLevel AS NVQLevel, 
                [dbo].[Core_LARS_LearningDelivery].EntrySubLevel    as  Entry_SubLevel,
                [dbo].[Core_LARS_LearningDelivery].AwardOrgCode AS Awarding_Body,
        --      dbo.LAIM_AIMS.NOTIONAL_NVQ_LEVEL_CODE AS NVQLevel, 
        --      dbo.LAIM_AIMS.AWARDING_ORGANISATION AS Awarding_Body,

                --£££££££££££££££
                dbo.PRILILR.PRIL_Year_Of AS QUAL_Year_of_Qual, 
                RTRIM(PRFSFeesCostRecovery.PRFS_Nominal) AS Cost_Recovery_AC, 
                RTRIM(PRFSFeesTuition.PRFS_Nominal) AS Tuition_Fees_AC, 
                Cost_Centres.Cost_Centre, 
                /********* TUI24+FC ************ */
                FC_Cost_Centres.FC_Cost_Centre,

                RTRIM(PRFSFeesTrainingCredit.PRFS_Nominal) AS Training_Credit_AC, 
                RTRIM(PRFSFeesExam.PRFS_Nominal) AS Exam_Fees_AC, 
                RTRIM(PRFSFeesMaterials.PRFS_Nominal) AS Material_Fees_AC, 
                RTRIM(PRFSFeesRegn.PRFS_Nominal) AS Regn_Fee_AC,
                dbo.PRILILR.PRIL_Funding_A10 AS FEFC_Fund_Q10, 
                dbo.GNCDgncodes.GNCD_Description AS Funding_Description, 
                dbo.PRPIProvisionInstance.PRPI_Max AS QUAL_Max_Size, 
                NULL AS National_Rate_1, 
                dbo.PRPIProvisionInstance.PRPI_Length_Years AS No_Of_Years, 
                dbo.PRPIProvisionInstance.PRPI_Instance AS Instance, 
                dbo.PRPHProvisionHeader.PRPH_Type, 
                dbo.PRPIProvisionInstance.PRPI_Code AS Qual_Instance_Code, 
             --   dbo.PRPIProvisionInstance.PRPI_MOA AS MOA_Code, 
                dbo.PRILILR.PRIL_Workplace_Learning AS WPL,
                ------------------- Columns swapped around ------------  
        --                      dbo.ALL_ANNUAL_VALUES.SSA_TIER1_CODE AS Subject_sector_area, 
                CASE WHEN [dbo].[Core_LARS_LearningDelivery].SectorSubjectAreaTier1 > 0 
                THEN RIGHT('0' + CAST(CAST([dbo].[Core_LARS_LearningDelivery].SectorSubjectAreaTier1 AS INT) AS VARCHAR),2) END AS Subject_sector_area, 
                --£££££££££££££
                dbo.PRILILR.PRIL_Title as Marketing_Title,

                dbo.PRILILR.PRIL_Delivery_Postcode_A23,
                ------------------------------------------------
                RTRIM(dbo.PRPHProvisionHeader.PRPH_Subject) as Sub_Section
                ------------------------------------------------                     
        FROM    dbo.PRILILR LEFT OUTER JOIN
                dbo.PRPHProvisionHeader INNER JOIN
                dbo.PRPIProvisionInstance ON 
                dbo.PRPHProvisionHeader.PRPH_Code = dbo.PRPIProvisionInstance.PRPI_Code LEFT OUTER JOIN
                dbo.PRCOCoordinators ON dbo.PRPHProvisionHeader.PRPH_Code = dbo.PRCOCoordinators.PRCO_Code AND 
                dbo.PRPIProvisionInstance.PRPI_Instance = dbo.PRCOCoordinators.PRCO_Instance

                LEFT OUTER JOIN
                [dbo].[Core_LARS_LearningDelivery] ON dbo.PRPIProvisionInstance.PRPI_Aim COLLATE Latin1_General_CI_AS = [dbo].[Core_LARS_LearningDelivery].[LearnAimRef] LEFT OUTER JOIN

                (SELECT [dbo].[Core_LARS_AnnualValue].*
                 FROM   [dbo].[Core_LARS_AnnualValue] INNER JOIN
                        (SELECT [LearnAimRef], MAX([EffectiveFrom]) LatestDate
                        FROM [dbo].[Core_LARS_AnnualValue]
                        GROUP BY LearnAimRef) LatestAV ON LatestAV.LearnAimRef = [dbo].[Core_LARS_AnnualValue].[LearnAimRef] AND LatestAV.LatestDate = [dbo].[Core_LARS_AnnualValue].[EffectiveFrom]) AV ON dbo.PRPIProvisionInstance.PRPI_Aim COLLATE Latin1_General_CI_AS = AV.LearnAimRef LEFT OUTER JOIN

                dbo.PRFSFees AS PRFSFeesTuition ON dbo.PRPHProvisionHeader.PRPH_Code = PRFSFeesTuition.PRFS_Code AND 
                dbo.PRPIProvisionInstance.PRPI_Instance = PRFSFeesTuition.PRFS_Instance AND PRFSFeesTuition.PRFS_Fee_Type = 'TUICF' --'TUI'
                /********* TUI24+FC ************ */
                LEFT OUTER JOIN
                dbo.PRFSFees AS FullCost_24plus ON dbo.PRPHProvisionHeader.PRPH_Code = FullCost_24plus.PRFS_Code AND 
                dbo.PRPIProvisionInstance.PRPI_Instance = FullCost_24plus.PRFS_Instance AND (FullCost_24plus.PRFS_Fee_Type = 'TUI24+FC') 
                -----------------------
                LEFT OUTER JOIN
                dbo.PRFSFees AS PRFSFeesUniform ON dbo.PRPHProvisionHeader.PRPH_Code = PRFSFeesUniform.PRFS_Code AND 
                dbo.PRPIProvisionInstance.PRPI_Instance = PRFSFeesUniform.PRFS_Instance AND PRFSFeesUniform.PRFS_Fee_Type = 'UNIFORM'

                LEFT OUTER JOIN
                dbo.PRFSFees AS PRFSFeesKit ON dbo.PRPHProvisionHeader.PRPH_Code = PRFSFeesKit.PRFS_Code AND 
                dbo.PRPIProvisionInstance.PRPI_Instance = PRFSFeesKit.PRFS_Instance AND PRFSFeesKit.PRFS_Fee_Type = 'Kit'
                --------------------

                LEFT OUTER JOIN
                dbo.PRFSFees AS PRFSFeesCostRecovery ON dbo.PRPHProvisionHeader.PRPH_Code = PRFSFeesCostRecovery.PRFS_Code AND 
                dbo.PRPIProvisionInstance.PRPI_Instance = PRFSFeesCostRecovery.PRFS_Instance AND 
                PRFSFeesCostRecovery.PRFS_Fee_Type = 'COST REC'

                LEFT OUTER JOIN
                dbo.PRFSFees AS PRFSFeesMaterials ON dbo.PRPHProvisionHeader.PRPH_Code = PRFSFeesMaterials.PRFS_Code AND 
                dbo.PRPIProvisionInstance.PRPI_Instance = PRFSFeesMaterials.PRFS_Instance AND PRFSFeesMaterials.PRFS_Fee_Type = 'MAT' LEFT OUTER JOIN
                -----------------------------Full Cost Materials Fees---------------------------------------------------------------------------------------------------
                dbo.PRFSFees AS FullCostMaterialsFees ON dbo.PRPHProvisionHeader.PRPH_Code = FullCostMaterialsFees.PRFS_Code AND
                dbo.PRPIProvisionInstance.PRPI_Instance = FullCostMaterialsFees.PRFS_Instance AND FullCostMaterialsFees.PRFS_Fee_Type = 'MATCF' LEFT OUTER JOIN
                -----------------------------CRB Fees---------------------------------------------------------------------------------------------------
                dbo.PRFSFees AS CRB_Fees ON dbo.PRPHProvisionHeader.PRPH_Code = CRB_Fees.PRFS_Code AND
                dbo.PRPIProvisionInstance.PRPI_Instance = CRB_Fees.PRFS_Instance AND CRB_Fees.PRFS_Fee_Type = 'UCF5' LEFT OUTER JOIN
                --------------------------------------------------------------------------------------------------------------------------------------------------------
                dbo.PRFSFees AS PRFSFeesExam ON dbo.PRPHProvisionHeader.PRPH_Code = PRFSFeesExam.PRFS_Code AND 
                dbo.PRPIProvisionInstance.PRPI_Instance = PRFSFeesExam.PRFS_Instance AND PRFSFeesExam.PRFS_Fee_Type = 'EXM' LEFT OUTER JOIN
                dbo.PRFSFees AS PRFSFeesTrainingCredit ON dbo.PRPHProvisionHeader.PRPH_Code = PRFSFeesTrainingCredit.PRFS_Code AND 
                dbo.PRPIProvisionInstance.PRPI_Instance = PRFSFeesTrainingCredit.PRFS_Instance AND 
                PRFSFeesTrainingCredit.PRFS_Fee_Type = 'TRAIN CRED' LEFT OUTER JOIN
                dbo.PRFSFees AS PRFSFeesOverseas ON dbo.PRPHProvisionHeader.PRPH_Code = PRFSFeesOverseas.PRFS_Code AND 
                dbo.PRPIProvisionInstance.PRPI_Instance = PRFSFeesOverseas.PRFS_Instance AND 
                PRFSFeesOverseas.PRFS_Fee_Type = 'NONEU' LEFT OUTER JOIN
                dbo.PRFSFees AS PRFSFeesRegn ON dbo.PRPHProvisionHeader.PRPH_Code = PRFSFeesRegn.PRFS_Code AND 
                dbo.PRPIProvisionInstance.PRPI_Instance = PRFSFeesRegn.PRFS_Instance AND PRFSFeesRegn.PRFS_Fee_Type = 'REG' LEFT OUTER JOIN
                dbo.PRFSFees AS PRFSFeesUserFee5 ON dbo.PRPHProvisionHeader.PRPH_Code = PRFSFeesUserFee5.PRFS_Code AND 
                dbo.PRPIProvisionInstance.PRPI_Instance = PRFSFeesUserFee5.PRFS_Instance AND PRFSFeesUserFee5.PRFS_Fee_Type = 'UCF5' LEFT OUTER JOIN
                dbo.PRTTTutors ON dbo.PRPHProvisionHeader.PRPH_Code = dbo.PRTTTutors.PRTT_Code AND 
                dbo.PRPIProvisionInstance.PRPI_Instance = dbo.PRTTTutors.PRTT_Instance LEFT OUTER JOIN
                dbo.PERSstaff ON dbo.PRTTTutors.PRTT_Tutor = dbo.PERSstaff.PERS_Staff_Code LEFT OUTER JOIN
                dbo.IM_LU_Sections_vw ON dbo.PRPHProvisionHeader.PRPH_ML2 = dbo.IM_LU_Sections_vw.Section_Code LEFT OUTER JOIN
                dbo.IM_LU_Departments_vw ON dbo.PRPHProvisionHeader.PRPH_ML1 = dbo.IM_LU_Departments_vw.Department_Code LEFT OUTER JOIN
                dbo.IM_LU_Departments_vw AS IM_LU_Departments_vw1 ON dbo.PERSstaff.PERS_Department_ML = IM_LU_Departments_vw1.Department_Code ON 
                dbo.PRILILR.PRIL_Code = dbo.PRPHProvisionHeader.PRPH_Code AND 
                dbo.PRILILR.PRIL_Instance = dbo.PRPIProvisionInstance.PRPI_Instance LEFT OUTER JOIN
                dbo.GNCDgncodes ON dbo.GNCDgncodes.GNCD_General_Code = dbo.PRILILR.PRIL_Funding_A10 AND 
                dbo.GNCDgncodes.GNCD_Code_Type = 'CH' AND dbo.GNCDgncodes.GNCD_Level <= 98 LEFT OUTER JOIN
                (SELECT PRFS_Code AS Code, PRFS_Instance AS Instance, RTRIM(PRFS_Finance_Dept) AS Cost_Centre
                 FROM   dbo.PRFSFees
                 WHERE  (PRFS_Fee_Type = 'TUICF')--'TUI'
                 GROUP BY PRFS_Code, PRFS_Instance, PRFS_Finance_Dept) AS Cost_Centres ON 
                dbo.PRPHProvisionHeader.PRPH_Code = Cost_Centres.Code AND dbo.PRPIProvisionInstance.PRPI_Instance = Cost_Centres.Instance

                /********* TUI24+FC ************ */
                LEFT OUTER JOIN
                (SELECT PRFS_Code AS Code, PRFS_Instance AS Instance, RTRIM(PRFS_Finance_Dept) AS FC_Cost_Centre
                 FROM   dbo.PRFSFees
                 WHERE  (PRFS_Fee_Type = ('TUI24+FC'))--'TUI'
                 GROUP BY PRFS_Code, PRFS_Instance, PRFS_Finance_Dept) AS FC_Cost_Centres ON 

                dbo.PRPHProvisionHeader.PRPH_Code = FC_Cost_Centres.Code 
                AND dbo.PRPIProvisionInstance.PRPI_Instance = FC_Cost_Centres.Instance
                -------------------

        WHERE   (dbo.PRPHProvisionHeader.PRPH_Type = 'Q')

感谢任何帮助。

谢谢, 阿尔

【问题讨论】:

哪一列的类型是smallint MS - 刚刚检查过,它们都不是smallint,都是varchar IM_EN_Main_Qualifcation_with_title_vw 视图中可能存在错误。你能分享它的内容和数据类型吗? 您的视图是否可能在选择中包含 * 并且其中一个基础表有一个新列?在这种情况下,请尝试运行 alter view。最好指定所有列 @Ako - 请见上文 【参考方案1】:

您应该查看dbo.IM_EN_Main_Qualifcation_with_title_vw。你的问题是你正在做类似的事情

c1 + c2

其中 c1 和 c2 是列,c1 是 smallint 类型,c2 是文本,当创建视图时,c2 只包含数值,但最近添加了一个文本数据,其中包含 '20PA'。您需要更改视图以将 c1 转换为文本数据,使用 CAST or CONVERT。

编辑

正如 Aruna Raghuna 所指出的,事实证明这是 College_Year 列中的一个问题,至少在某种情况下,它还包含文本,而不是预期的数值。虽然我已经向操作人员展示了找到答案的方法,但坦率地说,是 Aruna 搜索了这个问题。使用的思考过程如下:

检查列类型,找出哪些是 smallint 类型,这样我们就知道到哪里寻找问题了 检查文本列并找出哪些包含错误消息中的文本 可能的最后一步是修复查询或功能以及数据,因此此类问题将来不会再次发生

【讨论】:

@ArunaRaghunam,你能测试一下你的观点吗?哪一个包含错误?你能描述一下每一列的数据类型吗?哪一列的任何值包含“20PA”? @Aruna,尝试运行从字段中选择值的查询,并检查其中是否包含“20PA”。 (如“%20PA%”)。哪个包含它? @ArunaRaghunam,我想帮助你,但似乎你没有给我我需要的信息。我要求您检查所有涉及的列。您检查了单个列而不是所有列并且没有告诉我结果,因此除非您告诉我所涉及的所有列的确切类型以及至少包含 20PA 的记录的列,否则我无法帮助您。该价值必须来自某个地方。这也可能是 20 与 PA 连接的结果。事已至此,我没有足够的信息来解决您的问题。 @ArunaRaghunam,这些是您查询的***别。但在***别上,我看不到语法错误。您需要检查添加到问题中的依赖项中使用的列。有很多专栏,这使得这个问题对于我们这些 SO 用户来说太费力了。您需要通过向我们提供我们可以考虑的相关信息来缩小问题空间。为了实现这一点,我们需要查看类型为 smallint 的列名(这是一条线索)和值包含 20PA 的列(另一条线索)。 我认为是 College_Year

以上是关于将 varchar 转换为 smallint 时出现转换错误的主要内容,如果未能解决你的问题,请参考以下文章

将 varchar 值“N”转换为数据类型 smallint 时转换失败

将smallint转换为时间

将 varchar 转换为数字数据类型时出现算术溢出错误。找不到存储过程''。?

将 varchar 转换为 int 时出现语法错误

将 varchar 值“1.33”转换为数据类型 int 时出现问题

运行存储过程时出现错误,提示无法将varchar转换为int