Skip to content

Commit c25b7e2

Browse files
committed
Fix #35 - Created rule G-7125 Always use CREATE OR REPLACE instead of CREATE alone.
1 parent a27e09e commit c25b7e2

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

docs/4-language-usage/7-stored-objects/1-general/g-7120.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# G-7120 Always add the name of the program unit to its end keyword.
1+
# G-7120: Always add the name of the program unit to its end keyword.
22

33
!!! tip "Minor"
44
Maintainability
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# G-7125: Always use CREATE OR REPLACE instead of CREATE alone.
2+
3+
!!! tip "Minor"
4+
Maintainability
5+
6+
## Reason
7+
8+
Using `create` alone makes your scripts give an error if the program unit already exists, which makes the script not repeatable. It is good practice to use `create or replace` to avoid such errors.
9+
10+
## Example (bad)
11+
12+
``` sql
13+
create package body employee_api is
14+
function employee_by_id (in_employee_id in employees.employee_id%type)
15+
return employees%rowtype is
16+
r_employee employees%rowtype;
17+
begin
18+
select *
19+
into r_employee
20+
from employees
21+
where employee_id = in_employee_id;
22+
23+
return r_employee;
24+
exception
25+
when no_data_found then
26+
null;
27+
when too_many_rows then
28+
raise;
29+
end employee_by_id;
30+
end employee_api;
31+
/
32+
```
33+
34+
## Example (good)
35+
36+
``` sql
37+
create or replace package body employee_api is
38+
function employee_by_id (in_employee_id in employees.employee_id%type)
39+
return employees%rowtype is
40+
r_employee employees%rowtype;
41+
begin
42+
select *
43+
into r_employee
44+
from employees
45+
where employee_id = in_employee_id;
46+
47+
return r_employee;
48+
exception
49+
when no_data_found then
50+
null;
51+
when too_many_rows then
52+
raise;
53+
end employee_by_id;
54+
end employee_api;
55+
/
56+
```

0 commit comments

Comments
 (0)