这是我的表:
ID ReportID M_ID ColsName Type Level Parent ID
1989 66 349 t169.ID j J 1 0
1990 66 350 t170.ID j J 1 0
1991 66 351 t171.ID j J 1 0
1992 66 352 Linkw2.t170ID j 2 1990
1993 66 352 Linkw2.t169ID j 2 1989
1994 66 352 Linkw2.t171ID j 2 1991
我的代码:
DatabaseObject objData = new DatabaseObject();
int ReportID = Convert.ToInt32(Request["ReportsID"]);
objData.Query = "select DISTINCT R.ReportName,R.ID from Reports R inner join ReportModuleCols RC on R.ID=RC.ReportID where RC.ReportID= " + ReportID + " and R.ID= " + ReportID;
string ReportName = Convert.ToString(objData.GetSingleValue());
string StoredProcedure = "";
StoredProcedure += " CREATE PROCEDURE spGet" + ReportName + ReportID + "\n";
StoredProcedure += " @Error Varchar(1000) output\n";
StoredProcedure += " AS \n";
StoredProcedure += " BEGIN \n";
StoredProcedure += " BEGIN TRY\n";
StoredProcedure += " SELECT ";
objData.Query = "select ID,ReportID,ReportModuleID,ColsName,Type,ReportLevel,ParentID from ReportModuleCols where Type='d' and ReportID=" + ReportID;
DataTable Displaytb = objData.GetTable();
for (int d = 0; d < Displaytb.Rows.Count; d++)
StoredProcedure += Displaytb.Rows[d]["ColsName"] + ",";
string DisplayParam = StoredProcedure.Remove(StoredProcedure.Length - 1, 1);
DisplayParam += " FROM \n";
objData.Query = "select ID,ReportID,ReportModuleID,ColsName,Type,ReportLevel,ParentID from ReportModuleCols where Type='i' and ReportID=" + ReportID;
DataTable inputtable = objData.GetTable();
for (int t = 0; t < inputtable.Rows.Count; t++)
{
string table = "";
table = inputtable.Rows[t]["ColsName"].ToString();
table = table.Substring(0, table.IndexOf("."));
DisplayParam += table + ",";
}
string SPQuery = DisplayParam.Remove(DisplayParam.Length - 1, 1) + "\n";
SPQuery += " WHERE \n";
//Here i want to write join query
SPQuery += " END TRY\n";
SPQuery += " BEGIN CATCH\n";
SPQuery += " SET @Error = ERROR_NUMBER() + ' ' + ERROR_MESSAGE();\n";
SPQuery += " END CATCH\n";
SPQuery += " END";
objData.Query = SPQuery;
objData.Execute();
我有一张桌子,我有3种类型,i和d和j
我输入参数
显示参数
和j用于连接参数,我正在为它创建动态过程我不知道如何编写连接查询。请帮帮我。
答案 0 :(得分:0)
我这样做了。它正在工作......
DatabaseObject objData = new DatabaseObject();
int ReportID = Convert.ToInt32(Request["ReportsID"]);
objData.Query = " select DISTINCT R.ReportName,R.ID from Reports R inner
join ReportModuleCols RC on R.ID=RC.ReportID where RC.ReportID= " + ReportID
+ " and R.ID= " + ReportID;
string ReportName = Convert.ToString(objData.GetSingleValue());
string storedProcedure = "";
storedProcedure += " CREATE PROCEDURE spGet" + ReportName + ReportID + "\n";
storedProcedure += " @Error Varchar(1000) output\n";
storedProcedure += " AS \n";
storedProcedure += " BEGIN \n";
storedProcedure += " BEGIN TRY\n";
storedProcedure += " SELECT ";
objData.Query = " select ID,ReportID,ReportModuleID,ColsName,Type,ReportLevel,ParentID from ReportModuleCols where Type='d' and ReportID=" + ReportID;
DataTable Displaytb = objData.GetTable();
for (int d = 0; d < Displaytb.Rows.Count; d++)
{
storedProcedure += Displaytb.Rows[d]["ColsName"] + ",";
}
string DisplayParam = storedProcedure.Remove(storedProcedure.Length - 1, 1);
DisplayParam += " FROM \n";
objData.Query = "select ColsName from ReportModuleCols where Type='d' and ReportID=" + ReportID;
DataTable inputtable = objData.GetTable();
string TableColumn = "";
for (int t = 0; t < inputtable.Rows.Count; t++)
{
string table = "";
table = inputtable.Rows[t]["ColsName"].ToString();
table = table.Substring(0, table.IndexOf("."));
TableColumn += table + ",";
}
string[] TCols = TableColumn.Split(",".ToCharArray());
string[] unique = TCols.Distinct().ToArray();
foreach (string TableName in unique)
{
if (TableName != "")
{
DisplayParam += TableName + ",";
}
}
string SPQuery = DisplayParam.Remove(DisplayParam.Length - 1, 1) + "\n";
SPQuery += " WHERE \n";
objData.Query = " select ID,ReportID,ReportModuleID,ColsName,Type,ReportLevel,ParentID from ReportModuleCols where Type='j' and ParentID=0 and ReportID=" + ReportID;
DataTable Join = objData.GetTable();
for (int t = 0; t < Join.Rows.Count; t++)
{
objData.Query = " select ColsName from ReportModuleCols where Type='j' and ParentID=" + Convert.ToInt32(Join.Rows[t]["ID"]) + " and ReportID=" + ReportID;
string ColJoin = objData.GetSingleValue() + "";
SPQuery += Join.Rows[t]["ColsName"] + "=" + ColJoin + " AND \n";
}
SPQuery += " 1 = 1 \n";
SPQuery += " END TRY\n";
SPQuery += " BEGIN CATCH\n";
SPQuery += " SET @Error = ERROR_NUMBER() + ' ' + ERROR_MESSAGE();\n";
SPQuery += " END CATCH\n";
SPQuery += " END";
objData.Query = SPQuery;
objData.Execute();