INSERT INTO SELECT 给出:列名或提供的值数量与 table 定义不匹配
INSERT INTO SELECT gives: Column name or number of supplied values does not match table definition
我在执行以下代码时收到此错误消息。
列名称或提供的值数量与 table 定义不匹配。
--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;
但是当我注释掉 INSERT INTO #UniqueUsageFkAndBuildingFk 行时,我返回了所需的结果集。
我迷路了……
在您的 table #UniqueUsageFkAndBuildingFk
中,您有 3 列 (Building_fk, Usage_fk, CustomersBuildingNumberKey
)。因此,在插入此 table 时,您必须为这三列
指定值
INSERT INTO #UniqueUsageFkAndBuildingFk
SELECT Building_fk, Usage_fk, 0
FROM UnknownUsageFkAndBuildingFkCTE;
或者您可以在插入内容中指定列
INSERT INTO #UniqueUsageFkAndBuildingFk (Building_fk, Usage_fk)
SELECT Building_fk, Usage_fk
FROM UnknownUsageFkAndBuildingFkCTE;
正如上面阿卜杜勒的回答,我意识到我错过了行中的 (Building_fk, Usage_fk)
INSERT INTO #UniqueUsageFkAndBuildingFk
我在执行以下代码时收到此错误消息。
列名称或提供的值数量与 table 定义不匹配。
--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;
但是当我注释掉 INSERT INTO #UniqueUsageFkAndBuildingFk 行时,我返回了所需的结果集。
我迷路了……
在您的 table #UniqueUsageFkAndBuildingFk
中,您有 3 列 (Building_fk, Usage_fk, CustomersBuildingNumberKey
)。因此,在插入此 table 时,您必须为这三列
INSERT INTO #UniqueUsageFkAndBuildingFk
SELECT Building_fk, Usage_fk, 0
FROM UnknownUsageFkAndBuildingFkCTE;
或者您可以在插入内容中指定列
INSERT INTO #UniqueUsageFkAndBuildingFk (Building_fk, Usage_fk)
SELECT Building_fk, Usage_fk
FROM UnknownUsageFkAndBuildingFkCTE;
正如上面阿卜杜勒的回答,我意识到我错过了行中的 (Building_fk, Usage_fk)
INSERT INTO #UniqueUsageFkAndBuildingFk