SQL帮助-此查询有什么问题?

时间:2018-10-09 19:31:58

标签: sql sql-server

我正在使用SSMS-在此查询中我做错了什么?错误消息说

  

INSERT语句的选择列表包含的项目多于   插入列表。 SELECT值的数量必须与   INSERT列。

但是我只是没有看到它。请帮忙!

INSERT INTO newtable (code, [Description], PACV21, CMSV22, CMSV23, RxV05, PACE_PYMNT, CMS_PYMNT, CMSV22_PYMNT, CMSV23_PYMNT, Rx_PYMNT, 2019 as DatasetYear)
SELECT code, [Description], PACV21, CMSV22, null, RxV05, PACE2018PYMNT, CMS2018PYMNT, null, null, Rx2018PYMNT, 2018 as DatasetYear,
     CASE WHEN PAC2018PYMNT = 'Yes' THEN 1 
          WHEN CMS2018PYMNT = 'Yes' THEN 1 
          WHEN Rx2018PYMNT = 'Yes' THEN 1 ELSE 0 END
FROM tableA
    UNION ALL
SELECT code, [Description], PACV21, CMSV22, CMSV23, RxV05, PAC2019PYMNT, null, CMSV22_2019PYMNT, CMSV23_2019PYMNT, Rx2019PYMNT, 2019 as DatasetYear,
    CASE WHEN PAC2019PYMNT = 'Yes' THEN 1
         WHEN CMSV22_2019PYMNT = 'Yes' THEN 1
         WHEN CMSV23_2019PYMNT = 'Yes' THEN 1
         WHEN Rx2019PYMNT = 'Yes' THEN 1 ELSE 0 END
FROM tableB

1 个答案:

答案 0 :(得分:0)

您的插入语句具有12个值,而您的select语句具有13个值。您试图将case语句放入哪一列?好像您缺少它的一列。