Combine two views in a stored procedure

最后都变了- 提交于 2020-05-09 17:15:15

问题


I don't know what is the best practice to do that but if I need a stored procedure to fetch data from multiple views.

I have two views first one of them passing parameter CreationDate to it and second one execute the first one with additional statements.

What I want to do is to select data fro these two views from C# code; I am using ASP.NET Webforms, VS 2010 to get the result from second view after changing the CreationDate from the first view.

This is the select from the first view called VwInprogressSub1

select 
    CurrentAgentFK, 
    Count(*) as TotalLeads,
    Sum(CASE WHEN LeadTicket.leadticketstatusfk=4 THEN 1 ELSE 0 END) AS AssignedCount,
    Sum(CASE WHEN LeadTicket.leadticketstatusfk=6 THEN 1 ELSE 0 END) AS InProgressCount,
    Sum(CASE WHEN DaysToGetInProgress=0 THEN 1 ELSE 0 END) AS RespondedSameDay,
    Sum(CASE WHEN DaysToGetInProgress=1 THEN 1 ELSE 0 END) AS RespondedSecondDay,
    Sum(CASE WHEN DaysToGetInProgress=2 THEN 1 ELSE 0 END) AS RespondedThirdDay,
    Sum(CASE WHEN DaysToGetInProgress>2 THEN 1 ELSE 0 END) AS RespondedMorethanThreeDay
 from 
     leadticket
 where 
     isfullleadticket = 1 and isold = 0
     and leadticket.CreationDateLG >= 20200201
     and leadticket.CreationDateLG <= 20200229
 group by 
     CurrentAgentFK

This is the select from the second view called VwInprogress :

SELECT
    dbo.VW_User.BranchFranchiseeName AS Branch,
    dbo.VW_User.UserName,
    dbo.VwInprogressSub1.TotalLeads,
    dbo.VwInprogressSub1.AssignedCount,
    dbo.VwInprogressSub1.InProgressCount,
    dbo.VwInprogressSub1.RespondedSameDay,
    dbo.VwInprogressSub1.RespondedSecondDay,
    dbo.VwInprogressSub1.RespondedThirdDay,
    dbo.VwInprogressSub1.RespondedMorethanThreeDay,
    (((((dbo.VwInprogressSub1.RespondedSameDay) * 5) +
       ((dbo.VwInprogressSub1.RespondedSecondDay) * 3) +
       ((dbo.VwInprogressSub1.RespondedThirdDay) * 2))) -
      ((dbo.VwInprogressSub1.RespondedMorethanThreeDay) * 2)) AS Score,
    dbo.Vw_AgentsInAllTeams.TeamManagerUsername
FROM
    dbo.VwInprogressSub1
INNER JOIN 
    dbo.VW_User ON dbo.VW_User.UserIdLG = dbo.VwInprogressSub1.CurrentAgentFK
LEFT JOIN 
    dbo.Vw_AgentsInAllTeams ON dbo.VW_User.UserIdLG = dbo.Vw_AgentsInAllTeams.UserPK
WHERE
    dbo.VW_User.IsApproved = 1

回答1:


I would wrap each view in a separate stored procedure, fetch the results and do further logic in C# code. Here's how you create stored procedure and open connection, create command and execute stored procedure. You didn't specify ORM framework so I assume you would like to use stuff from System.Data.Sqlclient.

CREATE PROCEDURE [dbo].[StoredProcedure]
  @CreationDate INT
AS
BEGIN
   -- ... view select
END
using (var connection = new SqlConnection(connectionString))
using (var command = new SqlCommand("StoredProcedure", connection) { 
                           CommandType = CommandType.StoredProcedure }) {
   connection.Open();
   command.Parameters.Add(new SqlParameter("@CreationDate", creationDate));
   var reader = command.ExecuteReader();

   while (reader.Read())
   {
      // Logic depends on how are you going to store view results. DataTable? DTO?
   }
}

--EDIT Further to conversation SqlDataAdapter approach:

using (var connection = new SqlConnection(connectionString))
using (var command = new SqlCommand("StoredProcedure", connection) { 
                           CommandType = CommandType.StoredProcedure }) {
   command.Parameters.Add(new SqlParameter("@CreationDate", creationDate));

   using (var dataAdapter = new SqlDataAdapter(command)) {
      var dataTable = new DataTable();
      dataAdapter.Fill(dataTable);
   }

}



回答2:


This is solved my issue for what i want to do with two sets of views i need enhancement this stored procedure because it takes more time for execute .

Any Advice

CREATE PROCEDURE sp_ScoreReport   
    @CreationDateFrom datetime,   
    @vCreationDateTo datetime   
AS   

    SET NOCOUNT ON;     
	SELECT
dbo.VW_User.BranchFranchiseeName AS Branch,
dbo.VW_User.UserName,
VwInprogressSub1.TotalLeads,

VwInprogressSub1.AssignedCount,
VwInprogressSub1.InProgressCount,


VwInprogressSub1.RespondedSameDay,
VwInprogressSub1.RespondedSecondDay,
VwInprogressSub1.RespondedThirdDay,
VwInprogressSub1.RespondedMorethanThreeDay,
 
(((((VwInprogressSub1.RespondedSameDay)*5)+
((VwInprogressSub1.RespondedSecondDay)*3)+
((VwInprogressSub1.RespondedThirdDay)*2)))-
((VwInprogressSub1.RespondedMorethanThreeDay)*2))


AS Score,
dbo.Vw_AgentsInAllTeams.TeamManagerUsername

FROM
(select CurrentAgentFK,Count(*) as TotalLeads,
Sum(CASE WHEN LeadTicket.leadticketstatusfk=4 THEN 1 ELSE 0 END) AS AssignedCount,
Sum(CASE WHEN LeadTicket.leadticketstatusfk=6 THEN 1 ELSE 0 END) AS InProgressCount,
Sum(CASE WHEN DaysToGetInProgress=0 THEN 1 ELSE 0 END) AS RespondedSameDay,
Sum(CASE WHEN DaysToGetInProgress=1 THEN 1 ELSE 0 END) AS RespondedSecondDay,
Sum(CASE WHEN DaysToGetInProgress=2 THEN 1 ELSE 0 END) AS RespondedThirdDay,
Sum(CASE WHEN DaysToGetInProgress>2 THEN 1 ELSE 0 END) AS RespondedMorethanThreeDay




 from leadticket

where isfullleadticket=1 and isold=0
and leadticket.CreationDateLG >= @CreationDateFrom
and leadticket.CreationDateLG <= @vCreationDateTo

group by CurrentAgentFK) as VwInprogressSub1
INNER JOIN dbo.VW_User ON dbo.VW_User.UserIdLG = VwInprogressSub1.CurrentAgentFK
left JOIN dbo.Vw_AgentsInAllTeams ON dbo.VW_User.UserIdLG = dbo.Vw_AgentsInAllTeams.UserPK
WHERE
dbo.VW_User.IsApproved = 1
	
GO  


来源:https://stackoverflow.com/questions/61169980/combine-two-views-in-a-stored-procedure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!