2

we developed one stored procedure like this

CREATE OR REPLACE PROCEDURE CPCE.SP_GET_SUBJECTIVE_EFILE_DATA
(
IN @RptGrpId    INTEGER,
IN @BatchId     INTEGER,
IN @UserId      VARCHAR,
IN @LogMessage  VARCHAR,
OUT @ListCaseReportsCurType CURSOR,
OUT @ListCrDisclosureCurType CURSOR,
OUT @ListCrDisclosureTranDetailAllCurType CURSOR,
OUT @ListCrSarSubPhotoIdAllCurType CURSOR,
OUT @ListRptGrpStaticDataCurType CURSOR,
OUT @ListCountryRptGrpCurType CURSOR,
OUT @ListIdTypeAllCurType CURSOR,
OUT @ListRptGrpCountryAliasCurType CURSOR,
OUT @ListRptGrpStateAliasCurType CURSOR,
OUT @ListRptGrpCityAliasCurType CURSOR,
OUT @ListAgentProfileCurType CURSOR
)

In this stored procedure we are returning the out parameters. In C# code we are calling the stored procedure and retrieving the values like this

DB2Command cmd = new DB2Command("CPCE.SP_GET_SUBJECTIVE_REPORT_DETAILS_FOR_EFILE", connection);
                wParam.dsResult = new DataSet();
                cmd.CommandTimeout = 0;
                cmd.Connection = connection;
                cmd.CommandType = CommandType.StoredProcedure;
                AddParamsForSp(wParam, cmd);
                DB2DataAdapter da = new DB2DataAdapter(cmd);
                da.Fill(wParam.dsResult);

Here we are reading all the values to the dsResult dataset, it contains all the datatables we are retrieving the table values like this

   dtCaseReports = wParam.dsResult.Tables["Table0"];
                dtCrDisclosure = wParam.dsResult.Tables["Table1"];
                dtCrDiscTransDetails = wParam.dsResult.Tables["Table2"];
                dtSubjectPhotoId = wParam.dsResult.Tables["Table3"];
                dtStaticData = wParam.dsResult.Tables["Table4"];

I can able to retrieve all the values but i want to retrieve the values with the valid table names because it is very difficult to remember the Table0,Table1...like this

What we need to change in the stored procedure side for this requirement.

trinadh
  • 137
  • 1
  • 5
  • 19
  • 1
    If possible, I would separate the return results into different procedure calls. This will reduce the confusion of the C# code and reduce the complexity of your DB2 stored procedures. – AWinkle Dec 17 '15 at 20:29

1 Answers1

2

Your question is similar to this question: How can you name the Dataset's Tables you return in a stored proc?

As far as I know, it is not possible to directly control the dataset's table names from within the stored procedure. I usually resort to renaming the tables as soon as the dataset is retrieved so that they have meaningful names, like this:

wParam.dsResult.Tables[0].TableName = "CaseReports";
wParam.dsResult.Tables[1].TableName = "CrDisclosure";
wParam.dsResult.Tables[2].TableName = "CrDiscTransDetails";
wParam.dsResult.Tables[3].TableName = "SubjectPhotoId";
wParam.dsResult.Tables[4].TableName = "StaticData";

If nothing else, you can at least simplify your code slightly by selecting the tables using their numeric index .Tables[3] as opposed to their default table name .Tables["Table3"].

Alternatively, this answer suggests using a column to the result set which would contain the desired table name, and naming the table based on that column value.

SQL:

SELECT col1, col2, 'CaseReports' TableName FROM CaseReports;
SELECT col1, col2, 'CrDisclosure' TableName FROM CrDisclosure;
SELECT col1, col2, 'CrDiscTransDetails' TableName FROM CrDiscTransDestails;
SELECT col1, col2, 'SubjectPhotoId' TableName FROM SubjectPhotoId;
SELECT col1, col2, 'StaticData' TableName FROM StaticData;

C#:

foreach (DataTable table in wParam.dsResult.Tables)
{
    table.TableName = table.Rows[0]["TableName"].ToString();
}

If you truly want to set the table names from within the stored procedure, this workaround could help you. Personally, I would stick with renaming the columns manually.

Community
  • 1
  • 1
Benjamin Ray
  • 1,855
  • 1
  • 14
  • 32