我運行SQL Server 2008 R2和我收到以下錯誤,當我運行存儲過程sp_smc_add_personuser
得到一個錯誤。運行時,這個存儲過程
消息213,級別16,狀態1,行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 [email protected] and
[email protected])
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
編輯 - 我正在尋找一些幫助追逐問題,因爲我不太清楚如何去解決這個問題。如果所有你能做的就是給我提供一些故障診斷的一般策略(因爲我不能使用內置的調試程序,由於權限問題),那麼這將是非常有幫助的。
你確定你需要使用遊標?這個程序RBAR並不需要! – Matthew 2014-09-25 23:16:37
旁註:你應該** **不使用'sp_'前綴爲您的存儲過程。微軟已經保留了這個前綴以供自己使用(參見*命名存儲過程*)](http://msdn.microsoft.com/en-us/library/ms190669%28v=sql.105%29.aspx),以及你將來有可能冒着名字衝突的風險。 [這對你的存儲過程性能也是不利的](http://www.sqlperformance.com/2012/10/t-sql-queries/sp_prefix)。最好只是簡單地避免使用'sp_'並將其他內容用作前綴 - 或者根本沒有前綴! – 2014-09-26 05:06:12