Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
230 views
in Technique[技术] by (71.8m points)

sql server - How to use multiple stored procedures with join tables in one view/controller?

I have created multiple stored procedures with join operations in it. I am trying to use these stored procedures in my ASP.NET MVC application but it's not working. I am calling these stored procedures on single view. I am using Entity Framework for comm. to database.

Basically, I have to call these stored procedures bases on particular condition that means have to switch between stored procedures on single view.

Does anyone have idea to call such stored procedure in a single view?

Here are my two stored procedures:

CREATE PROCEDURE sp_GetTotalRecordsByAccountNumber
    @branch varchar(4),
    @basic varchar(6),
    @suffix varchar(3)
AS 
BEGIN
    SELECT
        Date AS 'Date',
        desc1 + RTRIM(LTRIM(ISNULL(desc2, ''))) + '' +
                RTRIM(LTRIM(ISNULL(desc2, ''))) AS 'Description',
        ISNULL(CASE WHEN Amount < 0 THEN (Transactionamt / Currency) END, 0) AS Debit,
        ISNULL(CASE WHEN Amount > 0 THEN (Transactionamt / Currency) END, 0) AS Credit,
        (TotalAmount / currency) AS Balance
    FROM
        tbl1
    LEFT OUTER JOIN 
        tbl2 ON tbl1.accountnumber = tbl2.accountnumber
    LEFT OUTER JOIN 
        tbl3 ON tbl1.currency = tbl3.currency
    LEFT OUTER JOIN 
        tbl4 ON tbl1.accountName = tbl4.accountName
    WHERE
        branch = @branch 
        AND basic = @basic 
        AND suffix = @suffix
    ORDER BY 
        date 
END

CREATE PROCEDURE sp_GetTotalRecordsByDates
    @branch varchar(4),
    @basic varchar(6),
    @suffix varchar(3),
    @startdate datetime,
    @enddate datetime
AS
BEGIN
    SELECT 
        Date AS 'Date',
        desc1 + RTRIM(LTRIM(ISNULL(desc2, ''))) + '' +
                RTRIM(LTRIM(ISNULL(desc2, ''))) AS 'Description',
        ISNULL(CASE WHEN Amount < 0 THEN (Transactionamt/Currency) END, 0) AS Debit,
        ISNULL(CASE WHEN Amount > 0 THEN (Transactionamt/Currency) END, 0) AS Credit,
        (TotalAmount/currency) AS Balance
    FROM
        tbl1
    LEFT OUTER JOIN 
        tbl2 ON tbl1.accountnumber = tbl2.accountnumber
    LEFT OUTER JOIN 
        tbl3 ON tbl1.currency = tbl3.currency
    LEFT OUTER JOIN 
        tbl4 ON tbl1.accountName = tbl4.accountName
    WHERE 
        branch = @branch 
        AND basic = @basic 
        AND suffix = @suffix
        AND date BETWEEN @startdate AND @enddate
    ORDER BY 
        date date
END

My controller is:

[HttpPost]
public ActionResult Index(FormCollection formCollection, string accountNo,string date,bool allcheckbox) 
{  
    if(allcheckbox == true) 
    {
       var dc = db.sp_GetTotalRecordsByAccountNumber(accountNo).ToList();
    }
    else
    {
       var dc = db.sp_GetTotalRecordsByDates(accountNo).ToList();
    }

    return view("Index",dc);
}

And my view is:

@model IEnumerable<ComplexStoredProceduremvc.Models.sp_GetTotalRecordsByAccountNumber_Result>

Here, it only gives data by account number if I want to get data by date then I have to change the stored procedure name which is not the correct option.

I hope you will get the problem....waiting for some solutions...Help!

question from:https://stackoverflow.com/questions/65857417/how-to-use-multiple-stored-procedures-with-join-tables-in-one-view-controller

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think you can handle both situation with one SP like below

CREATE PROCEDURE sp_GetTotalRecordsByDates
@branch varchar(4)='0',
@basic varchar(6)='0',
@suffix varchar(3)='0',
@startdate datetime=null,
@enddate datetime=null
AS
BEGIN
SELECT 
    Date AS 'Date',
    desc1 + RTRIM(LTRIM(ISNULL(desc2, ''))) + '' +
            RTRIM(LTRIM(ISNULL(desc2, ''))) AS 'Description',
    ISNULL(CASE WHEN Amount < 0 THEN (Transactionamt/Currency) END, 0) AS Debit,
    ISNULL(CASE WHEN Amount > 0 THEN (Transactionamt/Currency) END, 0) AS Credit,
    (TotalAmount/currency) AS Balance
FROM
    tbl1
LEFT OUTER JOIN 
    tbl2 ON tbl1.accountnumber = tbl2.accountnumber
LEFT OUTER JOIN 
    tbl3 ON tbl1.currency = tbl3.currency
LEFT OUTER JOIN 
    tbl4 ON tbl1.accountName = tbl4.accountName
WHERE 
    (branch = @branch or @branch='0')
    AND (basic = @basic or @basic='0')
    AND (suffix = @suffix or @suffix ='0')
    AND (date >= @startdate or @startdate is null)
    AND (date <= @enddate or @enddate is null)
ORDER BY 
    date
END

Just make sure if you do not want to filter with some parameter pass it as 0 or null. In this case I used 0 for varchar and null for date.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...