Home » SQL & PL/SQL » SQL & PL/SQL » New developer and facing trouble with a store procedure (Oracle 11g)
New developer and facing trouble with a store procedure [message #625207] Thu, 02 October 2014 12:22 Go to next message
new_techie_14
Messages: 3
Registered: October 2014
Location: US
Junior Member
Please know that I recently start working with sql & pl/sql so very new to this.

So I'm trying to write a store procedure to populate a type_product_relation table which will take data from 2 tables, 1: Type table 2: Product table.

So added a attachment of how the data should look. The TYPE_PRODUCT_RELATION table is a incremental table.The TYPE_KEY comes from TYPE table, the PRODUCT_KEY comes from PRODUCT Table.

The DATE field should be changed based on what date it is.
The START_DATE is when the relationship between TYPE and PRODUCT entered in the TYPE_PRODUCT_RELATION table.
The END_DATE when 1st entered should be the date it was entered, but next day it should be changed to that date and so on.

The example I have in the file where a particular relation was added on Day 1, and then Day 2 nothing changed in the relation so the DATE & the END_DATE fields have to be changed to that date. Other scenario is when it's Day 3 the TYPE is not linked to a PRODUCT any more so that TYPE_KEY-PRODUCT_KEY relation row the END_DATE should be yesterday, but rest of the END_DATE's should be changed to Day 3 date.

I'm not exactly sure how to go about building the store procedure for this.

Thanks for all the help.
  • Attachment: Book2.csv
    (Size: 0.61KB, Downloaded 997 times)
Re: New developer and facing trouble with a store procedure [message #625208 is a reply to message #625207] Thu, 02 October 2014 12:24 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Welcome to this forum.

Please read and follow the forum guidelines, to enable us to help you:

http://www.orafaq.com/forum/t/88153/0/ and read http://www.orafaq.com/forum/t/174502/
Re: New developer and facing trouble with a store procedure [message #625209 is a reply to message #625207] Thu, 02 October 2014 12:29 Go to previous messageGo to next message
EdStevens
Messages: 1376
Registered: September 2013
Senior Member
I'm sorry, but my workplace (as do many others) blocks the download of attached files. Why can't you just put a little sample directly int your post, enclosed by "code" tags?

Without looking any further, your approach of constantly updating 'end-date' to the current date is a serious design flaw. What is the purpose of doing so? Because an 'end date' hasn't yet been established? If so I would leave the column null until such time as it is established.

If you are having trouble with the PL/SQL, show us your work so far. Many people will be more than happy to help out, but no one worth their salt is going to write it for you. It's much easier for us to correct code that misses the mark than to make a wild guess at something.

[Updated on: Thu, 02 October 2014 12:31]

Report message to a moderator

Re: New developer and facing trouble with a store procedure [message #625211 is a reply to message #625209] Thu, 02 October 2014 13:36 Go to previous messageGo to next message
new_techie_14
Messages: 3
Registered: October 2014
Location: US
Junior Member
My sample data:
DAY 1:				
TYPE_KEY	PRODUCT_KEY	DATE	        START_DATE	END_DATE
2	             12	        10/1/2014	10/1/2014	10/1/2014
2	             45	        10/1/2014	10/1/2014	10/1/2014
2	             23	        10/1/2014	10/1/2014	10/1/2014
2	             90	        10/1/2014	10/1/2014	10/1/2014
				
DAY 2:				
TYPE_KEY	PRODUCT_KEY	DATE	        START_DATE	END_DATE
2	             12	        10/2/2014	10/1/2014	10/2/2014
2	             45	        10/2/2014	10/1/2014	10/2/2014
2	             23	        10/2/2014	10/1/2014	10/2/2014
2	             90	        10/2/2014	10/1/2014	10/2/2014
				
DAY 3:				
TYPE_KEY	PRODUCT_KEY	DATE	        START_DATE	END_DATE
2	            12	        10/3/2014	10/1/2014	10/3/2014
2	            45	        10/3/2014	10/1/2014	10/3/2014
2	            23	        10/3/2014	10/1/2014	10/3/2014
2	            90	        10/3/2014	10/1/2014	10/2/2014 - this product isn't linked to that type anymore


So far this is the way I think it should be done.
PROCEDURE TYPE_PRODUCT_REL

MERGE INTO TYPE_PRODUCT_RELATION TPR
USING
(
SELECT TYPE_KEY,
       PRODUCT_KEY
 FROM TYPE T JOIN PRODUCT P
	ON T.PRODUCT_ID = P.PRODUCT_ID

)SRC

ON (TPR.TYPE_KEY = SRC.TYPE_KEY
AND TPR.PRODUCT_KEY = SRC.PRODUCT_KEY
)

WHEN NOT MATCHED THEN
INSERT
(
        TYPE_KEY,
	PRODUCT_KEY,
        DATE,
        START_DATE,
        END_DATE,
        INSERT_TIME,
        LAST_UPDATED_BY
)
VALUES
(
        SRC.TYPE_KEY
        SRC.PRODUCT_KEY,
        TRUNC(SYSDATE),
        TRUNC(SYSDATE),
        TRUNC(SYSDATE),
        SYSTIMESTAMP,
        USER
)

WHEN MATCHED THEN
UPDATE

SET
DATE= TRUNC(SYSDATE),
END_DATE = TRUNC(SYSDATE),
LAST_UPDATED_TIME = SYSTIMESTAMP;

COMMIT;



Even I don't agree with the end date constantly updating, but that's the requirement.
Re: New developer and facing trouble with a store procedure [message #625212 is a reply to message #625209] Thu, 02 October 2014 13:57 Go to previous messageGo to next message
new_techie_14
Messages: 3
Registered: October 2014
Location: US
Junior Member
I think my questions is, what is a better way to do what I am trying to do?
Re: New developer and facing trouble with a store procedure [message #625214 is a reply to message #625212] Thu, 02 October 2014 15:48 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Post TESTED DDL (CREATE TABLE) for referenced tables & INSERT statements for test data to reproduce problem test case.
Re: New developer and facing trouble with a store procedure [message #625220 is a reply to message #625214] Fri, 03 October 2014 03:06 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Does the merge do what you want?
Re: New developer and facing trouble with a store procedure [message #625236 is a reply to message #625211] Fri, 03 October 2014 07:40 Go to previous message
EdStevens
Messages: 1376
Registered: September 2013
Senior Member
Quote:
Even I don't agree with the end date constantly updating, but that's the requirement.


Have you tried to explain the problems and alternatives?

Sometimes you win, sometimes you don't, but never just roll over in the face of stupidity.
Previous Topic: Re: Update trigger
Next Topic: Question on LONG datatype
Goto Forum:
  


Current Time: Fri Apr 26 19:12:56 CDT 2024