我正在运行SQL Server 2008 R2,并且在运行存储过程sp_smc_add_personuser
时收到以下错误。
Msg 213,Level 16,State 1,Line 45
列名或提供的值数与表定义不匹配。
这是我尝试运行的代码。
ALTER PROCEDURE [dbo].[sp_smc_add_personuser]
as
--source variables
declare @personid int
declare @samid nvarchar(256)
declare @rdate datetime
declare @cdate datetime
DECLARE @YEAR VARCHAR(6)
DECLARE @TERM VARCHAR(6)
--SELECT @YEAR=ACADEMIC_YEAR,
-- @TERM=ACADEMIC_TERM
--FROM SMCHELPER..CURRENTTERM
--WHERE OFFICE='ADMISSION'
declare cur_PersonAD cursor for
select distinct p.personid,ad.samid as UserName,getdate() as CreateDate, getdate() as RevisionDate from people p
inner join smchelper.dbo.activedirectory ad on p.people_code_id=ad.empid
inner join academic a on p.people_code_id=a.people_code_id
where ad.samid in (select samid from smchelper..activedirectory)
and (a.academic_flag='Y' or a.application_flag='Y')
and a.academic_year >= '2012'
and p.personid not in (select personid from campus6.dbo.personuser)
open cur_PersonAD
fetch next from cur_PersonAD into @personid,@samid,@rdate,@cdate
while (@@fetch_status<>-1)
begin
if (@@fetch_status<>-2)
begin
--print @personid
--print @samid
if not exists (select personid from personuser pu
where pu.personid=@personid and
pu.username=@samid)
begin
insert into personuser
values
(@personid
,@samid
,@rdate
,@cdate
)
end
end
fetch next from cur_PersonAD into @personid,@samid,@rdate,@cdate
end
close cur_PersonAD
Deallocate cur_PersonAD
编辑 - 我正在寻找帮助追逐问题的一些帮助,因为我不太确定如何对此进行故障排除。如果您能做的就是给我一些一般的故障排除策略(鉴于权限问题导致我无法使用内置调试器),那将非常有用。
答案 0 :(得分:1)
您的错误似乎在这里:
insert into personuser
values
(@personid
,@samid
,@rdate
,@cdate
)
您应该明确列出要使用提供的值填充personuser
中的哪四列
http://www.w3schools.com/sql/sql_insert.asp
只有personuser
恰好是四列时,您的语法才有效。