在单个Oracle Query中更新

时间:2018-06-01 02:05:50

标签: oracle11g outer-join insert-update insert-into

我尝试在Oracle数据库中编写单个代码,但我无法将其合并到一个查询中。当我最初运行代码时,我得到“ORA-00933:SQL命令未正确结束错误。我也尝试使用FOR LOOP,CASE语句,但它仍然没有用。你能告诉我如何在一个代码中合并查询吗?

要求 在DTW上创建列CIDO并将数据从V插入到DTW,必须满足以下条件 CIDO = CID 作为连接的一部分,我必须在V的CID和Q的SID上使用左连接到Q表 然后需要更新DTW中的CID,其中QCI中的CFCID来自V = MX00

的CCode

我写道:

Alter table DTW add CIDO  -- added column 


Select  V.CID from  V,Q  where  V.CID=Q.SID 
Insert into DTW (CIDO)
Select V.CID from  V 
Left outer join Q  on V.CID = Q.SID 

Update DTW
Set CIDO = (select V.CID from V, Q
Where V.CID=Q.CFCID)

1 个答案:

答案 0 :(得分:0)

好吧,您没有说如何运行它,所以我假设使用sqlplus。您在那里有4个不同的命令。在命令行中,您需要运行每个命令并放一个;在每个结尾处。

-- Pretty sure your syntax is wrong here. What's the datatype of CIDO?
Alter table DTW add CIDO;

-- This will just display a list of CIDs, not sure if that's what you want.
Select  V.CID from  V,Q  where  V.CID=Q.SID;

-- This will insert one or more rows from V and Q
Insert into DTW (CIDO)
Select V.CID from  V 
Left outer join Q  on V.CID = Q.SID;

-- This will update all the rows in DTW to the same value, which is probably not what
-- you want. Also, if there's more than one row in this subquery, it will raise an error.
Update DTW
Set CIDO = (select V.CID from V, Q
Where V.CID=Q.CFCID);