gpt4 book ai didi

sql-server - INSERT INTO SELECT 给出 : Column name or number of supplied values does not match table definition

转载 作者:行者123 更新时间:2023-12-04 17:54:15 26 4
gpt4 key购买 nike

我在执行下面的代码时收到此错误消息。

列名或提供的值数量与表定义不匹配。

       --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 行时,我返回了所需的结果集。

我迷路了...

最佳答案

在您的表 #UniqueUsageFkAndBuildingFk 中,您有 3 列(Building_fk、Usage_fk、CustomersBuildingNumberKey)。因此,在插入到该表中时,您必须为这三列指定值

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;

关于sql-server - INSERT INTO SELECT 给出 : Column name or number of supplied values does not match table definition,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41733574/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com