DROP TABLE LIMITS_AND_TOTALS; CREATE TABLE LIMITS_AND_TOTALS ( LIMITS_AND_TOTALS_KEY NUMBER(10) NOT NULL, ACTIVE_FLAG VARCHAR2(1) DEFAULT 'Y' NOT NULL, REC_CLASS VARCHAR2(30) NOT NULL, CAP_LIMIT NUMBER(10) NOT NULL, CURRENT_CNT NUMBER(10) NOT NULL, DEALER2DEALER NUMBER(10), DEALER_SER2DEALER_SERVICE NUMBER(10), WHERE_CLAUSE VARCHAR2(4000 BYTE), PLAYING_FIELD2PLAYING_FIELD NUMBER(10) ); ALTER TABLE LIMITS_AND_TOTALS ADD ( CONSTRAINT LIMITS_AND_TOTALS_PK PRIMARY KEY (LIMITS_AND_TOTALS_KEY)); --ALTER TABLE LIMITS_AND_TOTALS ADD ( -- CONSTRAINT DEALER_FK -- FOREIGN KEY (DEALER2DEALER) -- REFERENCES DEALER (DEALER_KEY) -- ); --ALTER TABLE LIMITS_AND_TOTALS ADD ( -- CONSTRAINT DEALER_SER_FK -- FOREIGN KEY (DEALER_SER2DEALER_SERVICE) -- REFERENCES DEALER_SERVICE (DEALER_SERVICE_KEY) -- ); --ALTER TABLE LIMITS_AND_TOTALS ADD ( -- CONSTRAINT PLAYING_FIELD_FK -- FOREIGN KEY (PLAYING_FIELD2PLAYING_FIELD) -- REFERENCES PLAYING_FIELD (PLAYING_FIELD_KEY)); CREATE OR REPLACE FUNCTION flimit_unique ( rec_class VARCHAR2 , dealer_key INTEGER , dealer_service_key INTEGER , playing_field_key INTEGER , active_flag VARCHAR2 , limits_and_totals_key INTEGER ) RETURN VARCHAR2 DETERMINISTIC AS class_str CHAR ( 30 ); key_str CHAR ( 10 ); playing_field_str CHAR ( 10 ); unique_str CHAR ( 10 ); result_str VARCHAR2 ( 90 ); active_str CHAR ( 1 ); BEGIN class_str := rec_class; -- copy the playing field FK or use 0 (zero) in the even of null playing_field_str := COALESCE ( TO_CHAR ( playing_field_key ), '0' ); active_str := active_flag; IF class_str = 'PRODUCT' OR ( class_str = 'DEALER_SERVICE' AND active_str = 'N' ) THEN -- append the primary key unique_str := TO_CHAR ( limits_and_totals_key ); -- -- doing this forces the generation of a unique index key -- so THIS will always be unique ELSE unique_str := 0; END IF; -- pick the FK that is relevant to the current REC_CLASS type. -- Only one of these FKs will have a non null value key_str := COALESCE ( TO_CHAR ( dealer_key ), TO_CHAR ( dealer_service_key ) ); result_str := RPAD ( RTRIM ( class_str ), 30, '*' ) || LPAD ( RTRIM ( key_str ), 10, '#' ) || LPAD ( RTRIM ( playing_field_str ), 10, '+' ) || LPAD ( RTRIM ( unique_str ), 10, '@' ); RETURN result_str; END; / CREATE UNIQUE INDEX limits_and_totals_ix ON limits_and_totals (flimit_unique ("REC_CLASS", "DEALER2DEALER", "DEALER_SER2DEALER_SERVICE", "PLAYING_FIELD2PLAYING_FIELD", "ACTIVE_FLAG", "LIMITS_AND_TOTALS_KEY")); INSERT INTO limits_and_totals ( limits_and_totals_key, active_flag, rec_class, cap_limit, current_cnt , dealer_ser2dealer_service, where_clause, playing_field2playing_field ) VALUES ( 70, 'Y', 'PRODUCT', 4, 1 , 2202, 'number-Floors=2', 1111 ); INSERT INTO limits_and_totals ( limits_and_totals_key, active_flag, rec_class, cap_limit, current_cnt , dealer_ser2dealer_service, where_clause, playing_field2playing_field ) VALUES ( 80, 'Y', 'PRODUCT', 5, 2 , 2202, 'number_floors = 1', 1111 ); INSERT INTO limits_and_totals ( limits_and_totals_key, active_flag, rec_class, cap_limit, current_cnt , dealer_ser2dealer_service, playing_field2playing_field ) VALUES ( 90, 'N', 'DEALER_SERVICE', 22, 5 , 2202, 3333 ); INSERT INTO limits_and_totals ( limits_and_totals_key, active_flag, rec_class, cap_limit, current_cnt , dealer_ser2dealer_service, playing_field2playing_field ) VALUES ( 100, 'Y', 'DEALER_SERVICE', 14, 3 , 2202, 3333 ); INSERT INTO limits_and_totals ( limits_and_totals_key, active_flag, rec_class, cap_limit, current_cnt , dealer2dealer, playing_field2playing_field ) VALUES ( 110, 'Y', 'DEALER', 20, 6 , 1000, 3333 ); INSERT INTO limits_and_totals ( limits_and_totals_key, active_flag, rec_class, cap_limit, current_cnt , dealer2dealer, playing_field2playing_field ) VALUES ( 10, 'Y', 'DEALER', 25, 5 , 1000, 1111 ); INSERT INTO limits_and_totals ( limits_and_totals_key, active_flag, rec_class, cap_limit, current_cnt , dealer2dealer, playing_field2playing_field ) VALUES ( 20, 'Y', 'DEALER', 20, 6 , 1000, 2222 ); INSERT INTO limits_and_totals ( limits_and_totals_key, active_flag, rec_class, cap_limit, current_cnt , dealer_ser2dealer_service, playing_field2playing_field ) VALUES ( 30, 'Y', 'DEALER_SERVICE', 14, 3 , 2202, 1111 ); INSERT INTO limits_and_totals ( limits_and_totals_key, active_flag, rec_class, cap_limit, current_cnt , dealer_ser2dealer_service, playing_field2playing_field ) VALUES ( 40, 'N', 'DEALER_SERVICE', 22, 5 , 2202, 1111 ); INSERT INTO limits_and_totals ( limits_and_totals_key, active_flag, rec_class, cap_limit, current_cnt , dealer_ser2dealer_service, where_clause, playing_field2playing_field ) VALUES ( 50, 'Y', 'PRODUCT', 5, 2 , 2200, 'number_floors = 1', 1111 ); INSERT INTO limits_and_totals ( limits_and_totals_key, active_flag, rec_class, cap_limit, current_cnt , dealer_ser2dealer_service, where_clause, playing_field2playing_field ) VALUES ( 60, 'Y', 'PRODUCT', 4, 1 , 2200, 'number-Floors=2', 1111 ); COMMIT ; |
No comments:
Post a Comment