orcle\sqlserver更改数据库相关字段的写法

先判断再加字段

 

oracle 语法:

DECLARE num NUMBER;
BEGIN
  SELECT COUNT(1)
    INTO num
    from cols where TABLE_NAME = upper('sys_emp_history') and COLUMN_NAME = upper('IS_HAVEHOUSE');
  IF num <= 0 THEN
      execute immediate 'ALTER TABLE sys_emp_history ADD ( IS_HAVEHOUSE CHAR(1)  DEFAULT ''0''  NOT NULL ) ';
      execute immediate 'COMMENT ON COLUMN sys_emp_history.IS_HAVEHOUSE IS ''是否有房'' ';
  END IF;
END;

sqlserver 语法

if not exists(select * from syscolumns where name='data_from' and id=object_id('hr_slr_change'))
    
alter table hr_slr_change add data_from int null DEFAULT 0 ; 

go
 

 

 

sqlserver建表语句 判断:

if not exists (select *
            from  sysobjects
           where  id = object_id('xxxx')
            and   type = 'U')

create table xxxx (
   note_id              bigint               identity,
   note_code            nvarchar(40)         not null,
   comp_code            nvarchar(20)         not null
)
go 
 

oracle 建表语句的判断

 

declare num NUMBER;
begin
    select count(1) into num from user_tables where table_name = upper('xxxx') ;
    if num = 0 then
execute immediate 'CREATE TABLE xxxx (
    note_id NUMBER(6) NOT NULL,
    note_code NVARCHAR2(40) NOT NULL
 
)';
 execute immediate 'ALTER TABLE  hr_slr_note  ADD PRIMARY KEY (note_id)';

end if;
end;
/  ---分割符号

comment on table hr_slr_note is
'note_code:  DTX+单位编码+6位年月+4位流水号。';  -- 加注释说明

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(解决问题)