如何在游标中使用存储过程?
我建立了一个游标BankStatment求银行的存款
declare
@results table
(
id int,
PersonName varchar(100),
Balance money,
)
declare BankStatment cursor
for
select ID from Person
open BankStatement
declare @id int
fetch next from BankStatement into @id
while
(@@fetch_status =0)
begin
exec GetPersonBalance @id
insert @results
fetch next from BankStatement into @id
end
deallocate BankStatement
这个游标通过表Person中的ID,用存储过程GetPersonBalance计算出PersonName和他的Balance
请大家看看我的写的这个哪里错了?
为何说Incorrect syntax near the keyword 'fetch'
------解决方案--------------------
- SQL code
fetch next into @id from BankStatement --还有需close BankStatementdeallocate BankStatement
------解决方案--------------------
- SQL code
--不好意思,你是这里错了,fetch语法没错--insert @results fetch next from BankStatement into @id
------解决方案--------------------
------解决方案--------------------
- SQL code
insert @results exec GetPersonBalance @id
------解决方案--------------------
用insert @result values(@id,@PersonName,@Balance)