ORACLE存储过程中,把一张表中指定条件的数据存到游标中使用和存到临时表中使用有什么区别?
2个回答
2016-06-21
展开全部
游标效率比临时表效率高
临时表如下:
SELECT EmpCo FROM Employees
select identity(int,1,1) as id,* into #T from Employees --第一条搜索语句
declare @i int
declare @total int
declare @EmpCo varchar(10)
select @total = count(*) from #T
set @i = 1
while @i <= @total
begin
select @EmpCo=EmpCo from #T where id = @i --第二条搜索语句,而且是循环执行的。
print @EmpCo
set @i = @i + 1
end
drop table #T
-----------------------------------------------
游标如下:
Declare @EmpCo as varchar(50)
Declare @EmpCo1 as varchar(50)
DECLARE Employee_Cursor CURSOR FOR
SELECT EmpCo FROM Employees ---只有在这有一条搜索语句
OPEN Employee_Cursor
FETCH NEXT FROM Employee_Cursor INTO @EmpCo
WHILE @@FETCH_STATUS = 0
BEGIN
FETCH NEXT FROM Employee_Cursor INTO @EmpCo
print @EmpCo
END
CLOSE Employee_Cursor
DEALLOCATE Employee_Cursor
临时表的,要用两条搜索语句,而且是循环执行的。 ;然而,游标的只用一条搜索语句,没有循环执行的循环语句
临时表如下:
SELECT EmpCo FROM Employees
select identity(int,1,1) as id,* into #T from Employees --第一条搜索语句
declare @i int
declare @total int
declare @EmpCo varchar(10)
select @total = count(*) from #T
set @i = 1
while @i <= @total
begin
select @EmpCo=EmpCo from #T where id = @i --第二条搜索语句,而且是循环执行的。
print @EmpCo
set @i = @i + 1
end
drop table #T
-----------------------------------------------
游标如下:
Declare @EmpCo as varchar(50)
Declare @EmpCo1 as varchar(50)
DECLARE Employee_Cursor CURSOR FOR
SELECT EmpCo FROM Employees ---只有在这有一条搜索语句
OPEN Employee_Cursor
FETCH NEXT FROM Employee_Cursor INTO @EmpCo
WHILE @@FETCH_STATUS = 0
BEGIN
FETCH NEXT FROM Employee_Cursor INTO @EmpCo
print @EmpCo
END
CLOSE Employee_Cursor
DEALLOCATE Employee_Cursor
临时表的,要用两条搜索语句,而且是循环执行的。 ;然而,游标的只用一条搜索语句,没有循环执行的循环语句
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询