SQL Server中的游标函数等价物

SQL Server中的游标函数等价物,sql,sql-server,oracle,cursor,batch-processing,Sql,Sql Server,Oracle,Cursor,Batch Processing,我想问一下SQL Server中的等效代码 我正在从事一个项目,由于数据库迁移,我们将把unix批处理转换为windows批处理。因为我使用的是shell脚本和oracle的东西,所以我在windows端的工作很辛苦 我的问题是关于oracle sql使用的游标 有人能帮我转换下面的示例查询吗 sampleanimal.sql: declare cursor getGM is select dog as d, cat as c, fish as f from animals begin

我想问一下SQL Server中的等效代码

我正在从事一个项目,由于数据库迁移,我们将把unix批处理转换为windows批处理。因为我使用的是shell脚本和oracle的东西,所以我在windows端的工作很辛苦

我的问题是关于oracle sql使用的游标

有人能帮我转换下面的示例查询吗

sampleanimal.sql

declare cursor getGM is
select dog as d, cat as c, fish as f
  from animals

begin
  for dr in getGM loop

    UPDATE zoo
      SET dogs = dr.d
          cats = dr.c
          fishes = dr.f
      ;

  end loop;
end;
/

commit;

quit; 

可以有更有效或更简单的方法,我只是写了没有测试

DECLARE @d nvarchar(10);
DECLARE @c nvarchar(10);
DECLARE @f nvarchar(10);

DECLARE getGM CURSOR LOCAL FORWARD_ONLY READ_ONLY FOR
select dog as d, cat as c, fish as f from animals

OPEN getGM
FETCH NEXT FROM getGM INTO @d, @c, @f

WHILE @@FETCH_STATUS = 0
Begin
UPDATE zoo
      SET dogs = @d
          cats = @c
          fishes = @f

FETCH NEXT FROM getGM INTO @d, @c, @f
End

CLOSE getGM
DEALLOCATE getGM

您可以查看

中的示例实际上,在这种情况下不需要使用游标。试试这样的-

UPDATE z
SET 
      dogs = a.dog
    , cats = a.cat
    , fishes = a.fish
FROM zoo z
JOIN animals a ON z.id = a.id