读书人

怎么在游标中使用存储过程

发布时间: 2012-08-02 11:35:26 作者: rapoo

如何在游标中使用存储过程

如何在游标中使用存储过程?

我建立了一个游标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

--不好意思,你是这里错了,fetch语法没错
--insert @results
fetch next from BankStatement into @id


是insert语法有问题吗

[解决办法]
SQL code
 insert @results exec GetPersonBalance @id
[解决办法]
用insert @result values(@id,@PersonName,@Balance)

读书人网 >SQL Server

热点推荐