1

I am using same stored procedure for inserting and fetching data on condition base.

Stored Procedure-

   ALTER PROCEDURE dbo.sp_SaveUserRole
    @company_name nvarchar(50)=null,
    @address nvarchar(250)=null,
    @mobileno int =0,
    @phoneno int=0,
    @faxno int=0,
    @email nvarchar(250)=null,
    @regno int=0,
    @establish_date datetime=null,
    @cond int=0

    AS
    if(@cond=1)
    begin
    insert into Company (company_name,address,mobileno,phoneno,faxno,regno,email,establish_date) values (@company_name,@address,@mobileno,@phoneno,@faxno,@regno,@email,@establish_date)
    end
    else if(@cond=2)
    begin
    select * from company where isactive = 'True';
    end

        RETURN

As on inserting data using entity framework I am doing this-

public ActionResult SaveRole(CompanyModel cmp)
    {
        var company_name = cmp.Company_Name;
        var regno = cmp.regNo;
        var mobileno = cmp.mobileNo;
        var phoneno = cmp.phoneNo;
        var establish_date = cmp.establish_Date;
        var email = cmp.emaiL;
        var address = cmp.Address;
        var faxno = cmp.faxNo;
        db.sp_SaveUserRole(company_name, address, mobileno, phoneno, faxno, email, regno, establish_date,1);
        return Json(new { success = true });

Note- Here condition is 1 so it goes to insert data procedure.

Trying to get a list-

 var list = db.sp_SaveUserRole("", "", 0, 0, 0, "", 0, null, 2).ToList();

I tried this way of getting data from table, where I had to pass necessary arguments to this procedure call. Though I wanted to go this procedure only to 2nd condition I've mentioned there.

So only for this 2nd condition How can I modify this procedure without passing arguments?

1
  • 1
    A stored procedure that's named "sp_SaveUserRole" but returns a listing from the company table when a nondescript parameter has some nondescript value doesn't make sense. Commented Apr 2, 2014 at 9:00

1 Answer 1

1

Instead of using a stored procedure I would add your company table as an entity in your edmx and access it in code.

That way instead of passing @Cont=2 to a stored proc you can instead use LINQ to access the data you require as the SQL seems very basic.

You can then remove that piece of SQL from your stored proc as mixing Inserts with selects doesnt feel right.

e.g.

// For insert
if (cont == 1)
{
   // Call your stored proc here

   // or if you add the company table to EF you can use the Insert use the Add 
   // e.g. Rough Example
   _yourEntities.Companies.Add(cmp);

   // Update the DB
   _yourEntities.SaveChanges();
}
else
{
   var companies = _yourEntities.Companies.Where(c => c.isactive == 'True');
}

If this solution is not an option i would still look to split the stored procs into two to make life easily.

Ideally though as you using Entity Framework and are looking to insert and select from a single table you get this functionality for free when you add the Company table as a entity in EF.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.