Sei sulla pagina 1di 26

DB SCRIPT RULE GUIDE FOR

INTEGRAL
For Base Integral Database

Prepared by
DXC Technology

Version 0.4: 20 April 2018


Document ID: Integral DB Script Rule_Updated.doc
© 2018, DXC Technology

The document contains proprietary and confidential information and is provided on the basis
that such information will be confidential. This work is copyright. No part may be reproduced
or transmitted in any form or by any means, electronic, mechanical, photocopying, recording
or otherwise or stored in any retrieval system of any nature, without prior written permission
of DXC Technology.

The information in this document is believed to be accurate in all respects. The company and
its divisions and subsidiaries cannot, however, assume responsibility for any consequences
resulting from the use of this information. The information contained herein is subject to
changes; revisions or new editions may be issued to incorporate such changes.

DXC Technology
1 Depot Close
#03-01Tower A
Singapore 109841

Telephone +65 6809 9000


Facsimile +65 6809 9084

DXC Technology is created by the merger of CSC and the Enterprise Services business of
Hewlett Packard Enterprise. Both CSC and HPE Enterprise Services were known for
evolving to keep pace with the ever-changing world of technology – and for offering clients a
fresh perspective built on a rich heritage of innovation and industry-leading services.

Version 0.4: 20 April 2018


Document ID: Integral DB Script Rule_Updated.doc
Publication History

Date Version Description Author


July 27, 2017 0.1 Initial draft Luo Bin
Added the content of rule from Trang Nguyen
July 27, 2017 0.2 Luo Bin
and Goyal, Amar
Adding the content for QUOTED_IDENTIFIER
July 28, 2017 0.3 Luo Bin
setting for MSSQL
June 20, 2018 0.4 Adding chapter 3.4, 3.5, 3.6, 4.4 Luo Bin

Version 0.4: 20 April 2018


Document ID: Integral DB Script Rule_Updated.doc
Table of Contents
1. INTRODUCTION ............................................................................................................ 6

2. PURPOSE .......................................................................................................................... 6

3. COMMON ERRORS FOR ORACLE............................................................................ 7


3.1 ADD THE “/” AFTER ANY PL/SQL BLOCK. ............................................................................................. 7

3.2 REMOVE THE BLANK LINE DURING SCRIPT FOR “CREATE TABLE” OR “CREATE VIEW”. .......................... 7

3.3 ADD THE “COMMIT” AFTER ANY DML SCRIPT. ...................................................................................... 7

3.4 USE “UTF-8” ENCODING FORMAT INSTEAD “UNICODE BIG ENDIAN” WHEN USING MUTLI-LANGUAGE
DATA, AND MAKE THE FIRST LINE IS BLANK LINE. ........................................................................................... 8

3.5 INSERT DATA FOR GENAREA OF ITEMPF . ......................................................................................... 8

3.6 ADD THE “;” AFTER ANY WHOLE SCRIPT. ............................................................................................... 9

3.7 RESET THE SEQUENCE CURRENT VALUE TO INSERT NEW DATA WHEN MET ERROR: ORA-00001 . ....... 9

4. COMMON ERRORS FOR MSSQL............................................................................. 10


4.1 ADD SCHEMA NAME TO ALL THE OBJECTS. .......................................................................................... 10

4.2 ADD “GO” AFTER ANY DDL SCRIPT ..................................................................................................... 10

4.3 ADD “QUOTED_IDENTIFIER” BEFORE SCRIPT WITH “CREATE、UPDATE、INSERT AND


DELETE” SQL .............................................................................................................................................. 10

4.4 REMOVE “” FOR ANY OBJECT NAME OR COLUMN NAME ....................................................................... 11

5. DML/DDL TEMPLATE FOR ORACLE .................................................................... 11


5.1 DML STATEMENT. .............................................................................................................................. 11

5.2 DDL STATEMENT, PLEASE ADD COMMENTS AT THE SAME TIME .......................................................... 12
5.2.1 DDL for “CREATE A NEW TABLE” ................................................................................... 12
5.2.2 DDL for “DROP TABLE” .................................................................................................... 13
5.2.3 DDL for “ADD NEW COLUMN TO TABLE” ..................................................................... 14
5.2.4 DDL for “MODIFY EXISTING COLUMN” ........................................................................ 14
5.2.5 DDL for “DROP EXISTING COLUMN”............................................................................. 15
5.2.6 DDL for “RENAME A COLUMN in an existing table” ....................................................... 16
5.2.7 DDL for “ADD NEW INDEX” ............................................................................................. 16
5.3 VIEW STATEMENT. ............................................................................................................................. 17

5.4 TRIGGER STATEMENT. ...................................................................................................................... 17

5.5 STORED PROCEDURE STATEMENT. ............................................................................................... 18

5.6 FUNCTIONS STATEMENT. ................................................................................................................. 18

5.7 SEQUENCE STATEMENT. .................................................................................................................. 18

5.8 TEST ORACLE SCRIPTS USING “SQLPLUS” ......................................................................................... 19


5.8.1 Download Oracle Client from ORACLE website ................................................................. 19
5.8.2 Open Oracle “Net Manager” to configure DB service name .............................................. 19
Version 0.4: 20 April 2018
Document ID: Integral DB Script Rule_Updated.doc Page 4
5.8.3 Open a Dos window to execute your script .......................................................................... 20

6. DML/DDL TEMPLATE FOR MSSQL........................................................................ 20


6.1 DML STATEMENT. .............................................................................................................................. 20

6.2 DDL STATEMENT. ............................................................................................................................... 21


6.2.1 DDL for “CREATE A NEW TABLE” ................................................................................... 21
6.2.2 DDL for “DROP A TABLE” ................................................................................................ 22
6.2.3 DDL for “ADD NEW COLUMN” ........................................................................................ 22
6.2.4 DDL for “MODIFY EXISTING COLUMN” ........................................................................ 23
6.2.5 DDL for “DROP A EXISTING COLUMN” ......................................................................... 23
6.3 VIEW STATEMENT. ............................................................................................................................. 24

6.4 TRIGGER STATEMENT. ...................................................................................................................... 24

6.5 STORED PROCEDURE STATEMENT. ............................................................................................... 25

6.6 FUNCTIONS STATEMENT. ................................................................................................................. 25

6.7 INDEX STATEMENT. ........................................................................................................................... 26

Version 0.4: 20 April 2018


Document ID: Integral DB Script Rule_Updated.doc Page 5
1. Introduction
There are many script which is submit by developers during developing, so we should has a
DB script rule for these script to decrease the script errors for daily DB script deploy.

2. Purpose
The purpose of this document is to define the DB RULES for Integral database script
which is submitted by developers.

Version 0.4: 20 April 2018


Document ID: Integral DB Script Rule_Updated.doc Page 6
3. Common Errors for ORACLE

3.1 Add the “/” after any PL/SQL block.


For example:

DECLARE
cnt number(2,1) :=0;
BEGIN
SELECT count(*) into cnt FROM USER_TAB_COLUMNS where table_name
= 'PREQPF' and column_name ='ZTXINVDT' ;
IF ( cnt > 0 ) THEN
EXECUTE IMMEDIATE (' ALTER TABLE PREQPF MODIFY ZTXINVDT
NUMBER(8) ');
END IF;
END;
/

Since the automatic DB script Execution is using ORACLE base tools:


SQLPLUS, it will request “/” to confirm if this script is executed.
Otherwise the script execution will show success, but actually it does not
execute in database.

3.2 Remove the blank line during script for “create table” or
“create view”.
For example:

CREATE TABLE VM1DTA.ZFNETREQPF


(
UNIQUE_NUMBER NUMBER(18,0) NOT NULL,
LABEL_NUM NUMBER(2,0) NOT NULL,
STATUS VARCHAR2(20 CHAR) NOT NULL,

USRPRF VARCHAR2(10 CHAR),


DATIME TIMESTAMP(6) DEFAULT SYSDATE,
CONSTRAINT PK_ZFNETREQPF PRIMARY KEY(UNIQUE_NUMBER)
);

The above script will fail because the blank line during “create table”.
And this script will success in MSSQL !

3.3 Add the “commit” after any DML script.


For example:

INSERT INTO VM1DTA.USRDPF (UNIQUE_NUMBER,USERID,……


Commit;
Version 0.4: 20 April 2018
Document ID: Integral DB Script Rule_Updated.doc Page 7
Especially if there are many insert rows in script, immediate commit will make
the system faster.

Note:
Commit is not needed for DDL script since the database will commit
automatically.

3.4 Use “UTF-8” Encoding format instead “Unicode big


endian” when using mutli-language data, and make the
first line is blank line.

For auto-exec script(using sqlplus to execute), “Unicode big endian” could not
be recognized and show ERRORS like this:
SQL> SP2-0042: unknown command "þÿ" - rest of line ignored.
After this error, all the following script will be not be executed!

So please use the following method to avoid these errors:


1. Use “UTF-8” Encoding format as the script file format
2. Make the first line is blank line
since “UTF-8” Encoding format will still generate some little error for the first
line, so please make the first line is blank line, then the script will pop up a error
like the following, but the script will continue to execute normally:
SQL> SP2-0042: unknown command "" - rest of line ignored.
(The above error could be ignored in script execution)

3.5 Insert data for GENAREA of ITEMPF .

For ORACLE, the datatype of GENAREA of ITEMPF is:

GENAREA RAW(2000)

1. So please make sure you data size for GENAREA of ITEMPF is less than
2000.
Usually you could just remove the last 2000 characters directly when you
convert the MSSQL data to ORACLE data.

For example:
Just change the data:
'20202020E8A2ABE4BF9DE4BABAE79BAEE5898DE698AFE590A6E59CA8
E6B2BBE79697E4B8ADEFBC8CE69C8DE88DAFE4B8ADEFBC8CE69C89
E5A484E696B9E68896E88085E585B6E5AE83E79A84EFBC8CE698AFE59
0A6E59CA8E7AD89E5BE85E4BBBBE4BD95E8AF8AE79697203F20202020
20202020202020202020202020202020202020202020202020202020202020
2020202020202020202020202020202020202020‘
To

Version 0.4: 20 April 2018


Document ID: Integral DB Script Rule_Updated.doc Page 8
'20202020E8A2ABE4BF9DE4BABAE79BAEE5898DE698AFE590A6E59CA8E6B2BBE
79697E4B8ADEFBC8CE69C8DE88DAFE4B8ADEFBC8CE69C89E5A484E696B9E68
896E88085E585B6E5AE83E79A84EFBC8CE698AFE590A6E59CA8E7AD89E5BE85E
4BBBBE4BD95E8AF8AE79697203F’

2. Decrease your single line less than 2499 for your sql script when you met
the following errors:
SP2-0027: Input is too long (> 2499 characters) - line ignored

3.6 Add the “;” after any whole script.


Please add “;” after any whole script.
Otherwise ORACLE will not execute it really, just ignored.

For example:
ALTER TABLE ACAGPF1 ADD JOBNM CHAR(10)
The above script will not execute.

ALTER TABLE ACAGPF1 ADD JOBNM CHAR(10);


The above script will execute immediately.

3.7 Reset the sequence current value to insert new data


WHEN met error: ORA-00001 .

Sometimes, when you insert new data to table, there are errors like this:
ORA-00001: unique constraint (VM1DTA.PK_BUPAPF) violated

The error happens because the imported dump file is generated when many
developers access data, this will make the table data(mainly
UNIQUE_NUMBER column value) is not synchronous with sequence current
value.

The step to fix this:


1. Find the max value for current table data:
Select max(UNIQUE_NUMBER) from BUPAPF.
Get the max value: MAX_XXX
2. Find the sequence what the table used:
1) select trigger_name from all_triggers where table_name= BUPAPF;
get the trgger name: 'TR_XXX'

2) select text from all_source where type='TRIGGER' AND


name='TR_XXX';
get the sequence name from the trigger content: SEQ_XXX

3) reset the sequence like the following:


drop SEQUENCE VM1DTA. SEQ_XXX;
CREATE SEQUENCE VM1DTA.SEQ_XXX
START WITH MAX_XXX
INCREMENT BY 1
Version 0.4: 20 April 2018
Document ID: Integral DB Script Rule_Updated.doc Page 9
MAXVALUE 9999999999999999999999999999
MINVALUE 1
NOCYCLE
CACHE 20
NOORDER;

4. Common Errors for MSSQL

4.1 Add schema name to all the objects.


For example, add “vm1dta” before tablename.
GMHIPF => VM1DTA.GMHIPF

Otherwise the object will be created in system default schema:dbo and this
will make the database confused and sometime it will cause errors.

4.2 Add “go” after any DDL script


This step is very important!
Otherwise sometime your script will not execute correctly.

4.3 Add “QUOTED_IDENTIFIER” before script with


“CREATE、UPDATE、INSERT and DELETE” sql
For the tables which use with indexed views and/or indexes on computed
columns and/or filtered indexes and/or query notifications and/or XML data
type methods and/or spatial index operations, the parameter:
QUOTED_IDENTIFIER should be on.
But the default value for this parameter is off.

So when you use sql with Update/CREATE/INSERT/DELETE sql, please add


the following script around your script:
SET QUOTED_IDENTIFIER ON
GO
.......
SET QUOTED_IDENTIFIER OFF
GO
Note: “…….” is your original script.

For example:
SET QUOTED_IDENTIFIER ON
GO
IF EXISTS (SELECT 1 FROM VM1DTA.CLBADDB WHERE
BANKACCKEY='123456789')
UPDATE VM1DTA.CLBADDB set BANKACCKEY = '58' WHERE
BANKACCKEY = '123456789'
GO
Version 0.4: 20 April 2018
Document ID: Integral DB Script Rule_Updated.doc Page 10
SET QUOTED_IDENTIFIER OFF
GO

Otherwise it will cause errors like this:


Msg 1934, Level 16, State 1, Server CSCDBNOI014, Line 2
UPDATE failed because the following SET options have incorrect settings:
'QUOTED_IDENTIFIER'. Verify that SET options are correct for use with indexed
views and/or indexes on computed columns and/or filtered indexes and/or query
notifications and/or XML data type methods and/or spatial index operations.
"SQL script is failed"

4.4 Remove “” for any object name or column name


For MSSQL scripts, there are errors like this when executing with auto-exec:
ALTER VIEW "VM1DTA"."HITS" ("UNIQUE_NUMBER", "CHDRCOY",
"CHDRNUM", "LIFE", "COVERAGE", "RIDER", "PLNSFX", "ZINTBFND",
"ZCURPRMBAL", "ZLSTUPDT", "FUNDPOOL","USRPRF", "JOBNM",
"DATIME") AS
Msg 102, Level 15, State 1, Server CSCDBNOI014, Line 1
Incorrect syntax near 'VM1DTA'.

Please do not use “” to specify the object name or column name, use [] or just
blank is enough, otherwise your script will not execute correctly.

5. DML/DDL Template for ORACLE


/*
Created by: Trang Nguyen
Created Date: Aug 18 2016
*/
/*
Description: This template is used for creating any DML statement ON ORACLE for daily
build. The purpose is to reduce duplicate data when insert/update data in a table
Developer must follow the systax on this file whenever create any DML statement
*/

5.1 DML Statement.

--INSERT statement must be check existing data inadvance


INSERT INTO
VM1DTA.HELPPF(HELPPFX,HELPCOY,HELPLANG,HELPTYPE,HELPPROG,HELPIT
EM,HELPSEQ,TRANID,VALIDFLAG,HELPLINE,USRPRF,JOBNM,DATIME)
VALUES ('HP','','E','S','SQ956', '','1','','1','This screen is used to create/ maintain
','UNDERWR1','UNDERWR1',CURRENT_TIMESTAMP )

-- ==> REPLACE BY this

Version 0.4: 20 April 2018


Document ID: Integral DB Script Rule_Updated.doc Page 11
DECLARE
cnt number(2,1) :=0;
BEGIN
SELECT count(1) INTO cnt
FROM VM1DTA.HELPPF h
WHERE h.HELPPFX = 'HP' AND h.HELPLANG='E' AND h.HELPTYPE = 'S'
AND h.HELPPROG ='SQ956' AND h.HELPSEQ = '1' AND h.VALIDFLAG = '1'
AND h.HELPLINE = 'This screen is used to create/ maintain ' ;
IF ( cnt = 0 ) THEN
INSERT INTO
VM1DTA.HELPPF(HELPPFX,HELPCOY,HELPLANG,HELPTYPE,HELPPROG,HELPIT
EM,HELPSEQ,TRANID,VALIDFLAG,HELPLINE,USRPRF,JOBNM,DATIME)
VALUES ('HP','','E','S','SQ956', '','1','','1','This screen is used to create/ maintain
','UNDERWR1','UNDERWR1',CURRENT_TIMESTAMP );
END IF;
END;
/

5.2 DDL Statement, please add comments at the same time


/*
Description: This template is used for creating any DDL statement ON MSSQL for
daily build. The purpose is to reduce error when execute DB script change on daily
build
Developer must follow the systax on this file whenever create any DDL
*/

5.2.1 DDL for “CREATE A NEW TABLE”

-------------------------------------TABLE -------------------------------------
-- -- -- -- -- -- -- -- CREATE A NEW TABLE -- -- -- -- -- -- -- --
DECLARE
cnt number(2,1);
begin
SELECT count(*) into cnt FROM user_tables where table_name = '<table_name>' ;
IF ( cnt > 0 ) THEN
EXECUTE IMMediate ('CREATE TABLE table_name
( { <column_definition> } ,...n
)'
);
EXECUTE IMMEDIATE ('COMMENT ON TABLE table_name IS ''………''');

Version 0.4: 20 April 2018


Document ID: Integral DB Script Rule_Updated.doc Page 12
EXECUTE IMMEDIATE ('COMMENT ON COLUMN table_name.column_name IS
''……..''');
end if ;
end;
/

-- -- -- -- -- -- -- -- ---Example
DECLARE
cnt number(2,1) :=0;
BEGIN
SELECT count(*) into cnt FROM user_tables where table_name = 'ACAGPF1' ;
IF ( cnt = 0 ) THEN
EXECUTE IMMediate (
' CREATE TABLE VM1DTA.ACAGPF1
( UNIQUE_NUMBER NUMBER(18,0),
CONSTRAINT PK_ACAGPF1 PRIMARY KEY (UNIQUE_NUMBER)
)'
);
EXECUTE IMMEDIATE ('COMMENT ON TABLE ACAGPF1 IS ''test''');
EXECUTE IMMEDIATE ('COMMENT ON COLUMN VM1DTA.ACAGPF1.
UNIQUE_NUMBER IS ''Contract Number''');
END IF;
END;
/

5.2.2 DDL for “DROP TABLE”

--DROP TABLE
DECLARE
cnt number(2,1);
begin
SELECT count(*) into cnt FROM user_tables where table_name = 'table_name' ;
IF ( cnt > 0 ) THEN
EXECUTE IMMEDIATE ('DROP TABLE table_name ');
end if ;
end;
/

Version 0.4: 20 April 2018


Document ID: Integral DB Script Rule_Updated.doc Page 13
5.2.3 DDL for “ADD NEW COLUMN TO TABLE”

-- -- -- -- -- -- -- -- ADD NEW COLUMN TO TABLE -- -- -- -- -- -- -- --


DECLARE
cnt number(2,1) :=0;
BEGIN
SELECT count(*) into cnt FROM USER_TAB_COLUMNS where table_name =
'table_name' and column_name ='col_name' ;
IF (cnt = 0 ) THEN
EXECUTE IMMEDIATE (' ALTER TABLE table_name ADD col_name column-
definition ');
EXECUTE IMMEDIATE ('COMMENT ON COLUMN table_name. col_name IS ''
…….''');
END IF;
END;
/
-- -- -- -- -- -- -- -- --Example
DECLARE
cnt number(2,1) :=0;
BEGIN
SELECT count(*) into cnt FROM USER_TAB_COLUMNS where table_name =
'ACAGPF1' and column_name ='JOBNM' ;
IF ( cnt = 0 ) THEN
EXECUTE IMMEDIATE (' ALTER TABLE ACAGPF1 ADD JOBNM CHAR(10) ');
EXECUTE IMMEDIATE ('COMMENT ON COLUMN ACAGPF1. JOBNM IS ''test ''');
END IF;
END;
/

5.2.4 DDL for “MODIFY EXISTING COLUMN”

-- -- -- -- -- -- -- -- -- -- MODIFY EXISTING COLUMN


DECLARE
cnt number(2,1) :=0;
BEGIN
SELECT count(*) into cnt FROM USER_TAB_COLUMNS where table_name =
'table_name' and column_name ='col_name' ;
IF (cnt > 0 ) THEN
EXECUTE IMMEDIATE (' ALTER TABLE table_name MODIFY col_name column-
definition ');
END IF;
Version 0.4: 20 April 2018
Document ID: Integral DB Script Rule_Updated.doc Page 14
END;
/

--EXAMPLE
DECLARE
cnt number(2,1) :=0;
BEGIN
SELECT count(*) into cnt FROM USER_TAB_COLUMNS where table_name =
'ACAGPF1' and column_name ='JOBNM' ;
IF ( cnt > 0 ) THEN
EXECUTE IMMEDIATE (' ALTER TABLE ACAGPF1 MODIFY JOBNM CHAR(20) ');
END IF;
END;
/

5.2.5 DDL for “DROP EXISTING COLUMN”

-- -- -- -- -- -- -- -- -- -- DROP EXISTING COLUMN


DECLARE
cnt number(2,1) :=0;
BEGIN
SELECT count(*) into cnt FROM USER_TAB_COLUMNS where table_name =
'table_name' and column_name ='col_name' ;
IF (cnt > 0 ) THEN
EXECUTE IMMEDIATE (' ALTER TABLE table_name DROP COLUMN col_name ');
END IF;
END;
/
--EXAMPLE
DECLARE
cnt number(2,1) :=0;
BEGIN
SELECT count(*) into cnt FROM USER_TAB_COLUMNS where table_name =
'ACAGPF1' and column_name ='JOBNM' ;
IF ( cnt > 0 ) THEN
EXECUTE IMMEDIATE (' ALTER TABLE ACAGPF1 DROP COLUMN JOBNM ');
END IF;
END;
/

Version 0.4: 20 April 2018


Document ID: Integral DB Script Rule_Updated.doc Page 15
5.2.6 DDL for “RENAME A COLUMN in an existing table”

-- -- -- -- -- -- -- -- -- -- RENAME A COLUMN in an existing table


DECLARE
cnt number(2,1) :=0;
BEGIN
SELECT count(*) into cnt FROM USER_TAB_COLUMNS where table_name =
'table_name' and column_name ='col_name' ;
IF (cnt > 0 ) THEN
EXECUTE IMMEDIATE (' ALTER TABLE table_name RENAME COLUMN
old_col_name TO new_col_name ');
END IF;
END;
/
--EXAMPLE
DECLARE
cnt number(2,1) :=0;
BEGIN
SELECT count(*) into cnt FROM USER_TAB_COLUMNS where table_name =
'ACAGPF1' and column_name ='JOBNM' ;
IF ( cnt > 0 ) THEN
EXECUTE IMMEDIATE (' ALTER TABLE ACAGPF1 RENAME COLUMN JOBNM
TO JOBNM1 ');
END IF;
END;
/

5.2.7 DDL for “ADD NEW INDEX”

-- -- -- -- -- -- -- -- ADD NEW INDEX -- -- -- -- -- -- -- --


DECLARE
cnt number(2,1) :=0;
BEGIN
SELECT count(*) into cnt FROM USER_INDEXES where index_name =
'index_name';
IF (cnt > 0 ) THEN
EXECUTE IMMEDIATE (' DROP INDEX index_name ');
EXECUTE IMMEDIATE (‘CREATE INDEX index_name on
table_name(column_name1, …’);
ELSE
EXECUTE IMMEDIATE (‘CREATE INDEX index_name on
table_name(column_name1, …’);

Version 0.4: 20 April 2018


Document ID: Integral DB Script Rule_Updated.doc Page 16
END IF;
END;
/
-- -- -- -- -- -- -- -- --Example
DECLARE
cnt number(2,1) :=0;
BEGIN
SELECT count(*) into cnt FROM USER_INDEXES where index_name = ‘CLBADDB';
IF ( cnt > 0 ) THEN
EXECUTE IMMEDIATE (' DROP INDEX CLBADDB ');
EXECUTE IMMEDIATE (‘CREATE INDEX CLBADDB ON VM1DTA.CLBAPF
(CLNTCOY, CLNTNUM, BANKKEY, BANKACCKEY, CLNTPFX) ‘);
ELSE
EXECUTE IMMEDIATE (‘CREATE INDEX CLBADDB ON VM1DTA.CLBAPF
(CLNTCOY, CLNTNUM, BANKKEY, BANKACCKEY, CLNTPFX) ‘);
END IF;
END;
/

5.3 VIEW Statement.


--MUST USE CREATE OR REPLACE FORCE VIEW
CREATE OR REPLACE FORCE VIEW

-- Example
CREATE OR REPLACE FORCE VIEW "VM1DTA"."ACAGRNL"
("UNIQUE_NUMBER", "RLDGCOY", "SACSCODE", "RLDGPFX", "RLDGACCT",
"ORIGCURR", "SACSTYP", "SACSCURBAL", "RDOCNUM", "GLSIGN", "JOBNM",
"USRPRF", "DATIME") AS
SELECT
UNIQUE_NUMBER,RLDGCOY,SACSCODE,RLDGPFX,RLDGACCT,ORIGCURR,SA
CSTYP,SACSCURBAL,RDOCNUM,GLSIGN,JOBNM,USRPRF,DATIME
FROM ACAGPF;

5.4 TRIGGER Statement.


--MUST USER create or replace TRIGGER
-- EXAMPLE
CREATE OR REPLACE TRIGGER "VM1DTA"."GBMHPF_SEQ_TR"
BEFORE INSERT ON VM1DTA.GBMHPF FOR EACH ROW
WHEN (NEW.UNIQUE_NUMBER IS NULL) BEGIN

Version 0.4: 20 April 2018


Document ID: Integral DB Script Rule_Updated.doc Page 17
SELECT VM1DTA.GBMHPF_seq.NEXTVAL INTO :NEW.UNIQUE_NUMBER FROM
DUAL;
END;
/

5.5 STORED PROCEDURE Statement.


--MUST USE "CREATE OR REPLACE PROCEDURE"
--EXAMPLE
CREATE OR REPLACE PROCEDURE get_ACAGRNL
AS
BEGIN
DECLARE d date;
SELECT sysdate into d FROM DUAL;
END get_ACAGRNL ;
/

5.6 FUNCTIONS Statement.


--MUST USE "CREATE OR REPLACE FUNCTION"
CREATE OR REPLACE FUNCTION Get_Acctamt (v_unieq_number NUMBER )
RETURN number
AS
BEGIN

END;
/

5.7 SEQUENCE Statement.


DECLARE
cnt number(2,1) :=0;
BEGIN
SELECT count(*) into cnt FROM user_sequences WHERE sequence_name =
'SCHEMA.SEQUENCE_NAME';
IF ( cnt = 0 ) THEN
EXECUTE IMMEDIATE (' CREATE SEQUENCE sequence_name
MINVALUE value
MAXVALUE value
START WITH value
INCREMENT BY value

Version 0.4: 20 April 2018


Document ID: Integral DB Script Rule_Updated.doc Page 18
CACHE value ');
END IF;
END;
/

DECLARE
cnt number(2,1) :=0;
BEGIN
SELECT count(*) into cnt FROM user_sequences WHERE sequence_name =
'SCHEMA.SEQUENCE_NAME';
IF ( cnt > 0 ) THEN
EXECUTE IMMEDIATE (' DROP SEQUENCE sequence_name');
END IF;
END;
/

5.8 Test ORACLE scripts using “sqlplus”


Description: Since the scripts are executed using auto tools and sqlplus, so using
sqlplus test is way to forbit some errors that caused by sqlplus

5.8.1 Download Oracle Client from ORACLE website


You could go to the website:
https://www.oracle.com/technetwork/database/enterprise-
edition/downloads/oracle12c-windows-3633015.html

Then, choose the following download(32 bit or 64 bit) according to your env:
5.8.1.1 Oracle Database 12c Release 2 Client (12.2.0.1.0) for Microsoft
Windows (x64)
winx64_12201_client.zip (64-bit) (1,180,923,073 bytes)

5.8.1.2 Oracle Database 12c Release 2 Client (12.2.0.1.0) for Microsoft


Windows (32-bit)
win32_12201_client.zip (32-bit) (1,154,114,135 bytes)

After download, install it successfully.

5.8.2 Open Oracle “Net Manager” to configure DB service name


1. Click “Start” => query “net” => click “Net Manager”
2. Then go to window “Oracle Net Manager”
3. Click “Local” => “Service Naming”
Click “+” on the left
4. In new pop window, Give Your_service_name, and IP/PORT, “service
name”(SID), then test it successfully.
Version 0.4: 20 April 2018
Document ID: Integral DB Script Rule_Updated.doc Page 19
5.8.3 Open a Dos window to execute your script
1. Click “Start” => query “cmd” => click “Command Prompt”
2. cd $Your_directory_of_Script
3. exec the command:
sqlplus user/pass@Your_service_name < your_script.sql > your_script.log
4. check the log file : your_script.log to see if there are some errors

6. DML/DDL Template for MSSQL


/*
Created by: Trang Nguyen
Created Date: Aug 18 2016
*/
/*
Description: This template is used for creating any DML statement ON MSSQL for daily
build. The purpose is to reduce duplicate data when insert/update data in a table
Developer must follow the systax on this file whenever create any DML/DDL statement
*/

6.1 DML Statement.


--INSERT statement must be check existing data inadvance
--Example:
INSERT INTO
[VM1DTA].[HELPPF]([HELPPFX],[HELPCOY],[HELPLANG],[HELPTYPE],[HELPPROG],[HELPI
TEM],[HELPSEQ],[TRANID],[VALIDFLAG],[HELPLINE],[USRPRF],[JOBNM],[DATIME])
VALUES ('HP','','E','S','SQ956', '','1','','1','This screen is used to create/ maintain
','UNDERWR1','UNDERWR1',CURRENT_TIMESTAMP )
-- ==> REPLACE BY this
IF NOT EXISTS ( SELECT * FROM [VM1DTA].[HELPPF] h
WHERE h.HELPPFX = 'HP' AND h.HELPLANG='E' AND h.HELPTYPE = 'S' AND h.HELPPROG
='SQ956' AND h.[HELPSEQ] = '1' AND h.[VALIDFLAG] = '1' AND h.HELPLINE = 'This screen is
used to create/ maintain ' )

INSERT INTO
[VM1DTA].[HELPPF]([HELPPFX],[HELPCOY],[HELPLANG],[HELPTYPE],[HELPPROG],[HELPI
TEM],[HELPSEQ],[TRANID],[VALIDFLAG],[HELPLINE],[USRPRF],[JOBNM],[DATIME])
VALUES ('HP','','E','S','SQ956', '','1','','1','This screen is used to create/ maintain
','UNDERWR1','UNDERWR1',CURRENT_TIMESTAMP )

GO

Version 0.4: 20 April 2018


Document ID: Integral DB Script Rule_Updated.doc Page 20
6.2 DDL Statement.
/*
Description: This template is used for creating any DDL statement ON MSSQL for
daily build. The purpose is to reduce error when execute DB script change on daily
build
Developer must follow the systax on this file whenever create any DDL
*/

6.2.1 DDL for “CREATE A NEW TABLE”


-------------------------------------TABLE -------------------------------------
-- Creating a new table

IF NOT EXISTS ( SELECT * FROM sys.tables where name = '<table_name>' and type
= 'U'
and schema_id = (select schema_id from sys.schemas where
name='<schema_name>'))
BEGIN
CREATE TABLE [ database_name . [ schema_name ] . | schema_name . ]
table_name
( { <column_definition> } [ ,...n ]
)
[;];
EXEC sys.sp_addextendedproperty @name=N'MS_Description',
@value=N'<table_comment>' , @level0type=N'SCHEMA',@level0name=N'dbo',
@level1type=N'TABLE',@level1name=N'<table_name>';
EXEC sys.sp_addextendedproperty @name=N'MS_Description',
@value=N'<comment>' , @level0type=N'SCHEMA',@level0name=N'dbo',
@level1type=N'TABLE',@level1name=N'<table_name>',
@level2type=N'COLUMN',@level2name=N'<column_name>';

END
GO

--Example
IF NOT EXISTS ( SELECT * FROM sys.tables where name = 'MGCLPF1' and type =
'U' and schema_id = (select schema_id from sys.schemas where name='VM1DTA'))
BEGIN
CREATE TABLE [VM1DTA].[MGCLPF1](
[UNIQUE_NUMBER] [bigint] IDENTITY(1,1) NOT NULL,
[CLMCOY] [nchar](1) NULL
);

Version 0.4: 20 April 2018


Document ID: Integral DB Script Rule_Updated.doc Page 21
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'table
comment' , @level0type=N'SCHEMA',@level0name=N'VM1DTA',
@level1type=N'TABLE',@level1name=N'MGCLPF1';
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'test
comment' , @level0type=N'SCHEMA',@level0name=N'VM1DTA',
@level1type=N'TABLE',@level1name=N'MGCLPF1',
@level2type=N'COLUMN',@level2name=N'UNIQUE_NUMBER';
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'test
comment' , @level0type=N'SCHEMA',@level0name=N'VM1DTA',
@level1type=N'TABLE',@level1name=N'MGCLPF1',
@level2type=N'COLUMN',@level2name=N'CLMCOY ';
END
GO

6.2.2 DDL for “DROP A TABLE”

--DROP TABLE
IF EXISTS ( SELECT * FROM sys.tables where name = '<table_name>' and type = 'U'
and schema_id = (select schema_id from sys.schemas where
name='<schema_name>'))
DROP TABLE [ database_name . [ schema_name ] . | schema_name . ] table_name
GO

--Example
IF EXISTS ( SELECT * FROM sys.tables where name = 'MGCLPF1' and type = 'U' and
schema_id = (select schema_id from sys.schemas where name='VM1DTA'))
DROP TABLE [VM1DTA].[MGCLPF1]
*/

6.2.3 DDL for “ADD NEW COLUMN”

-- ADD NEW COLUMN TO TABLE


IF NOT EXISTS (SELECT * FROM sys.columns WHERE name = '<column_name>'
and object_id in (SELECT object_id FROM sys.tables WHERE name
='<table_name>'))
BEGIN
ALTER TABLE [ database_name] . [ schema_name ] . table_name ADD
<column_name> <datatype> [<NULL/NOT NULL> <Default>];
EXEC sys.sp_addextendedproperty @name=N'MS_Description',
@value=N'<comment>' , @level0type=N'SCHEMA',@level0name=N'dbo',
@level1type=N'TABLE',@level1name=N'<table_name>',
@level2type=N'COLUMN',@level2name=N'<column_name>';
END
GO
Version 0.4: 20 April 2018
Document ID: Integral DB Script Rule_Updated.doc Page 22
Example:
--Add column
IF NOT EXISTS (SELECT * FROM sys.columns WHERE name = 'GCDPNTNO' and
object_id in (SELECT object_id FROM sys.tables WHERE name ='MGCLPF1'))
BEGIN
ALTER TABLE [VM1DTA].[MGCLPF1] ADD GCDPNTNO nchar(5) null ;
EXEC sys.sp_addextendedproperty @name=N'MS_Description',
@value=N'<test comment>' , @level0type=N'SCHEMA',@level0name=N'VM1DTA',
@level1type=N'TABLE',@level1name=N'MGCLPF1',
@level2type=N'COLUMN',@level2name=N'GCDPNTNO ';
END
GO

6.2.4 DDL for “MODIFY EXISTING COLUMN”

-- MODIFY EXISTING COLUMN


IF EXISTS (SELECT * FROM sys.columns WHERE name = '<column_name>' and
object_id in (SELECT object_id FROM sys.tables WHERE name ='<table_name>'))
ALTER TABLE [ database_name] . [ schema_name ] . table_name ALTER
COLUMN <column_name> <datatype> [<NULL/NOT NULL> <Default> ]

Example:
--Modify column
IF EXISTS (SELECT * FROM sys.columns WHERE name = 'GCDPNTNO' and
object_id in (SELECT object_id FROM sys.tables WHERE name ='MGCLPF1'))
ALTER TABLE [VM1DTA].[MGCLPF1] ALTER COLUMN GCDPNTNO
varchar(50) null
GO

6.2.5 DDL for “DROP A EXISTING COLUMN”

-- DROP EXISTING COLUMN


IF EXISTS (SELECT * FROM sys.columns WHERE name = '<column_name>' and
object_id in (SELECT object_id FROM sys.tables WHERE name ='<table_name>'))
ALTER TABLE [ database_name] . [ schema_name ] . table_name DROP
COLUMN <column_name>

GO

Example:
--Drop column
Version 0.4: 20 April 2018
Document ID: Integral DB Script Rule_Updated.doc Page 23
IF EXISTS (SELECT * FROM sys.columns WHERE name = 'GCDPNTNO' and
object_id in (SELECT object_id FROM sys.tables WHERE name ='MGCLPF1'))
ALTER TABLE [VM1DTA].[MGCLPF1] DROP COLUMN GCDPNTNO
GO

6.3 VIEW Statement.


---CREATE OR ALTER View: Drop existing view (if exists) then re-create
IF EXISTS ( SELECT * FROM sys.views where name = '<View_name>' and
schema_id = (select schema_id from sys.schemas where name='<schema_name>'))
DROP VIEW [schema_name>].<view_name>
GO
--<Create view statement>
CREATE VIEW <schema_name>.<view_name> (columns_definition,...)
AS
SELECT <column_list...,)
FROM ...
WHERE ...
GO
Example
IF EXISTS ( SELECT * FROM sys.views where name = 'GCBA' and schema_id =
(select schema_id from sys.schemas where name='VM1DTA'))
DROP VIEW [VM1DTA].GCBA1
GO
CREATE VIEW [VM1DTA].[GCBA1](UNIQUE_NUMBER, CHDRCOY, CLAMNUM) AS
SELECT UNIQUE_NUMBER,CHDRCOY,CLAMNUM
FROM VM1DTA.GCBAPF

*/

6.4 TRIGGER Statement.


--DROP & RE-CREATE Trigger
IF EXISTS ( SELECT t.* FROM sys.triggers t inner join sys.objects o on t.parent_id
= o.object_id where o.name ='<table_name>' and t.name ='<trigger_name>')
DROP TRIGGER [<schema_name>].<trigger_name>
GO
<CREATE TRIGGER <Schema_name>.<trigger_name> ON
<schema_name>.<table_name> ...
...
GO

Example:
IF EXISTS ( SELECT t.* FROM sys.triggers t inner join sys.objects o on o.object_id
= t.parent_id where o.name ='MGCLPF1' and t.name ='TR_AUDIT_AGPLPF1')

Version 0.4: 20 April 2018


Document ID: Integral DB Script Rule_Updated.doc Page 24
DROP TRIGGER [VM1DTA].[TR_AUDIT_AGPLPF1]
GO

CREATE TRIGGER [VM1DTA].[TR_AUDIT_AGPLPF1] ON


[VM1DTA].[MGCLPF]
AFTER UPDATE, DELETE, INSERT AS
BEGIN
select getdate();
END;
GO
*/

6.5 STORED PROCEDURE Statement.


IF EXISTS ( SELECT * FROM sys.objects WHERE name = '<procedure_name>' and
type = 'P' )
DROP PROCEDURE <schema_name>.<procedure_name>
GO
CREATE PROCEDURE <schema_name>.<procedure_name> (<parameter_list>)
AS
BEGIN
--
END
GO

Example:
IF EXISTS ( SELECT * FROM sys.objects WHERE name = 'ZADRPF_Update1' and
type = 'P' )
DROP Procedure [VM1DTA].[ZADRPF_Update1]
GO

CREATE Procedure [VM1DTA].[ZADRPF_Update1]


(
@SchedNo INT,
@o_total int OUT,
@o_new int OUT,
@o_updated int OUT,
@o_failed int OUT
)
As
BEGIN
--SP contains
select getdate()
END
GO
6.6 FUNCTIONS Statement.
IF EXISTS ( SELECT * FROM sys.objects WHERE name = '<function_name>' and
type = 'FN' )
DROP FUNCTION <schema_name>.<function_name>
GO
CREATE FUNCTION [schema_name].[function_name] (@paramater_list )

Version 0.4: 20 April 2018


Document ID: Integral DB Script Rule_Updated.doc Page 25
RETURNS <datatype>
AS
BEGIN
.....
RETURN <@var>
END
GO

--Example
IF EXISTS ( SELECT * FROM sys.objects WHERE name =
'UDF_PROCESSDATE1' and type = 'FN' )
DROP FUNCTION [VM1DTA].[UDF_PROCESSDATE1]
GO
CREATE FUNCTION [VM1DTA].[UDF_PROCESSDATE1] (@LASTCHANGE
varchar(14))
RETURNS datetime
AS
BEGIN
RETURN getdate()
END
GO
6.7 INDEX Statement.
IF EXISTS ( SELECT * FROM sys.indexes i inner join sys.objects o on i.object_id =
o.object_id WHERE i.name = '<Index_name>' and o.name = '<table_name>' )
DROP INDEX [<index_name>] ON [<schema_name>].[<table_name>]
GO
<CREATE INDEX ...>
GO

Example
IF EXISTS ( SELECT o.* FROM sys.indexes i inner join sys.objects o on i.object_id
= o.object_id WHERE i.name = 'GRX21' and o.name = 'GRX2PF1' )
DROP INDEX [GRX21] ON [VM1DTA].[GRX2PF1]
GO

CREATE NONCLUSTERED INDEX [GRX21] ON [VM1DTA].[GRX2PF1]


(
[BATCCOY] ASC,
[BATCBRN] ASC,
[CLAIM] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
GO

Version 0.4: 20 April 2018


Document ID: Integral DB Script Rule_Updated.doc Page 26

Potrebbero piacerti anche