获取错误列名称或提供的值的数量与表定义不匹配
Posted
技术标签:
【中文标题】获取错误列名称或提供的值的数量与表定义不匹配【英文标题】:Getting Error Column name or number of supplied values does not match table definition 【发布时间】:2016-01-26 11:57:40 【问题描述】:当我执行这个存储过程时,我得到一个错误:
消息 213,第 16 级,状态 7,第 1 行 列名或提供的值的数量与表定义不匹配。
代码:
alter procedure AutoUpdate_EmpSal
as
begin
DECLARE @cmd AS NVARCHAR(max)
CREATE TABLE #result
(
SalaryId int,
EmpId INT,
TakenSalary float,
AvailSalary float
)
declare @salaryId int
declare @empId int
declare @takenSalary float
declare @availSalary float
SET @cmd ='select * from Employee_SalaryDetails where SalaryId in( SELECT max(SalaryId) FROM Employee_SalaryDetails group by EmpId )'
Insert into #result
EXEC(@cmd)
declare result_cursor cursor for
select SalaryId,EmpId,TakenSalary,AvailSalary
from #result
OPEN result_cursor
FETCH NEXT FROM result_cursor INTO @salaryId,@empId,@takenSalary,@availSalary
WHILE @@FETCH_STATUS = 0
BEGIN
update Employee_SalaryDetails
set Employee_SalaryDetails.AvailSalary=(@availSalary+Employee.Salary)
from Employee_SalaryDetails,Employee
where Employee_SalaryDetails.SalaryId=@salaryId
and Employee_SalaryDetails.EmpId=Employee.EmpId
FETCH NEXT FROM result_cursor INTO @salaryId,@empId,@takenSalary,@availSalary
end
select * from #result
end
【问题讨论】:
【参考方案1】:从您的 cmd 查询中删除“*”并选择 #result 表中的那些列。
【讨论】:
【参考方案2】:也许,您可以选择要插入的详细信息列。您可以更改此代码:
select * from Employee_SalaryDetails
where SalaryId in( SELECT max(SalaryId) FROM Employee_SalaryDetails group by EmpId)
代码:
select SalaryId, EmpId, TakenSalary, AvailSalary FROM Employee_SalaryDetails
where SalaryId in( SELECT max(SalaryId) FROM Employee_SalaryDetails group by EmpId)
【讨论】:
以上是关于获取错误列名称或提供的值的数量与表定义不匹配的主要内容,如果未能解决你的问题,请参考以下文章