列名或提供的值的数量与更新时的表定义不匹配
Posted
技术标签:
【中文标题】列名或提供的值的数量与更新时的表定义不匹配【英文标题】:Column name or number of supplied values does not match table definition on Update 【发布时间】:2018-11-16 19:22:14 【问题描述】:执行存储过程时出现以下错误
消息 213,级别 16,状态 1,过程 dbo.SP_PostYearDataCopy,第 97 行 [批处理开始第 2 行] 列名或提供的值的数量与表定义不匹配。
我的存储过程如下所示:
ALTER PROCEDURE [dbo].[SP_PostYearDataCopy]
AS
BEGIN
-- Declare the variable to be used.
DECLARE @Year AS INT
DECLARE @clubYear AS INT
-- Initialize the variable.
SET @Year = YEAR(GETDATE()) - 1;
SET @clubYear = @Year + 1;
CREATE TABLE #PLAYERTEMP
(
PlayerID INT,
TeamID INT,
Number INT,
Year INT
)
---- (STEP 2)
---- Create Process for Archiving Team Data and Create next years teams
CREATE TABLE #TEAMTEMP
(
teamTypeID INT,
TeamName VARCHAR(50),
clubYear VARCHAR(50),
Age VARCHAR(3),
Gender VARCHAR(50),
Display BIT,
CoachID INT,
CoachAsst INT,
stateCupID INT,
Manager VARCHAR(50),
ManagerID INT,
LeagueID INT,
League2ID INT,
League3ID INT,
LeagueLink VARCHAR (250),
LeagueLink2 VARCHAR (250),
LeagueLink3 VARCHAR (250),
Callink VARCHAR (200),
GotSoccer VARCHAR (250),
Accomplishments VARCHAR (MAX),
Doc varchar(MAX),
DocName varchar (200),
DocType varchar (5)
)
INSERT INTO #TEAMTEMP
SELECT
teamTypeID, TeamName, clubYear, Age, Gender, Display,
CoachID, CoachAsst, stateCupID, Manager, ManagerID,
LeagueID, League2ID, League3ID, LeagueLink, LeagueLink2, LeagueLink3,
Callink, GotSoccer, Accomplishments, Doc, DocName, DocType
FROM
dbo.Teams
WHERE
(clubYear = @Year) AND (TeamTypeID = 3)
UPDATE #TEAMTEMP
SET clubYear = @clubYear,
Age = RIGHT(Age, 2) + 1,
CoachID = NULL,
CoachAsst = NULL,
stateCupID = NULL,
Manager = NULL,
ManagerID = NULL <--- LINE 97 --->
【问题讨论】:
信息很清楚。检查您的INSERT
声明
我这样做了 20 次,但没有发现任何遗漏。你呢?
是的。 INSERT
声明看起来不错。尝试将查询复制到新的查询窗口并尝试。您可能已经在当前会话(查询窗口)中创建了#TEAMTEMP
。您可以通过在当前查询窗口中执行SELECT * FROM #TEAMTEMP
来确认这一点
旁注:您应该不为您的存储过程使用sp_
前缀。微软有reserved that prefix for its own use (see Naming Stored Procedures),你确实会在未来某个时候冒着名称冲突的风险。 It's also bad for your stored procedure performance。最好只是简单地避免 sp_
并使用其他东西作为前缀 - 或者根本没有前缀!
【参考方案1】:
问题是我没有明确设置字段以将值插入表中。该表添加了两个我忘记的新字段,并且 ID 字段未设置为自动递增,所以我猜它也在寻找该字段的值。
更正这些后,存储过程按预期运行。
【讨论】:
总是列出列名的好教训。以上是关于列名或提供的值的数量与更新时的表定义不匹配的主要内容,如果未能解决你的问题,请参考以下文章