希望這是你在找什麼:
declare
asdf number;
begin
for r in (select * from xyz) loop
insert into abc (column1, column2, column3)
values (r.asdf, r.vcxvc, r.dffgdfg) returning id into asdf;
update xyz set column10 = asdf where ID = r.ID;
end loop;
end;
將成爲
DECLARE @asdf int
DECLARE @ID int
DECLARE @MyTmpTableVar table(asdf int, abc_id int)
// insert records from your cursor r into table abc,
// and return a temporary table with "id" and "asdf" fields from the xyz table
INSERT INTO abc(column1, column2, column3)
OUTPUT asdf, id INTO @MyTmpTableVar
SELECT r.asdf, r.vcxvc, r.dffgdfg
FROM xyz
// if it would return just one row and you wanted to find the value
//SELECT @asdf = asdf, @id = abc_id
//FROM @MyTmpTableVar
// update the table xyz with the values stored in temporary table
// restricting by the key "id"
UPDATE xyz
SET xyz.column10 = t.asdf
FROM @MyTmpTableVar AS t
WHERE xyz.id = t.abc_id
sqlserver版本的在Oracle中返回子句是OUTPUT子句。
請訪問this msdn page的完整信息,
戴維斯,請考慮使用遊標,你不應該,如果在所有可能使用SQL Server遊標,因爲它們相比非常緩慢,基於集合的解決方案。提供了基於集合的解決方案,您應該考慮它們。即使您正在從Oracle轉換,您也需要停止思考循環,並在使用SQL Server時開始思考數據集的間歇性。 – HLGEM 2009-05-26 14:42:57