表存在則刪除(一句sql實現)

通過一句sql實現:

1、表存在則刪除;2、表不存在則創建;


mysql:

drop table if exists `test`;
create table if not exists `test` (`id` integer not null, `name` varchar(10), primary key(`id`));


sqlserver:

if exists (select count(*) from [sys].[schemas] S JOIN [sys].[tables] T ON S.schema_id = T.schema_id where S.name='ms-1')
drop table [ms-1];

if not EXISTS(select count(*) from [sys].[schemas] S JOIN [sys].[tables] T ON S.schema_id = T.schema_id where S.name='ms-1')
CREATE TABLE [ms-1] (
	[id] INTEGER not null,
	[name] VARCHAR(10),
	PRIMARY KEY([id]
);

oracle:

declare num number; 
begin
    select count(*) into num from all_tables where TABLE_NAME = 'tt-1' and OWNER='ODBC'; 
    if num=1 then 
        execute immediate 'drop table "tt-1"';
    end if;
end;


dameng:

同oracle。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章