关于MERGE编译错误,请帮助解决

Uncommon 2007-09-05 12:45:15
create or replace procedure PR_UpSert_T_UserInfo (
v_UserName T_UserInfo.UserName%TYPE,
v_Password T_UserInfo.Password%TYPE,
v_RoleID T_UserInfo.RoleID%TYPE,
v_Sex T_UserInfo.Sex%TYPE,
v_IDCard T_UserInfo.IDCard%TYPE,
v_Unit T_UserInfo.Unit%TYPE,
v_Department T_UserInfo.Department%TYPE,
v_Memo T_UserInfo.Memo%TYPE
) AS
begin

merge into T_UserInfo
using T_UserInfo
on (T_UserInfo.UserName=v_UserName)
when matched then
update set
T_UserInfo.Password=v_Password,
T_UserInfo.RoleID=v_RoleID
when not matched then
insert values(
v_UserName,
Password,
RoleID) ;

end PR_UpSert_T_UserInfo;
/
...全文
223 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
JWealth 2010-04-22
  • 打赏
  • 举报
回复
英语不好呀
icedut 2007-09-07
  • 打赏
  • 举报
回复
zt
http://www.vipcn.com/InfoView/Article_104338.html
MERGE INTO [your table-name] [rename your table here]

USING

(

[write your query here]

)[rename your query-sql and using just like a table]

ON

([conditional eXPression here] AND [...]...)

WHEN

MATHED

THEN

[here you can execute some update sql or something else ]

WHEN

NOT MATHED

THEN

[execute something else here ! ]

icedut 2007-09-07
  • 打赏
  • 举报
回复
zt
merge Statement

Oracle9i introduces a new set of server functionality especially beneficial for the ETL (Extraction, Transformation, and Loading) part of any Business Intelligence process flow, addressing all the needs of highly scalable data transformation inside the database.
One of the most exciting new features addressing the needs for ETL is the SQL statement merge. The new SQL combines the sequence of conditional INSERT and UPDATE commands in a single atomic statement, depending on the existence of a record. This operation is commonly known as Upsert functionality.

In a data warehouse environment, tables (typically fact tables) need to be refreshed periodically with new data arriving from on-line systems. This new data may contain changes to existing rows in tables of the warehouse and/or new rows that need to be inserted. If a row in the new data corresponds to an item that already exists in the table, an UPDATE is performed; if the row's primary key does not exist in the table, an INSERT is performed. Many times, the source system cannot distinguish between newly inserted or changed information during the extraction; for very complex transformations, it's sometimes nearly impossible to know the effect of changed source data. These scenarios require the determination of insert versus update to be done during data loading.

Prior to Oracle9i, these operations were expressed either as a sequence of DMLs (INSERT/UPDATE) or as PL/SQL loops deciding, for each row, whether to insert or update the data. Both these techniques face performance handicaps: the first requires multiple data scans, and the second operates on a per-record basis. By extending SQL with a new syntax - the merge statement - Oracle9i overcomes the deficiencies of the old approaches and makes the implementation of warehousing applications more simple and intuitive.

The following is an example of the merge statement. The fact table SALES_FACT in a data warehouse for a retailer needs to be periodically updated with sales data coming from the on-line systems. If the retailer opens a new store, then the data for the store needs to be inserted into the SALES_FACT table.

Oracle9i Implementation:
In Oracle9i, the merge statement INSERTS and UPDATES the data with a single SQL statement.
merge INTO SALES_FACT D
USING SALES_JUL01 S
ON (D.TIME_ID = S.TIME_ID
AND D.STORE_ID = S.STORE_ID
AND D.REGION_ID = S.REGION_ID)
WHEN MATCHED THEN
UPDATE
SET d_parts = d_parts + s_parts,
d_sales_amt = d_sales_amt + s_sales_amt,
d_tax_amt = d_tax_amt + s_tax_amt,
d_discount = d_discount + s_discount
WHEN NOT MATCHED THEN
INSERT (D.TIME_ID ,D.STORE_ID ,D.REGION_ID,
D.PARTS ,D.SALES_AMT ,D.TAX_AMT ,D.DISCOUNT)
VALUES (
S.TIME_ID ,S.STORE_ID ,S.REGION_ID,
S.PARTS ,S.SALES_AMT ,S.TAX_AMT ,S.DISCOUNT);

Oracle8i Implementation:
In Oracle8i, you could choose to implement it as a sequence of DML statements.
UPDATE
(SELECT
S.TIME_ID ,S.STORE_ID ,S.REGION_ID,
S.PARTS s_parts ,S.SALES_AMT s_sales_amt ,S.TAX_AMT s_tax_amt ,S.DISCOUNT s_discount,
D.PARTS d_parts ,D.SALES_AMT d_sales_amt ,D.TAX_AMT d_tax_amt ,D.DISCOUNT d_discount
FROM SALES_JUL01 S, SALES_FACT D
WHERE D.TIME_ID = S.TIME_ID
AND D.STORE_ID = S.STORE_ID
AND D.REGION_ID = S.REGION_ID) JV
SET d_parts = d_parts + s_parts,
d_sales_amt = d_sales_amt + s_sales_amt,
d_tax_amt = d_tax_amt + s_tax_amt,
d_discount = d_discount + s_discount
;
INSERT INTO SALES_FACT (
TIME_ID,STORE_ID ,REGION_ID,
PARTS ,SALES_AMT ,TAX_AMT ,DISCOUNT)
SELECT
S.TIME_ID ,S.STORE_ID ,S.REGION_ID,
S.PARTS ,S.SALES_AMT ,S.TAX_AMT ,S.DISCOUNT
FROM SALES_JUL01 S
WHERE (S.TIME_ID, S.STORE_ID, S.REGION_ID) NOT IN (
SELECT D.TIME_ID, D.STORE_ID, D.REGION_ID
FROM SALES_FACT D
)
;

Alternatively, you could create a procedural implementation. It would need to check every load record against the target to find if the record already exists; only then could it decide whether to insert or update the data.

Both of these approaches suffer from deficiencies in performance and usability. The new merge command overcomes these deficiencies, processing the conditional INSERT-or-UPDATE within a single statement. The data is scanned only once, and the appropriate DML command is issued, either serially or in parallel.

The new merge command brings major performance benefits by providing an optimized internal feature for the common Upsert task within ETL processing. Furthermore, it simplifies the development of transformation processing inside the database - using Oracle9i as the transformation engine.
icedut 2007-09-07
  • 打赏
  • 举报
回复
lz hongqi162(失踪的月亮)的方法不行么

create or replace procedure PR_UpSert_T_UserInfo (
v_UserName T_UserInfo.UserName%TYPE,
v_Password T_UserInfo.Password%TYPE,
v_RoleID T_UserInfo.RoleID%TYPE,
v_Sex T_UserInfo.Sex%TYPE,
v_IDCard T_UserInfo.IDCard%TYPE,
v_Unit T_UserInfo.Unit%TYPE,
v_Department T_UserInfo.Department%TYPE,
v_Memo T_UserInfo.Memo%TYPE
) AS
begin

merge into T_UserInfo t1
using ( select v_userName userName,v_Password Password,v_Roleid Roleid from dual) t
on (t1.UserName=t.username)
when matched then
update set
t1.Password=t.Password,
t1.RoleID=t.RoleID
when not matched then
insert (t1.UserName,t1.Password,t1.RoleID)
values(t.UserName,t.Password,t.RoleID) ;

end PR_UpSert_T_UserInfo;



kinglht 2007-09-05
  • 打赏
  • 举报
回复
同意楼上的,帮顶!
hongqi162 2007-09-05
  • 打赏
  • 举报
回复
create or replace procedure PR_UpSert_T_UserInfo (
v_UserName T_UserInfo.UserName%TYPE,
v_Password T_UserInfo.Password%TYPE,
v_RoleID T_UserInfo.RoleID%TYPE
) AS
begin

merge into T_UserInfo
using ( select v_userName userName,v_Password Password,v_Roleid Roleid from dual) t
on (T_UserInfo.UserName=t.username)
when matched then
update set
T_UserInfo.Password=t.Password,
T_UserInfo.RoleID=t.RoleID
when not matched then
insert (T_UserInfo.UserName,T_UserInfo.Password,T_UserInfo.RoleID)
values(t.UserName,t.Password,t.RoleID) ;
end PR_UpSert_T_UserInfo;

17,382

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧