我想在单个Oracle Command中执行以下所有插入到表语句中这可能吗?
OracleCommand cmd = new OracleCommand();
var parameter = cmd.Parameters;
string insrtPInfo = "Insert into PersonalInfo(Name, ContactNum, EmailID, Address, Gender, DOB) VALUES(:pName, :contactNum, :emailId, :address, :gender, :dob )";
string insrtEdu = "Insert into PersonalInfo(Degree, Institution, Year, CGPA_Marks) VALUES(:degree, :Instituition, :year, :marks)";
string insrtExprnce = "Insert into PersonalInfo(Organization, Organization, Desigination) VALUES(:organization, :duration, :desigination)";
string insrtSkils = "Insert into PersonalInfo(Programming_languages, Softwares, OS) VALUES(:progLang, :softwares, OS)";
string insrtProj = "Insert into PersonalInfo(FYP, Other_Projects) VALUES(:fyp, otherProj)";
// T1
parameter.Add("pName", pName);
parameter.Add("contactNum", contactNum);
parameter.Add("emailId", emailId);
parameter.Add("address", address);
parameter.Add("gender", gender);
parameter.Add("dob", dob);
// T2
parameter.Add("degree", degree);
parameter.Add("Instituition", Instituition);
parameter.Add("year", year);
parameter.Add("marks", marks);
// T3
parameter.Add("organization", organization);
parameter.Add("duration", duration);
parameter.Add("desigination", desigination);
// T4
parameter.Add("progLang", progLang);
parameter.Add("softwares", softwares);
parameter.Add("OS", OS);
// T5
parameter.Add("fyp", fyp);
parameter.Add("otherProj", otherProj);
cmd.CommandText = insrtPInfo;
cmd.ExecuteNonQuery();
如何在单个Oracle Command中执行所有这些语句。或者任何其他最佳方式。我正在使用Visual Studio 2013和Oracle 11g作为数据库。
答案 0 :(得分:3)
一种选择是使用Oracle的INSERT ALL
语法:
string sql = @"Insert all
into PersonalInfo(Name, ContactNum, EmailID, Address, Gender, DOB) VALUES(:pName, :contactNum, :emailId, :address, :gender, :dob )
into PersonalInfo(Degree, Institution, Year, CGPA_Marks) VALUES(:degree, :Instituition, :year, :marks)
into PersonalInfo(Organization, Organization, Desigination) VALUES(:organization, :duration, :desigination)
into PersonalInfo(Programming_languages, Softwares, OS) VALUES(:progLang, :softwares, :OS)
into PersonalInfo(FYP, Other_Projects) VALUES(:fyp, :otherProj)
select * from dual";
所有 into_clause
指定
ALL
后跟多个insert_into_clauses
以执行无条件多表插入。 Oracle数据库为子查询返回的每一行执行一次insert_into_clause
。
另一种选择是将所有INSERT
语句包装在匿名PL/SQL
块中:
string sql = @"begin
insert into PersonalInfo(Name, ContactNum, EmailID, Address, Gender, DOB) VALUES(:pName, :contactNum, :emailId, :address, :gender, :dob );
insert into PersonalInfo(Degree, Institution, Year, CGPA_Marks) VALUES(:degree, :Instituition, :year, :marks);
insert into PersonalInfo(Organization, Organization, Desigination) VALUES(:organization, :duration, :desigination);
insert into PersonalInfo(Programming_languages, Softwares, OS) VALUES(:progLang, :softwares, :OS);
insert into PersonalInfo(FYP, Other_Projects) VALUES(:fyp, :otherProj);
end;";