在SqlServer和Oralce中创建索引

给表名A的字段A增加索引

SqlServer:

if exists (select 1 from sysobjects where name='表名A' and type='u')
and exists (select 1 from syscolumns where name='字段A' and id=object_id('表名A'))
and not exists(select 1 from sys.indexes where object_id = OBJECT_ID('表名A') and name = '索引名称')
begin
exec('create index 索引名称 on 表名A(字段)')
end
go

Oralce:

declare vCount1 int := 0 ;
vCount2 int := 0 ;
begin
select count(1) into vCount1 from user_all_tables where upper(Table_Name) = upper('表名A');
select count(1) into vCount2 from user_tab_columns where upper(Table_Name) = upper('表名A') and upper(column_name) = upper('字段A');
if(vCount1 > 0 and vCount2 > 0 ) then
vCount1 := 0;
select count(1) into vCount1 from user_indexes where upper(table_Name) = upper('表名A') and upper(Index_Name) = upper('索引名');
if(vCount1 = 0) then
execute immediate ('CREATE INDEX 索引名 ON 表名A(字段A)');
end if;
end if;
end;

原文地址:https://www.cnblogs.com/dragondouble/p/SqlServer_Oracle_Index.html