---
title: "sql脚本书写规范"
author: "beee"
date: "2014-08-20T09:52:10Z"
tags: []
categories: ["数据库"]
slug: "sql"
draft: false
lastmod: "2014-08-20T09:52:10Z"
id: 2106218
url: "https://beee.iteye.com/blog/2106218"
---

一些增量脚本的书写规范，mark一下，这些都是经常用到的

比如：

pdm\_table\_表名\_日期（扩充表字段长度）.sql

--修改人  
\--扩充字段长度  
\--修改原因

declare  
  nCount integer;  
begin  
  select count(\*) into nCount from user\_tab\_cols where lower(table\_name)='表名' and lower(column\_name)='列名';  
  if nCount=0 then  
    execute immediate 'alter table 表名 add 列名 字段类型(长度)';  
  end if;  
  select count(\*) into nCount from user\_tab\_cols where lower(table\_name)='表名' and lower(column\_name)='列名';  
  if nCount=1 then  
    execute immediate 'alter table 表名 modify 列名  字段类型(长度)';  
  end if;  
end;  
/  
pdm\_table\_表名\_日期（增加表字段）.sql

--修改人  
\--列名  中文注释 （C\_ISCUSTCARD  是否会员）  
\--增加原因  
declare  
  icount  integer:=0;  
  begin  
    select count(1) into icount from user\_tab\_columns u where lower(u.TABLE\_NAME) = '表名' and lower(u.COLUMN\_NAME) = '列名';  
    if icount = 0 then  
      execute immediate 'alter table 表名 add 列名 字段类型(长度)';  
    end if;  
  end;  
/

--增加表字段时填充默认值  
declare  
  icount  integer:=0;  
  begin  
    select count(1) into icount from user\_tab\_columns u where lower(u.TABLE\_NAME) = '表名' and lower(u.COLUMN\_NAME) = '列名';  
    if icount = 0 then  
      execute immediate 'alter table 表名 add 列名 字段类型(长度) default ''0''';  
    end if;  
  end;  
/

pdm\_table\_存储过程名称\_日期（删除或增加存储过程）.sql

declare  
  v\_count pls\_integer :=0;  
begin  
    select count(1) into iCount from USER\_PROCEDURES t where t.OBJECT\_NAME =upper('存储过程名称');  
  if iCount = 1 then  
    execute immediate 'DROP PROCEDURE 存储过程名称';  
  end if;  
end;  
/

 

等等吧，反正就是一些可以重复执行的批量脚本