Redian新闻
>
如何用SQL语句判断一个TABLE是否存在?
avatar
如何用SQL语句判断一个TABLE是否存在?# Database - 数据库
o*s
1
各位高手:
我想实现下面的SQL块语句
begin
if TABLE是否存在
delete from TABLE;
else
create TABLE;
...
end
谢了
avatar
g*s
2
sql server is
exists(select * from sysobjects where name='table_name' and type='U')

【在 o**s 的大作中提到】
: 各位高手:
: 我想实现下面的SQL块语句
: begin
: if TABLE是否存在
: delete from TABLE;
: else
: create TABLE;
: ...
: end
: 谢了

avatar
w*g
3
in Oracle, try this one.
declare
isThere varchar2(1);
begin
select 'Y' into isThere
from user_tables
where table_name = 'MYTABLE';
if isThere = 'Y' then
truncate table MYTABLE;
else
create table MYTABLE (....);
end if;
end;

【在 o**s 的大作中提到】
: 各位高手:
: 我想实现下面的SQL块语句
: begin
: if TABLE是否存在
: delete from TABLE;
: else
: create TABLE;
: ...
: end
: 谢了

avatar
s*t
4
it won't work. An error will be generated if more than
one table have the same name under different schemas.
declare
isThere varchar2(1) := 'N';
begin
select 'Y' into isThere
from dual
where exists
(select null from all_tables
where table_name = 'ABC');
if isThere = 'Y' then
dbms_output.put_line('exists');
end if;
exception
when no_data_found then
dbms_output.put_line('not exists');
end;

【在 w****g 的大作中提到】
: in Oracle, try this one.
: declare
: isThere varchar2(1);
: begin
: select 'Y' into isThere
: from user_tables
: where table_name = 'MYTABLE';
: if isThere = 'Y' then
: truncate table MYTABLE;
: else

avatar
m*t
5
In SQL Server, this will do it:
if exists (select * from sysobjects where id = object_id(N'tableName')
and OBJECTPROPERTY(id, N'Table') = 1)
avatar
s*j
6
In Oracle
select * from dba_tables where table_name = 'TABLE_NAME'

【在 m******t 的大作中提到】
: In SQL Server, this will do it:
: if exists (select * from sysobjects where id = object_id(N'tableName')
: and OBJECTPROPERTY(id, N'Table') = 1)

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。