2009-05-26 69 views
2

我想实现在MS SQL一种构造,看起来像这样在甲骨文的PL/SQL:的Transact-SQL:插入XYZ(从ABC选择*)

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; 

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

在此先感谢

+0

戴维斯,请考虑使用游标,你不应该,如果在所有可能使用SQL Server游标,因为它们相比非常缓慢,基于集合的解决方案。提供了基于集合的解决方案,您应该考虑它们。即使您正在从Oracle转换,您也需要停止思考循环,并在使用SQL Server时开始思考数据集的间歇性。 – HLGEM 2009-05-26 14:42:57

回答

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都会杀了你,如果你尝试并将光标潜入生产代码。

+0

DBA为什么会因游标而变得不适? – Andomar 2009-05-26 07:56:05

+1

因为游标是性能杀手,应该用基于集合的代码替换。我知道这在Oracle中并非如此,但它在SQL Server中。 – HLGEM 2009-05-26 14:39:45

6

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

好:

SELECT column1, column2, column3 INTO abc FROM xyz 

我想你也能以类似

INSERT INTO abc SELECT column1, column2, column3 FROM xyz 

但在你需要之前创建表的第二种情况,第一,而不是不还创建表

Cheers Johannes

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; 
1

如果我明白你问(我不精通PL/SQL),看起来像一个非常简单的任务:

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

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

PS:作为someelse已经指出的那样,你必须已经创建表ABC

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 

sqlserver版本的在Oracle中返回子句OUTPUT子句。

请访问this msdn page的完整信息,