

--
--    Licensed Materials - Property of IBM
--    
--    BI and PM: JSM
--    
--    (c) Copyright IBM Corp. 2003, 2010.
--    
--    US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
--    


-------------------------------------------------------------------------------
--	NC3_UPDATE_SYBASE.SQL			*	DO NOT EDIT	 *			Data Store Version: 3		 
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
--$100-- backup create begin
-------------------------------------------------------------------------------

CREATE TABLE X_DATAMAPPING(
   PK_DM_ID                      INTEGER NOT NULL,
   NAME                          NVARCHAR(255) NULL,
   DESCRIPTION                   NVARCHAR(1000) NULL,
   SQL_STRING                    TEXT NULL,
   FK_DS_ID                      INTEGER NULL,
   LASTMOD_BY                    CHAR(40) NULL,
   LASTMOD_TIME                  DATETIME DEFAULT GETDATE() NOT NULL,
   MOD_CNT                       INTEGER DEFAULT (-1) NOT NULL,
   FK_PPDS_ID                    INTEGER NULL,
   FK_COGNOSDS_ID                INTEGER NULL,
   URL                           VARCHAR(2024) NULL,
   NIC                           VARCHAR(2024) NULL
)
go

CREATE TABLE X_NEWSITEMS_NCOBJECTS(
   FK_NCID                       INTEGER NOT NULL,
   NID                           CHAR(32) NOT NULL,
   VID                           CHAR(32) NULL,
   TYPE                          SMALLINT NULL
)
go

-------------------------------------------------------------------------------
--$110-- backup inserts begin
-------------------------------------------------------------------------------

INSERT INTO X_DATAMAPPING (PK_DM_ID,NAME,DESCRIPTION,SQL_STRING,FK_DS_ID,
LASTMOD_BY,LASTMOD_TIME,MOD_CNT,FK_PPDS_ID,FK_COGNOSDS_ID,URL ,NIC)
SELECT PK_DM_ID,NAME,DESCRIPTION,SQL_STRING,FK_DS_ID,LASTMOD_BY,LASTMOD_TIME,
MOD_CNT,FK_PPDS_ID,FK_COGNOSDS_ID,URL,NIC FROM NC_DATAMAPPING
go

INSERT INTO X_NEWSITEMS_NCOBJECTS (FK_NCID,NID,VID,TYPE) 
	SELECT FK_NCID,NID,VID,TYPE FROM R_NEWSITEMS_NCOBJECTS
go

-------------------------------------------------------------------------------
--$120-- foreign keys drop begin
-------------------------------------------------------------------------------

ALTER TABLE NC_DATAMAPPING
   DROP CONSTRAINT F9N_DATAMAPPING
go

ALTER TABLE NC_DATAMAPPING
   DROP CONSTRAINT F5N_DATAMAPPING
go

-------------------------------------------------------------------------------
--$130-- views drop begin
-------------------------------------------------------------------------------

DROP VIEW V_BIE_COMPLETE
go

-------------------------------------------------------------------------------
--$140-- tables drop begin
-------------------------------------------------------------------------------

DROP TABLE NC_DATAMAPPING
go

DROP TABLE R_NEWSITEMS_NCOBJECTS
go

-------------------------------------------------------------------------------
--$200-- recreate tables begin
-------------------------------------------------------------------------------

CREATE TABLE NC_ACCMAN_TEMP_STORE(
   TMP_ID                        INTEGER NOT NULL,
   ACCMAN_UID                    NUMERIC(18) NOT NULL,
   USER_NAME                     VARCHAR(100) NULL,
   EMAIL_ADDRESS                 VARCHAR(100) NULL,
   DESCRIPTION                   VARCHAR(2000) NULL
)
go


CREATE TABLE NC_DATAMAPPING(
   PK_DM_ID                      INTEGER NOT NULL,
   NAME                          NVARCHAR(440) NULL,
   DESCRIPTION                   NVARCHAR(1000) NULL,
   SQL_STRING                    TEXT NULL,
   FK_DS_ID                      INTEGER NULL,
   LASTMOD_BY                    CHAR(40) NULL,
   LASTMOD_TIME                  DATETIME DEFAULT GETDATE() NOT NULL,
   MOD_CNT                       INTEGER DEFAULT (-1) NOT NULL,
   FK_PPDS_ID                    INTEGER NULL,
   FK_COGNOSDS_ID                INTEGER NULL,
   URL                           VARCHAR(2048) NULL,
   NIC                           TEXT NULL
)
go


CREATE TABLE NC_RUNTIME_TICKET(
   TICKET_ID                     NVARCHAR(32) NOT NULL
)
go


CREATE TABLE R_NEWSITEMS_NCOBJECTS(
	NID             	           	CHAR(32) NOT NULL, 
   FK_NCID                       INTEGER NOT NULL,
   TYPE									SMALLINT NULL
)
go


CREATE TABLE R_NEWSVIEWS_NCOBJECTS(
	VID 									CHAR(32) NOT NULL,
	NID									CHAR(32) NOT NULL,
	FK_NCID								INTEGER NOT NULL
)
go

-------------------------------------------------------------------------------
--$201-- indices create begin
-------------------------------------------------------------------------------

CREATE INDEX I1N_AGGREGATE_RULE ON NC_AGGREGATE_RULE_TREE (
   FK_PARENT_ID)
go

CREATE INDEX I1N_BIETOPIC ON NC_BIETOPIC (
   FK_DMT_ID)
go

CREATE INDEX I2N_BIETOPIC ON NC_BIETOPIC (
   FK_BIE_ID)
go

CREATE INDEX I1N_DATAMAPPING ON NC_DATAMAPPING (
   FK_PPDS_ID)
go

CREATE INDEX I2N_DATAMAPPING ON NC_DATAMAPPING (
   FK_DS_ID)
go

CREATE INDEX I1N_DYN_DELIVERY_I ON NC_DYN_DELIVERY_INFO_STRUCT (
   FK_DYNAMIC_RECIPIENT_STRUCT)
go

CREATE INDEX I7N_EXECUTABLE ON NC_EXECUTABLE (
   FK_TASK_ID)
go

CREATE INDEX I1N_MESSAGELINE ON NC_MESSAGELINE (
   FK_MESSAGESTRUCT_ID)
go

CREATE INDEX I1N_MESSAGESTRUCT ON NC_MESSAGESTRUCT (
   FK_DYNREC_ID)
go

CREATE INDEX I1N_MESSAGE_ATTACH ON NC_MESSAGE_ATTACHMENT (
   FK_MESSAGE_ID)
go

CREATE INDEX I1N_MSGDUPLICATION ON NC_MSGDUPLICATION_STORE (
   FK_MESSAGESTRUCT_ID)
go

CREATE INDEX I1N_ION_TEMP_STORE ON NC_MSGDUPLICATION_TEMP_STORE (
   SESSION_ID)
go

CREATE INDEX I1R_NEWSITEMS_NCOB ON R_NEWSITEMS_NCOBJECTS (
   TYPE)
go

CREATE INDEX I2R_NEWSITEMS_NCOB ON R_NEWSITEMS_NCOBJECTS (
   FK_NCID)
go

CREATE INDEX I1R_NEWSVIEWS_NCOB ON R_NEWSVIEWS_NCOBJECTS (
   FK_NCID)
go

CREATE INDEX I2R_NEWSVIEWS_NCOB ON R_NEWSVIEWS_NCOBJECTS (
   NID)
go

CREATE INDEX I1N_RULE ON NC_RULE (
   FK_BIE_ID)
go

CREATE INDEX I1N_TASKSCHEDULE ON NC_TASKSCHEDULE (
   FK_TASK_ID)
go

CREATE INDEX I7R_DS_MACHINE_DSP ON R_DS_MACHINE_DSPOOL (
   FK_MACHINE_ID)
go

CREATE INDEX I2R_DS_MACHINE_DSP ON R_DS_MACHINE_DSPOOL (
   FK_DSPOOL_ID)
go

CREATE INDEX I1R_MESSAGESTRUCT_ ON R_MESSAGESTRUCT_TASK (
   FK_MESSAGESTRUCT_ID)
go

CREATE INDEX I1R_MSGSTRUCT_AVOI ON R_MSGSTRUCT_AVOIDTOPICS (
   FK_MS_ID)
go

CREATE INDEX I1R_MSGSTRUCT_RECI ON R_MSGSTRUCT_RECIPIENT_DELIVINF (
   FK_MESSAGESTRUCT_ID)
go

CREATE INDEX I1R_RECIPIENT_ACCM ON R_RECIPIENT_ACCMANUSERID (
   F_RECIPIENTID)
go

CREATE INDEX I1R_TASKSCHEDULE_S ON R_TASKSCHEDULE_SCHEDULE (
   FK_TASKSCHEDULE_ID)
go

-------------------------------------------------------------------------------
--$202-- primary keys add begin
-------------------------------------------------------------------------------

ALTER TABLE NC_ACCMAN_TEMP_STORE
   ADD CONSTRAINT PN_ACCMAN_TEMP_STO PRIMARY KEY (ACCMAN_UID,TMP_ID)
go


ALTER TABLE NC_DATAMAPPING
   ADD CONSTRAINT PN_DATAMAPPING PRIMARY KEY (PK_DM_ID)
go


ALTER TABLE R_NEWSITEMS_NCOBJECTS
   ADD CONSTRAINT PR_NEWSITEMS_NCOBJ PRIMARY KEY (NID)
go


ALTER TABLE R_NEWSVIEWS_NCOBJECTS
   ADD CONSTRAINT PR_NEWSVIEWS_NCOBJ PRIMARY KEY (VID)
go

ALTER TABLE R_RULE_PARAMETER
   ADD CONSTRAINT PR_R_RULE_PARAM PRIMARY KEY (FK_TASK_ID)
go

-------------------------------------------------------------------------------
--$203-- alter tables begin
-------------------------------------------------------------------------------

ALTER TABLE NC_DELIVERYADDRESS
	ADD ADDRESS_TYPE INTEGER
go
	
ALTER TABLE NC_MESSAGESTRUCT ALTER COLUMN SUBJECT VARCHAR(210)
go

ALTER TABLE R_MSGSTRUCT_RECIPIENT_DELIVINF
	ADD DATE_ADDED	NUMERIC(18)
go

-------------------------------------------------------------------------------
--$210-- views create begin
-------------------------------------------------------------------------------

CREATE VIEW V_ACCMANUSER
AS SELECT 
   NC_ACCMAN_TEMP_STORE.TMP_ID, 
   NC_ACCMAN_TEMP_STORE.ACCMAN_UID, 
   NC_ACCMAN_TEMP_STORE.USER_NAME, 
   NC_ACCMAN_TEMP_STORE.DESCRIPTION, 
   R_RECIPIENT_ACCMANUSERID.F_RECIPIENTID, 
   NC_ACCMAN_TEMP_STORE.EMAIL_ADDRESS
FROM 
	NC_ACCMAN_TEMP_STORE LEFT OUTER JOIN R_RECIPIENT_ACCMANUSERID 
ON 
	NC_ACCMAN_TEMP_STORE.ACCMAN_UID = R_RECIPIENT_ACCMANUSERID.ACCMAN_UID
go



CREATE VIEW V_BIE_COMPLETE
AS 
SELECT 
	 NC_BIE.PK_BIE_ID AS BIE_PK_ID,  
    NC_BIETOPIC.FK_BIE_ID AS BIETOPIC_FK_BIE_ID, 
    NC_BIETOPIC.FK_CALC_ID AS BIETOPIC_FK_CALC_ID, 
    NULL AS CALC_LINK_PK_ID,
    NC_BIE.NAME AS BIE_NAME, 
    NC_BIE.DESCRIPTION AS BIE_DESCRIPTION, 
    NC_BIE.MOD_CNT AS BIE_MODCOUNT, 
    NC_BIETOPIC.NAME AS BIETOPIC_NAME, 
    NC_BIETOPIC.DESCRIPTION AS BIETOPIC_DESCRIPTION, 
    NC_BIETOPIC.PK_BIETOPIC_ID AS BIETOPIC_PK_ID, 
    NC_BIETOPIC.TYPE AS BIETOPIC_TYPE, 
    NC_BIETOPIC.MODCOUNT AS BIETOPIC_MODCOUNT, 
    NC_BIETOPIC.FK_DMT_ID AS BIETOPIC_FK_DMT_ID,
    NC_DMTOPIC.OBJ_TYPE AS TOPIC_OBJ_TYPE, 
    NC_DMTOPIC.PK_TOPIC_ID AS DMT_PK_ID, 
    NC_DMTOPIC.NAME AS DMT_NAME, 
    NC_DMTOPIC.DESCRIPTION AS DMT_DESCRIPTION,    
    NC_DMTOPIC.COLUMN_NAME AS DMT_COLUMN_NAME, 
    NC_DMTOPIC.DRE_COLUMN_NAME AS DMT_DRE_COLUMN_NAME,
    NC_DMTOPIC.TABLE_NAME AS DMT_TABLE_NAME, 
    NC_DMTOPIC.COLUMN_TYPE AS DMT_COLUMN_TYPE, 
    NC_DMTOPIC.FK_DM_ID AS DMT_FK_DM_ID, 
    NC_DMTOPIC.PP_TOPIC_VALUE AS DMT_PP_VALUE,   
    ' ' AS CALC_LINK_NAME, 
    ' ' AS CALC_LINK_DESCRIPTION, 
    NULL AS CALC_LINK_LHS_TOPIC_ID, 
    ' ' AS CALC_LINK_FK_RHSTYPE, 
    ' ' AS CALC_LINK_OPERATOR, 
    ' ' AS CALC_LINK_NUMERIC_LITERAL, 
    ' ' AS CALC_LINK_STRING_LITERAL, 
    ' ' AS CALC_LINK_DATE_LITERAL, 
    NULL AS CALC_LINK_FK_BIETOPIC_ID, 
    NC_RULE.FK_BIE_ID AS RULE_FK_BIE_ID, 
    NC_RULE.PK_RULE_ID AS RULE_PK_ID, 
    NC_RULE.NAME AS RULE_NAME, 
    NC_RULE.DESCRIPTION AS RULE_DESCRIPTION, 
    NC_RULE.FK_RULETYPE AS RULE_FK_RULETYPE, 
    NC_RULE.DATE_LITERAL AS RULE_DATE_LITERAL, 
    NC_RULE.STRING_LITERAL AS RULE_STRING_LITERAL, 
    NC_RULE.NUMERIC_LITERAL AS RULE_NUMERIC_LITERAL, 
    NC_RULE.LHS_TOPIC_ID AS RULE_LHS_TOPIC_ID, 
    NC_RULE.OPERATOR AS RULE_OPERATOR, 
    NC_RULE.FK_RHSTYPE AS RULE_FK_RHSTYPE, 
    NC_RULE.RHS_TOPIC_ID AS RULE_RHS_TOPIC_ID, 
    NC_RULE.RHS_OPTION AS RULE_RHS_OPTION, 
    NC_RULE.RHS_BIE_ID AS RULE_RHS_BIE_ID
FROM 
    NC_BIETOPIC RIGHT  OUTER JOIN  NC_BIE 
        LEFT OUTER JOIN NC_RULE ON NC_BIE.PK_BIE_ID = NC_RULE.FK_BIE_ID 
    ON NC_BIETOPIC.FK_BIE_ID = NC_BIE.PK_BIE_ID 
    LEFT OUTER JOIN NC_DMTOPIC ON  NC_BIETOPIC.FK_DMT_ID = NC_DMTOPIC.PK_TOPIC_ID
go

-------------------------------------------------------------------------------
--$220-- server data inserts begin
-------------------------------------------------------------------------------

INSERT INTO NC_DB_VERSION (DB_VERSION_ID,BUILD) VALUES (3,'build no.')
go

-------------------------------------------------------------------------------
--$230-- restore inserts begin
-------------------------------------------------------------------------------

INSERT INTO NC_DATAMAPPING (PK_DM_ID,NAME,DESCRIPTION,SQL_STRING,FK_DS_ID,
LASTMOD_BY,LASTMOD_TIME,MOD_CNT,FK_PPDS_ID,FK_COGNOSDS_ID,URL ,NIC)
SELECT PK_DM_ID,NAME,DESCRIPTION,SQL_STRING,FK_DS_ID,LASTMOD_BY,LASTMOD_TIME,
MOD_CNT,FK_PPDS_ID,FK_COGNOSDS_ID,URL,NIC FROM X_DATAMAPPING
go


INSERT INTO R_NEWSITEMS_NCOBJECTS 
	SELECT NID,FK_NCID,TYPE FROM X_NEWSITEMS_NCOBJECTS
	WHERE TYPE IN (905,906,907,908)
go

INSERT INTO R_NEWSVIEWS_NCOBJECTS
 	SELECT VID,NID,FK_NCID FROM X_NEWSITEMS_NCOBJECTS
	WHERE TYPE NOT IN (905,906,907,908)
go

-------------------------------------------------------------------------------
--$240-- foreign keys add begin
-------------------------------------------------------------------------------

ALTER TABLE NC_DATAMAPPING
   ADD CONSTRAINT F9N_DATAMAPPING FOREIGN KEY (FK_PPDS_ID)
      REFERENCES NC_COGNOS_DS
go


ALTER TABLE NC_DATAMAPPING
   ADD CONSTRAINT F5N_DATAMAPPING FOREIGN KEY (FK_DS_ID)
      REFERENCES NC_CONFIG_DS
go


ALTER TABLE R_NEWSVIEWS_NCOBJECTS
   ADD CONSTRAINT F2R_NEWSVIEWS_NCOB FOREIGN KEY (NID)
      REFERENCES R_NEWSITEMS_NCOBJECTS
go

-------------------------------------------------------------------------------
--$250-- backup drop begin
-------------------------------------------------------------------------------

DROP TABLE X_DATAMAPPING
go


DROP TABLE X_NEWSITEMS_NCOBJECTS
go

-------------------------------------------------------------------------------
--$260-- end sql by function
-------------------------------------------------------------------------------
--	END NC3_UPDATE_SYBASE.SQL			*	DO NOT EDIT	 * 		 
-------------------------------------------------------------------------------
