
/// 执行一条计算查询结果语句,返回查询结果(object)。
/// </summary>
/// <param name="SQLString">计算查询结果语句</param>
/// <returns>查询结果(object)</returns>
public static object GetSingle(string SQLString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(SQLString, connection))
{
try
{
connection.Open()
object obj = cmd.ExecuteScalar()
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
{
return null
}
else
{
return obj
}
}
catch (System.Data.SqlClient.SqlException e)
{
connection.Close()
throw e
}
}
}
}
/// <summary>
/// 判断是否存在某表
/// </summary>
/// <param name="tableName">表名称</param>
/// <returns>是否存在</returns>
public static bool ColumnExists(string tableName)
{
string sql = "select count(1) from syscolumns where [id]=object_id('" + tableName + "') "
object res = GetSingle(sql)
if (res == null)
{
return false
}
return Convert.ToInt32(res) >0
}
connectionString 是数据库链接字符串
直接复制粘贴就可以用
1、sql语句判断数据库表是否存在:sql:select * from user_all_tables where table_name='tableName'
如果结果为空则表示不存在,如何结果不为空则表示存在;
2、java如何判断数据库表是否存在
可以利用上面的sql,执行获取结果,相应的java代码如下:
String helperName= delegator.getGroupHelperName("com.asiainfo")
SQLProcessor sqlProcessor= new SQLProcessor(helperName)
String sql = "select * from user_all_tables where table_name='"+table+"'"
ResultSet rsTables =sqlProcessor.executeQuery(sql)
if(rsTables.next()){
Debug.logWarning("table:"+table+" exists", module)
}else{
Debug.logWarning("table:"+table+" does not exist", module)
}
通过语句select * from dba_tables where table_name='table的名字(大写)',通过这个语句去判断,如果存在内容那么就是存在,否则就是不存在。
当然如果你所谓的表是视图,那么还要查一下dba_views
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)