using stored procedures - · pdf fileusing stored procedures in otm agents ... • otm...

19

Upload: truongnhi

Post on 29-Mar-2018

228 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Using Stored Procedures - · PDF fileUsing Stored Procedures in OTM Agents ... • OTM agent built-in functionality is not present in the ... • Follow Software Implementation Best
Page 2: Using Stored Procedures - · PDF fileUsing Stored Procedures in OTM Agents ... • OTM agent built-in functionality is not present in the ... • Follow Software Implementation Best

Using Stored Procedures

in OTM Agents

David Tremain

OmniSource, Inc.

Fort Wayne, Indiana, USA

Page 3: Using Stored Procedures - · PDF fileUsing Stored Procedures in OTM Agents ... • OTM agent built-in functionality is not present in the ... • Follow Software Implementation Best

What is a stored procedure, and

how does it fit into OTM?• It is separate from OTM – it lives in the

database and works with OTM.

3

OTM DATABASE

Agents Stored Procedure

Page 4: Using Stored Procedures - · PDF fileUsing Stored Procedures in OTM Agents ... • OTM agent built-in functionality is not present in the ... • Follow Software Implementation Best

Disclaimer

• This is one organization’s experience

• It worked for us

• It has significant impact and requires database expertise

• It may not work for you – proceed at your own risk

• There may be a better way to do it

4

Page 5: Using Stored Procedures - · PDF fileUsing Stored Procedures in OTM Agents ... • OTM agent built-in functionality is not present in the ... • Follow Software Implementation Best

When to consider using a stored

procedure in an agent– Need to improve performance

– Need to simplify maintenance

– Agent has a non-trivial amount of SQL Activity (DSU’s primarily)

– Multiple DSUs occur in logical grouping(s)

– Multiple variations of an agent or group of agents

– There is limited need to communicate information with the agent

5

Page 6: Using Stored Procedures - · PDF fileUsing Stored Procedures in OTM Agents ... • OTM agent built-in functionality is not present in the ... • Follow Software Implementation Best

How to implement – Overview • Determine functionality to move to the stored

procedure

• Determine information needed by the stored procedure

• Write and test the stored procedure outside of the agent

• Create a version of the agent that uses the stored procedure

6

Page 7: Using Stored Procedures - · PDF fileUsing Stored Procedures in OTM Agents ... • OTM agent built-in functionality is not present in the ... • Follow Software Implementation Best

Identifying portions of agent to

implement in stored procedure• Multiple statements grouped by logic that

don’t have to be in an agent (DSU’s, Assign Variable, Set Ref Num)

• Restructure the agent to group DSU’s, etc. together, separate from agent-specific actions

• Stored Procedure does not return value(s) –it either works or errors

7

Page 8: Using Stored Procedures - · PDF fileUsing Stored Procedures in OTM Agents ... • OTM agent built-in functionality is not present in the ... • Follow Software Implementation Best

Implementation of the Stored

Procedure• Input Value(s) – e.g., $gid

• Use Blocking and Exceptions

• Use COMMIT for INSERT / UPDATE statements

• Conditionals

• Debug, Execution Time, and other functionality

8

Page 9: Using Stored Procedures - · PDF fileUsing Stored Procedures in OTM Agents ... • OTM agent built-in functionality is not present in the ... • Follow Software Implementation Best

Calling the stored procedure

from the agent• Use the Stored Procedure Statement Type

in Direct SQL Update

• SQL Statement to call the Stored Procedure:CALL GLOGOWNER.OSPK_SHIPMENT_CREATED.P1($GID)

• Passing values to the Stored Procedure

• No return values

9

Page 10: Using Stored Procedures - · PDF fileUsing Stored Procedures in OTM Agents ... • OTM agent built-in functionality is not present in the ... • Follow Software Implementation Best

Factors to Watch For• KISS – Don’t write a constitution when a paragraph

will do

• Execution Speed

• “Hidden” Functionality – stored procedure won’t be visible from OTM interface

• OTM agent built-in functionality is not present in the Stored Procedures

• External Processes not included in LOGs

10

Page 11: Using Stored Procedures - · PDF fileUsing Stored Procedures in OTM Agents ... • OTM agent built-in functionality is not present in the ... • Follow Software Implementation Best

Pitfalls to Avoid

• Need to manually perform OTM-provided

functionality

• Avoid Concurrency

• Follow Software Implementation Best

Practices

11

Page 12: Using Stored Procedures - · PDF fileUsing Stored Procedures in OTM Agents ... • OTM agent built-in functionality is not present in the ... • Follow Software Implementation Best

Agent Before Stored Procedure

12

MovetoStoredProcedure

Page 13: Using Stored Procedures - · PDF fileUsing Stored Procedures in OTM Agents ... • OTM agent built-in functionality is not present in the ... • Follow Software Implementation Best

Example: Agent using Stored

Procedure

13

Page 14: Using Stored Procedures - · PDF fileUsing Stored Procedures in OTM Agents ... • OTM agent built-in functionality is not present in the ... • Follow Software Implementation Best

Example

of DSU

calling

Stored

Procedure

14

Page 15: Using Stored Procedures - · PDF fileUsing Stored Procedures in OTM Agents ... • OTM agent built-in functionality is not present in the ... • Follow Software Implementation Best

Example Stored Procedure CodeCREATE OR REPLACE PACKAGE BODY GLOGOWNER.OSPK_SHIPMENT_CREATED

AS

PROCEDURE p1(

p_gid VARCHAR2)

IS

--

v_dttm DATE := SYSDATE;

v_user VARCHAR2(50) := vpd.get_gl_user();

--

BEGIN

--

BEGIN

INSERT

INTO shipment_refnum VALUES

(

p_gid,

'OMS.EQ_GROUP',

( SELECT eg.equipment_group_name FROM shipment s, equipment_group eg

WHERE s.first_equipment_group_gid = eg.equipment_group_gid

AND s.shipment_gid = p_gid

),

'OMS', v_user, v_dttm, '', ''

);

COMMIT;

EXCEPTION

WHEN OTHERS THEN

raise_application_error(-20001,'EQ_GROUP : '||SUBSTR(SQLERRM,1,200));

END;

--

15

Page 16: Using Stored Procedures - · PDF fileUsing Stored Procedures in OTM Agents ... • OTM agent built-in functionality is not present in the ... • Follow Software Implementation Best

Example Stored Procedure CodeBEGIN

INSERT

INTO shipment_refnum VALUES

(

p_gid,

'OMS.ORDER_RELEASES',

(SELECT listagg(o.order_release_xid, ', ') WITHIN GROUP (

ORDER BY o.order_release_xid)

FROM order_release o,

view_shipment_order_release vsor

WHERE o.order_release_gid = vsor.order_release_gid

AND vsor.shipment_gid = p_gid

),

'OMS',

v_user,

v_dttm,

'',

''

);

COMMIT;

EXCEPTION

WHEN OTHERS THEN

raise_application_error(-20001,'ORDER_RELEASES : '||SUBSTR(SQLERRM,1,200));

END;

--

16

Page 17: Using Stored Procedures - · PDF fileUsing Stored Procedures in OTM Agents ... • OTM agent built-in functionality is not present in the ... • Follow Software Implementation Best

Example Stored Procedure CodeBEGIN

INSERT

INTO shipment_refnum VALUES

(

p_gid,

'OMS.ITEM_DESCRIPTION',

(SELECT NVL(

(SELECT listagg(orr.order_release_refnum_value, ', ') WITHIN GROUP (

ORDER BY orr.order_release_gid)

FROM order_release_refnum orr,

order_release o,

view_shipment_order_release vsor

WHERE o.order_release_gid = orr.order_release_gid

AND orr.order_release_refnum_qual_gid = 'OMS.ITEM_DESCRIPTION'

AND o.order_release_gid = vsor.order_release_gid

AND vsor.shipment_gid = p_gid

),'NO_DESCRIPTION')

FROM dual

),

'OMS',

v_user,

v_dttm,

'',

''

);

COMMIT;

EXCEPTION

WHEN OTHERS THEN

raise_application_error(-20001,'ITEM_DESCRIPTION : '||SUBSTR(SQLERRM,1,200));

END;

--

17

Page 18: Using Stored Procedures - · PDF fileUsing Stored Procedures in OTM Agents ... • OTM agent built-in functionality is not present in the ... • Follow Software Implementation Best

Example Stored Procedure CodeBEGIN

INSERT INTO shipment_remark VALUES

( p_gid, 1,

'OMS.SHIPMENT_COST_DETAIL',

( SELECT 'Total: ' || ROUND(s.total_actual_cost,2) ||' USD; ' || listagg(

ct.description || CASE sc.cost_type WHEN 'A' THEN ' ' ||sc.accessorial_code_gid

END ||': ' || ROUND(COST,2) ||' USD' ||' {'

|| NVL(scd.detail,NVL(sc.adjustment_reason_gid,' - no detail')),'}, ')

WITHIN GROUP ( ORDER BY sc.shipment_cost_seqno) ||'}'

FROM shipment s, cost_type ct, shipment_cost sc

LEFT OUTER JOIN shipment_cost_detail scd

ON sc.shipment_cost_seqno = scd.shipment_cost_seqno

WHERE sc.cost_type = ct.cost_type_gid

AND sc.shipment_gid = s.shipment_gid

AND sc.cost_type <> 'O'

AND sc.shipment_gid = p_gid

GROUP BY s.total_actual_cost

), 'OMS', v_user, v_dttm, '', '' );

COMMIT;

EXCEPTION

WHEN OTHERS THEN

raise_application_error(-20001,'SHIPMENT_COST_DETAIL : '||SUBSTR(SQLERRM,1,200));

END;

--

END p1;

. . .

--

END ospk_shipment_created;

18

Page 19: Using Stored Procedures - · PDF fileUsing Stored Procedures in OTM Agents ... • OTM agent built-in functionality is not present in the ... • Follow Software Implementation Best

Contact Info

David Tremain

[email protected]

19