I have a situation, where I am executing a stored procedure and fetching results in my c# code which returns 55 records, now I have a column in the row which needs to be displayed as a result of executing another sql query. I have tried all means, but couldn’t figure out how to achieve. Please help.
The C# code where I call the procedure which works fine is:
public static MonthlyFinancialReportCollection GetList(SASTransaction withinTransaction)
{
MonthlyFinancialReportCollection records = null;
using (DataSet ds = Helpers.GetDataSet("ShowPremiumCalcReport", withinTransaction))
{
if (ds.Tables.Count > 0)
{
records = new MonthlyFinancialReportCollection();
foreach (DataRow dr in ds.Tables[0].Rows)
{
if (!string.IsNullOrEmpty(dr[0].ToString()))
records.Add(FillDataRecord(dr));
}
}
}
return records;
}
Now I want to run a query, against the called procedure in the above function, so it will fill the one of the column with calculated result of the query.
select
Sum(WorkingHours.WHO_Amount * dbo.PremiumLevel.PLE_Premium) as Snr_Teaching_Amt
from policy, policyline, coveroption,
premiumlevel, WorkingHours
where policy.pol_id=policyline.pli_pol_id
and policyline.pli_cop_seniorteachingcoverid=coveroption.cop_id
and premiumlevel.ple_own_id=policy.pol_own_id
and premiumlevel.ple_sca_id=coveroption.cop_sca_id
and WorkingHours.who_pos_id=policyline.pli_pos_id
and pol_dcsf=row["snr teaching staff"]`
The slowest and easiest way:
As a parameter.
ds.Tables[0].Columns.Add(“Snr_Teaching_Amt”, typeof(decimal));
However I don’t recommend this way because you’ll have 55 + 1 server round trips which is not cool.
The in-between way:
Since your second query returns a single value, make the second query a Sql function. You can call it in the first procedure like:
This will have better performance and is easier to maintain.
However I agree with Massanu, you can combine the procedures using sub queries. Best of luck 😉