INSERT INTO SELECT gives: Column name or number of supplied values does not match table definition

爱⌒轻易说出口 提交于 2019-12-11 08:00:39

问题


I'm getting this error message when executing the code below.

Column name or number of supplied values does not match table definition.

       --DROP TABLE #UniqueBuildingUsageFkAndBuildingFk
         CREATE TABLE #UniqueUsageFkAndBuildingFk (
         Building_fk INT NOT NULL 
         ,Usage_fk INT NOT NULL DEFAULT(0)
         );

         ;WITH UnknownUsageFkAndBuildingFkCTE AS (
        SELECT DISTINCT mm.Building_fk, 0 AS Usage_fk FROM Customers.StandardizedRecord sr
        JOIN Customers.MidasMatch mm ON mm.CustomersBuildingNumberKey = sr.CustomersBuildingNumberKey
        WHERE sr.CustomersBuildingNumberKey NOT IN (SELECT DISTINCT emm.CustomersBuildingNumberKey FROM
        [Customers].[CustomersBuildingNumberKeyCustomersUsageXRef] ebu 
        JOIN [Customers].[MidasMatch] emm ON emm.CustomersBuildingNumberKey = ebu.CustomersBuildingNumberKey WHERE emm.Building_fk > 0)
        AND mm.Building_fk > 0 
        ) 
        INSERT INTO #UniqueUsageFkAndBuildingFk 
        SELECT Building_fk, Usage_fk
        FROM UnknownUsageFkAndBuildingFkCTE;

But when I comment out the INSERT INTO #UniqueUsageFkAndBuildingFk line I'm returning the desired resultset.

I'm lost...


回答1:


In your table #UniqueUsageFkAndBuildingFk you have 3 columns (Building_fk, Usage_fk, CustomersBuildingNumberKey). So while insert into to this table you have to specify values for these three column

INSERT INTO #UniqueUsageFkAndBuildingFk 
SELECT Building_fk, Usage_fk, 0
FROM UnknownUsageFkAndBuildingFkCTE;

or you can specify the column in your insert

INSERT INTO #UniqueUsageFkAndBuildingFk (Building_fk, Usage_fk)
SELECT Building_fk, Usage_fk
FROM UnknownUsageFkAndBuildingFkCTE;



回答2:


As Abdul's answer above I realized I've missed the (Building_fk, Usage_fk) in the line INSERT INTO #UniqueUsageFkAndBuildingFk



来源:https://stackoverflow.com/questions/41733574/insert-into-select-gives-column-name-or-number-of-supplied-values-does-not-matc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!