问题
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