2

我想在 MS SQL 中实现一个在 Oracle PL/SQL 中看起来像这样的构造:

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;

任何想法如何实现这一点都会有所帮助。

提前致谢

4

5 回答 5

6

这似乎只是一张表的副本,对吧?

好:

SELECT column1, column2, column3 INTO abc FROM xyz

我想你也可以像

INSERT INTO abc SELECT column1, column2, column3 FROM xyz

但在第二种情况下,您需要先创建表,第一种情况也会创建 TABLE

干杯约翰内斯

于 2009-05-26T07:30:40.093 回答
3

没有光标但带有 temp 列的版本:

-- //temporarily add the column (assume the table "abc" already exists)
ALTER TABLE "abc" ADD xyzID INT;
GO;
-- //insert all the data (assuming the ID field on "xyz" is called ID)
INSERT INTO "abc" (column1, column2, column3, xyzID) SELECT asdf, vcxvc, rdffgdfg, ID FROM "xyz";
-- //update "xyd" with the new ID
UPDATE "xyd" SET column10 = "abc".ID FROM "xyd" INNER JOIN "abc" ON "xyd".ID = "abc".xydID
-- //drop the temporary column
ALTER TABLE "abc" DROP COLUMN xyzID;
于 2009-05-26T07:33:23.270 回答
2
declare @asdf int/varchar -- unsure of datatype
declare @vcxvcint/varchar -- unsure of datatype
declare @dffgdfg int/varchar -- unsure of datatype
declare @id int    
declare db_cursor CURSOR FOR SELECT asdf, vcxvc, dffgdfg FROM xyz

OPEN db_cursor
FETCH NEXT FROM db_cursor
INTO @asdf, @vcxvcint, @dffgdfg

WHILE @@FETCH_STATUS = 0
BEGIN    
  insert into abc (column1, column2, column3) values (@asdf, @vcxvcint, @vcxvcint)    
  set @id = scope_identity() -- This will get the auto generated ID of the last inserted row
  update xyz set column10 = @asdf where id = @    
  FETCH NEXT FROM db_cursor INTO @name    
END

CLOSE db_cursor
DEALLOCATE db_cursor

当然,如果您尝试将光标潜入生产代码中,基本上所有 DBA 都会杀死您。

于 2009-05-26T07:31:45.613 回答
1

如果我理解你的问题(我不精通 PL/SQL),看起来很容易:

INSERT INTO abc(column1, column2, column3) SELECT asdf, vcxvc, dffgdfg FROM xyz;
UPDATE xyz SET column10 = id;

但我只是猜测你的意图,希望没有误解。

PS:正如其他人已经指出的那样,您必须已经创建了表 abc

于 2009-05-26T07:56:42.787 回答
0

希望这是您正在寻找的:

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

Oracle中returning子句的sqlServer版本是OUTPUT子句。

请访问此 msdn 页面以获取完整信息

于 2009-11-07T02:03:37.003 回答