0

我有以下代码

SELECT dbo.COL_V_Cost_GEMS_Detail.TNG_SYS_NR AS [EHP Code], 
       dbo.COL_TBL_VCOURSE_NEW.TNG_NA AS [Course Title],
       dbo.COL_TBL_VCOURSE_NEW.FCT_TYP_CD & dbo.COL_TBL_VCOURSE_NEW.DEP_TYP_CD AS [Course Owner], 
       dbo.COL_TBL_VCOURSE_TYP.TNG_DESC AS [TYPE OF TRAINING], 
       dbo.COL_V_Cost_GEMS_Detail.JOB_FCT_CD, 
       dbo.COL_V_Cost_GEMS_Detail.JOB_GRP_CD, 
       dbo.COL_V_Cost_GEMS_Detail.ST_COST_SUM + dbo.COL_V_Cost_GEMS_Detail.IN_COST_SUM AS [Total Cost], 
       SUM(dbo.COL_V_Cost_GEMS_Detail.STUDENTS) AS SumOfSTUDENTS, 
       SUM(dbo.COL_V_Cost_GEMS_Detail.INSTRUCTORS) AS SumOfINSTRUCTORS,
       SUM(dbo.COL_V_Cost_GEMS_Detail.ST_HOURS_SUM) AS ST_HOURS,
       SUM(dbo.COL_V_Cost_GEMS_Detail.ST_COST_SUM) AS ST_COST, 
       SUM(dbo.COL_V_Cost_GEMS_Detail.IN_HOURS_SUM) AS IN_HOURS, 
       SUM(dbo.COL_V_Cost_GEMS_Detail.IN_COST_SUM) AS IN_COST
FROM  dbo.COL_V_Cost_GEMS_Detail INNER JOIN  dbo.COL_TBL_VCOURSE_NEW 
      ON dbo.COL_V_Cost_GEMS_Detail.TNG_SYS_NR = dbo.COL_TBL_VCOURSE_NEW.TNG_SYS_NR       
      INNER JOIN dbo.COL_TBL_VCOURSE_TYP 
      ON dbo.COL_TBL_VCOURSE_NEW.TNG_MDA_TYP_CD = dbo.COL_TBL_VCOURSE_TYP.TNG_TYP
WHERE (dbo.COL_V_Cost_GEMS_Detail.RRDD NOT LIKE '%12%') 
       AND 
       (dbo.COL_V_Cost_GEMS_Detail.RRDD NOT LIKE '%13%') 
       AND 
       (dbo.COL_V_Cost_GEMS_Detail.RRDD NOT LIKE '%2706%') 
       AND 
       (dbo.COL_V_Cost_GEMS_Detail.RRDD NOT LIKE '%2707%')
       AND 
       (dbo.COL_V_Cost_GEMS_Detail.RRDD NOT LIKE '%2331%')
GROUP BY dbo.COL_V_Cost_GEMS_Detail.TNG_SYS_NR, 
         dbo.COL_TBL_VCOURSE_NEW.TNG_NA, 
         dbo.COL_TBL_VCOURSE_NEW.FCT_TYP_CD & dbo.COL_TBL_VCOURSE_NEW.DEP_TYP_CD,         
         dbo.COL_TBL_VCOURSE_TYP.TNG_DESC, 
         dbo.COL_V_Cost_GEMS_Detail.JOB_FCT_CD,
         dbo.COL_V_Cost_GEMS_Detail.JOB_GRP_CD, 
         dbo.COL_V_Cost_GEMS_Detail.ST_COST_SUM +  
         dbo.COL_V_Cost_GEMS_Detail.IN_COST_SUM

我不断收到错误

Msg 402, Level 16, State 1, Line 1
The data types nvarchar and nvarchar are incompatible in the boolean AND operator.

无论我做了什么改变,我似乎都无法摆脱这个错误。起初我以为是数据类型,但它们都很好,我以为是一些符号,但替换那些也不起作用。

4

3 回答 3

1

看来你有一个 & 尝试改变它!

于 2013-05-23T13:15:05.457 回答
1

我删除了 &。如果这是否有效,请尝试此操作。

SELECT dbo.COL_V_Cost_GEMS_Detail.TNG_SYS_NR AS [EHP Code], dbo.COL_TBL_VCOURSE_NEW.TNG_NA AS [Course Title], 
           dbo.COL_TBL_VCOURSE_NEW.FCT_TYP_CD, dbo.COL_TBL_VCOURSE_NEW.DEP_TYP_CD AS [Course Owner], 
           dbo.COL_TBL_VCOURSE_TYP.TNG_DESC AS [TYPE OF TRAINING], dbo.COL_V_Cost_GEMS_Detail.JOB_FCT_CD, dbo.COL_V_Cost_GEMS_Detail.JOB_GRP_CD, 
           dbo.COL_V_Cost_GEMS_Detail.ST_COST_SUM + dbo.COL_V_Cost_GEMS_Detail.IN_COST_SUM AS [Total Cost], SUM(dbo.COL_V_Cost_GEMS_Detail.STUDENTS) 
           AS SumOfSTUDENTS, SUM(dbo.COL_V_Cost_GEMS_Detail.INSTRUCTORS) AS SumOfINSTRUCTORS, SUM(dbo.COL_V_Cost_GEMS_Detail.ST_HOURS_SUM) 
           AS ST_HOURS, SUM(dbo.COL_V_Cost_GEMS_Detail.ST_COST_SUM) AS ST_COST, SUM(dbo.COL_V_Cost_GEMS_Detail.IN_HOURS_SUM) AS IN_HOURS, 
           SUM(dbo.COL_V_Cost_GEMS_Detail.IN_COST_SUM) AS IN_COST
FROM  dbo.COL_V_Cost_GEMS_Detail INNER JOIN
           dbo.COL_TBL_VCOURSE_NEW ON dbo.COL_V_Cost_GEMS_Detail.TNG_SYS_NR = dbo.COL_TBL_VCOURSE_NEW.TNG_SYS_NR INNER JOIN
           dbo.COL_TBL_VCOURSE_TYP ON dbo.COL_TBL_VCOURSE_NEW.TNG_MDA_TYP_CD = dbo.COL_TBL_VCOURSE_TYP.TNG_TYP
WHERE (dbo.COL_V_Cost_GEMS_Detail.RRDD NOT LIKE '%12%') AND (dbo.COL_V_Cost_GEMS_Detail.RRDD NOT LIKE '%13%') AND 
           (dbo.COL_V_Cost_GEMS_Detail.RRDD NOT LIKE '%2706%') AND (dbo.COL_V_Cost_GEMS_Detail.RRDD NOT LIKE '%2707%') AND 
           (dbo.COL_V_Cost_GEMS_Detail.RRDD NOT LIKE '%2331%')
GROUP BY dbo.COL_V_Cost_GEMS_Detail.TNG_SYS_NR, dbo.COL_TBL_VCOURSE_NEW.TNG_NA, 
           dbo.COL_TBL_VCOURSE_NEW.FCT_TYP_CD, dbo.COL_TBL_VCOURSE_NEW.DEP_TYP_CD, dbo.COL_TBL_VCOURSE_TYP.TNG_DESC, 
           dbo.COL_V_Cost_GEMS_Detail.JOB_FCT_CD, dbo.COL_V_Cost_GEMS_Detail.JOB_GRP_CD, 
           dbo.COL_V_Cost_GEMS_Detail.ST_COST_SUM + dbo.COL_V_Cost_GEMS_Detail.IN_COST_SUM

或者你可能想要总和:

SELECT dbo.COL_V_Cost_GEMS_Detail.TNG_SYS_NR AS [EHP Code], dbo.COL_TBL_VCOURSE_NEW.TNG_NA AS [Course Title], 
               dbo.COL_TBL_VCOURSE_NEW.FCT_TYP_CD + dbo.COL_TBL_VCOURSE_NEW.DEP_TYP_CD AS [Course Owner], 
               dbo.COL_TBL_VCOURSE_TYP.TNG_DESC AS [TYPE OF TRAINING], dbo.COL_V_Cost_GEMS_Detail.JOB_FCT_CD, dbo.COL_V_Cost_GEMS_Detail.JOB_GRP_CD, 
               dbo.COL_V_Cost_GEMS_Detail.ST_COST_SUM + dbo.COL_V_Cost_GEMS_Detail.IN_COST_SUM AS [Total Cost], SUM(dbo.COL_V_Cost_GEMS_Detail.STUDENTS) 
               AS SumOfSTUDENTS, SUM(dbo.COL_V_Cost_GEMS_Detail.INSTRUCTORS) AS SumOfINSTRUCTORS, SUM(dbo.COL_V_Cost_GEMS_Detail.ST_HOURS_SUM) 
               AS ST_HOURS, SUM(dbo.COL_V_Cost_GEMS_Detail.ST_COST_SUM) AS ST_COST, SUM(dbo.COL_V_Cost_GEMS_Detail.IN_HOURS_SUM) AS IN_HOURS, 
               SUM(dbo.COL_V_Cost_GEMS_Detail.IN_COST_SUM) AS IN_COST
    FROM  dbo.COL_V_Cost_GEMS_Detail INNER JOIN
               dbo.COL_TBL_VCOURSE_NEW ON dbo.COL_V_Cost_GEMS_Detail.TNG_SYS_NR = dbo.COL_TBL_VCOURSE_NEW.TNG_SYS_NR INNER JOIN
               dbo.COL_TBL_VCOURSE_TYP ON dbo.COL_TBL_VCOURSE_NEW.TNG_MDA_TYP_CD = dbo.COL_TBL_VCOURSE_TYP.TNG_TYP
    WHERE (dbo.COL_V_Cost_GEMS_Detail.RRDD NOT LIKE '%12%') AND (dbo.COL_V_Cost_GEMS_Detail.RRDD NOT LIKE '%13%') AND 
               (dbo.COL_V_Cost_GEMS_Detail.RRDD NOT LIKE '%2706%') AND (dbo.COL_V_Cost_GEMS_Detail.RRDD NOT LIKE '%2707%') AND 
               (dbo.COL_V_Cost_GEMS_Detail.RRDD NOT LIKE '%2331%')
    GROUP BY dbo.COL_V_Cost_GEMS_Detail.TNG_SYS_NR, dbo.COL_TBL_VCOURSE_NEW.TNG_NA, 
               dbo.COL_TBL_VCOURSE_NEW.FCT_TYP_CD + dbo.COL_TBL_VCOURSE_NEW.DEP_TYP_CD, dbo.COL_TBL_VCOURSE_TYP.TNG_DESC, 
               dbo.COL_V_Cost_GEMS_Detail.JOB_FCT_CD, dbo.COL_V_Cost_GEMS_Detail.JOB_GRP_CD, 
               dbo.COL_V_Cost_GEMS_Detail.ST_COST_SUM + dbo.COL_V_Cost_GEMS_Detail.IN_COST_SUM
于 2013-05-23T13:17:26.603 回答
1

您正在使用& 按位运算符来连接字符串。字符串连接在 SQL-Server 中使用+(或者你可以使用 SQL-Server 2012 你可以使用CONCAT函数)

改变这个:

dbo.COL_TBL_VCOURSE_NEW.FCT_TYP_CD & dbo.COL_TBL_VCOURSE_NEW.DEP_TYP_CD, 

dbo.COL_TBL_VCOURSE_NEW.FCT_TYP_CD + dbo.COL_TBL_VCOURSE_NEW.DEP_TYP_CD

在 select 语句和 group by 子句中。

于 2013-05-23T13:17:42.730 回答