检查 table 是否存在
Checking if table exist or not
我正在使用 jdbc 从数据库中检索数据。在我的代码中,我使用 3-4 tables 来获取数据。但有时如果 table 不存在于数据库中,我的代码会给出异常。如何处理这种情况。我希望我的代码继续为其他 table 工作,即使一个 table 不存在。请帮忙
我写过这样的代码
sql="select * from table"
now Result set and all.
如果 table 不存在于数据库中,它给出异常,即没有这样的 table。我想处理它。在这段代码中,我不能使用预先已经存在的 tables。我想在这里自己检查 table 是否存在。
请不要将其标记为重复问题。您分享的 link 没有给我所需的答案,因为在那个问题中他们不是通过 JDBC 代码
在数据库中执行查询
这是检查 table 是否存在并删除它的方法:
IF EXISTS (
SELECT 1
FROM sysobjects
WHERE name = 'a_table'
AND type = 'U'
)
DROP TABLE a_table
GO
这就是检查 table 是否存在并创建它的方法。
IF NOT EXISTS (
SELECT 1
FROM sysobjects
WHERE name = 'a_table'
AND type = 'U'
)
EXECUTE("CREATE TABLE a_table (
col1 int not null,
col2 int null
)")
GO
(它们是不同的,因为在 table-drop 中创建了一个临时的 table,所以如果您尝试创建一个新的,您将得到一个异常,它已经存在)
在运行查询table不存在存在一定风险之前,运行下面的sql查询并检查结果数是否>= 1. 如果它 >= 1 那么你可以安全地执行普通查询。否则,请采取措施处理这种情况。
SELECT count(*)
FROM information_schema.TABLES
WHERE (TABLE_SCHEMA = 'your_db_name') AND (TABLE_NAME = 'name_of_table')
我不是 Sybase 方面的专家,但看看这个,
exec sp_tables '%', '%', 'master', "'TABLE'"
对于 Sybase ASE,easiest/quickest 方法将包括在您希望(用户定义的)table 驻留的数据库中查询 sysobjects
table:
select 1 from sysobjects where name = 'table-name' and type = 'U'
- 如果记录已 returned => table 存在
- 如果没有记录 returned => table 不存在
如何使用(上述)查询取决于您...
- return 0/1 行结果集给您的客户端
- 给@变量赋值
- 放置在
if [not] exists(...)
结构中
- 在
case
语句中使用
如果您知道数据库中不会有任何其他对象类型(例如,proc、trigger、view、UDF)具有相关名称,那么您也可以使用 object_id()
函数,例如:
select object_id('table-name')
- 如果您收到一个数字 => 对象存在
- 如果收到 NULL => 对象不存在
虽然 object_id()
将从 sysobjects
table 获取对象的 ID,但它不会检查对象 type
,例如,(以上)查询将return 如果存在名为 'table-name' 的存储过程,则为一个数字。
与 select/sysobjects
查询一样,您如何在代码中使用函数调用取决于您(例如,结果集、填充@variable、if [not] exists()
构造、case
声明)。
因此,解决评论中提供的其他详细信息...
假设您提交的单个批次需要在 运行 所需查询之前确定 table 存在:
-- if table exists, run query(s); obviously if table does not exist then query(s) is not run
if exists(select 1 from sysobjects where name = 'table-name' and type = 'U')
begin
execute("select * from table-name")
end
需要 execute()
来防止优化器生成 table 不存在的错误,即查询不是 parsed/compiled 除非 execute()
是实际调用
如果您的应用程序可以编写为使用多个批处理,则类似以下内容也应该有效:
# application specific code; I don't work with java but the gist of the operation would be ...
run-query-in-db("select 1 from sysobjects where name = 'table-name' and type = 'U'")
if-query-returns-a-row
then
run-query-in-db("select * from table-name")
fi
我正在使用 jdbc 从数据库中检索数据。在我的代码中,我使用 3-4 tables 来获取数据。但有时如果 table 不存在于数据库中,我的代码会给出异常。如何处理这种情况。我希望我的代码继续为其他 table 工作,即使一个 table 不存在。请帮忙
我写过这样的代码
sql="select * from table"
now Result set and all.
如果 table 不存在于数据库中,它给出异常,即没有这样的 table。我想处理它。在这段代码中,我不能使用预先已经存在的 tables。我想在这里自己检查 table 是否存在。
请不要将其标记为重复问题。您分享的 link 没有给我所需的答案,因为在那个问题中他们不是通过 JDBC 代码
在数据库中执行查询这是检查 table 是否存在并删除它的方法:
IF EXISTS (
SELECT 1
FROM sysobjects
WHERE name = 'a_table'
AND type = 'U'
)
DROP TABLE a_table
GO
这就是检查 table 是否存在并创建它的方法。
IF NOT EXISTS (
SELECT 1
FROM sysobjects
WHERE name = 'a_table'
AND type = 'U'
)
EXECUTE("CREATE TABLE a_table (
col1 int not null,
col2 int null
)")
GO
(它们是不同的,因为在 table-drop 中创建了一个临时的 table,所以如果您尝试创建一个新的,您将得到一个异常,它已经存在)
在运行查询table不存在存在一定风险之前,运行下面的sql查询并检查结果数是否>= 1. 如果它 >= 1 那么你可以安全地执行普通查询。否则,请采取措施处理这种情况。
SELECT count(*)
FROM information_schema.TABLES
WHERE (TABLE_SCHEMA = 'your_db_name') AND (TABLE_NAME = 'name_of_table')
我不是 Sybase 方面的专家,但看看这个,
exec sp_tables '%', '%', 'master', "'TABLE'"
对于 Sybase ASE,easiest/quickest 方法将包括在您希望(用户定义的)table 驻留的数据库中查询 sysobjects
table:
select 1 from sysobjects where name = 'table-name' and type = 'U'
- 如果记录已 returned => table 存在
- 如果没有记录 returned => table 不存在
如何使用(上述)查询取决于您...
- return 0/1 行结果集给您的客户端
- 给@变量赋值
- 放置在
if [not] exists(...)
结构中 - 在
case
语句中使用
如果您知道数据库中不会有任何其他对象类型(例如,proc、trigger、view、UDF)具有相关名称,那么您也可以使用 object_id()
函数,例如:
select object_id('table-name')
- 如果您收到一个数字 => 对象存在
- 如果收到 NULL => 对象不存在
虽然 object_id()
将从 sysobjects
table 获取对象的 ID,但它不会检查对象 type
,例如,(以上)查询将return 如果存在名为 'table-name' 的存储过程,则为一个数字。
与 select/sysobjects
查询一样,您如何在代码中使用函数调用取决于您(例如,结果集、填充@variable、if [not] exists()
构造、case
声明)。
因此,解决评论中提供的其他详细信息...
假设您提交的单个批次需要在 运行 所需查询之前确定 table 存在:
-- if table exists, run query(s); obviously if table does not exist then query(s) is not run
if exists(select 1 from sysobjects where name = 'table-name' and type = 'U')
begin
execute("select * from table-name")
end
-
需要
execute()
来防止优化器生成 table 不存在的错误,即查询不是 parsed/compiled 除非execute()
是实际调用
如果您的应用程序可以编写为使用多个批处理,则类似以下内容也应该有效:
# application specific code; I don't work with java but the gist of the operation would be ...
run-query-in-db("select 1 from sysobjects where name = 'table-name' and type = 'U'")
if-query-returns-a-row
then
run-query-in-db("select * from table-name")
fi