Oracle 为什么我得到ORA-00922?
我正在开发一个小小的C#WPF应用程序,在这个应用程序中,我需要有机会创建一个PLAN#u表(我与Oracle合作),但表的名称可以自定义。Oracle 为什么我得到ORA-00922?,oracle,ado.net,Oracle,Ado.net,我正在开发一个小小的C#WPF应用程序,在这个应用程序中,我需要有机会创建一个PLAN#u表(我与Oracle合作),但表的名称可以自定义。 所以,用户可以输入他的PLAN_表名,如果它不存在,它将自动创建 以下是创建计划表的方法: private void CreatePlanTable() { using (OracleConnection connection = new OracleConnection(ConnectionString)) {
所以,用户可以输入他的PLAN_表名,如果它不存在,它将自动创建 以下是创建计划表的方法:
private void CreatePlanTable()
{
using (OracleConnection connection = new OracleConnection(ConnectionString))
{
connection.Open();
var cmd = connection.CreateCommand();
cmd.CommandText = SqlUtils.GetPlanTableScript(planTable);
cmd.ExecuteNonQuery(); //Here is where I get ORA-00922 exception
}
}
public string Test(bool createPlanTable)
{
try
{
using (OracleConnection connection = new OracleConnection(ConnectionString))
{
connection.Open();
var cmd = connection.CreateCommand();
cmd.CommandText = "select 1 from all_tables where table_name = :name";
var p = cmd.Parameters;
p.Add("name", planTable.ToUpper());
var reader = cmd.ExecuteReader();
isValid = reader.HasRows;
if (isValid)
{
return "Success";
}
else
{
if (createPlanTable)
{
CreatePlanTable();
isValid = true;
return "Success";
}
return $"There is no {planTable} exists";
}
}
}
catch (Exception e)
{
return e.Message;
}
}
SqlUtils.GetPlanTableScript()使用create plan_table命令返回一个字符串,如下所示:
public static string GetPlanTableScript(string planTableName)
{
return
new StringBuilder()
.AppendLine($"create table \"{planTableName}\" sharing=none (")
.AppendLine("statement_id varchar2(30),")
.AppendLine("plan_id number,")
.AppendLine("timestamp date,")
.AppendLine("remarks varchar2(4000),")
.AppendLine("operation varchar2(30),")
.AppendLine("options varchar2(255),")
.AppendLine("object_node varchar2(128),")
.AppendLine("object_owner varchar2(128),")
.AppendLine("object_name varchar2(128),")
.AppendLine("object_alias varchar2(261),")
.AppendLine("object_instance numeric,")
.AppendLine("object_type varchar2(30),")
.AppendLine("optimizer varchar2(255),")
.AppendLine("search_columns number,")
.AppendLine("id numeric,")
.AppendLine("parent_id numeric,")
.AppendLine("depth numeric,")
.AppendLine("position numeric,")
.AppendLine("cost numeric,")
.AppendLine("cardinality numeric,")
.AppendLine("bytes numeric,")
.AppendLine("other_tag varchar2(255),")
.AppendLine("partition_start varchar2(255),")
.AppendLine("partition_stop varchar2(255),")
.AppendLine("partition_id numeric,")
.AppendLine("other long,")
.AppendLine("distribution varchar2(30),")
.AppendLine("cpu_cost numeric,")
.AppendLine("io_cost numeric,")
.AppendLine("temp_space numeric,")
.AppendLine("access_predicates varchar2(4000),")
.AppendLine("filter_predicates varchar2(4000),")
.AppendLine("projection varchar2(4000),")
.AppendLine("time numeric,")
.AppendLine("qblock_name varchar2(128),")
.AppendLine("other_xml clob")
.AppendLine(");")
.ToString();
}
这个看起来很愚蠢,我想问:是否有更好的方法来创建表
我从另一个方法调用CreatePlanTable(),该方法检查计划表的连接和存在性:
private void CreatePlanTable()
{
using (OracleConnection connection = new OracleConnection(ConnectionString))
{
connection.Open();
var cmd = connection.CreateCommand();
cmd.CommandText = SqlUtils.GetPlanTableScript(planTable);
cmd.ExecuteNonQuery(); //Here is where I get ORA-00922 exception
}
}
public string Test(bool createPlanTable)
{
try
{
using (OracleConnection connection = new OracleConnection(ConnectionString))
{
connection.Open();
var cmd = connection.CreateCommand();
cmd.CommandText = "select 1 from all_tables where table_name = :name";
var p = cmd.Parameters;
p.Add("name", planTable.ToUpper());
var reader = cmd.ExecuteReader();
isValid = reader.HasRows;
if (isValid)
{
return "Success";
}
else
{
if (createPlanTable)
{
CreatePlanTable();
isValid = true;
return "Success";
}
return $"There is no {planTable} exists";
}
}
}
catch (Exception e)
{
return e.Message;
}
}
所以,当用户希望使用现有表时,一切都正常,但当他们希望我创建它时,ORA-00922被抛出。如何避免此异常?我只需要删除;从create table语句的最后一个字符串:
.AppendLine(")")
关于如何更优雅地创建表的问题仍然悬而未决。事实上,您不再需要自己的计划表:很长一段时间(至少从10.1开始)Oracle创建了一个全局临时表
SYS.PLAN\u table$
(授予public)在数据库创建过程中,默认情况下会向其添加一个公共同义词PLAN\u TABLE