我可以将我的存储过程分成两个这样的吗?

时间:2013-09-10 14:23:20

标签: asp.net-mvc entity-framework stored-procedures

因为当我尝试从mvc项目运行存储过程并且返回多个结果集时出现错误错误,我决定将我的Sp拆分为: 我正在使用Entity Framework数据库优先方法。

这是我原来的存储过程:

  Create PROCEDURE [dbo].[uspMarket_test]
@UserID INT OUTPUT,
@Session VARCHAR(36)
  AS
  BEGIN
SET NOCOUNT ON;

    SELECT TOP 1
        FirstName, LastName, grant
    FROM dbo.test_Users AS U
    WHERE
       U.userID = @UserID 
       AND CAST (U.userHashKey as varchar(36)) = @Session;

    select
       GS.Account, GSA.Permissions
    from user_name GS 
    Inner join user_name_Apps GSA on GS.ID = GSA.ID
    where GS.UserID = @UserID

    UPDATE dbo.test_Users 
    SET grant = 0
    WHERE
     userID = @UserID
     ;

    END

现在我把它分成两个:

   USE [Tracker]
   GO
  SET ANSI_NULLS ON
   GO

   SET QUOTED_IDENTIFIER ON
  GO

   Create PROCEDURE [dbo].[Market_Scope]
@UserID INT OUTPUT
  AS
    BEGIN
SET NOCOUNT ON;

SELECT TOP 1

       GS.Account, GSA.Permissions
     from user_name GS 
     Inner join user_name_Apps GSA on GS.ID = GSA.ID
       where GS.UserID = @UserID
 ;
          END
           GO

第二个SP是:

    USE [Tracker]
   GO
   SET ANSI_NULLS ON
   GO

   SET QUOTED_IDENTIFIER ON
   GO

   Create PROCEDURE [dbo].[Market_Users]
          @UserID INT OUTPUT,
        @Session VARCHAR(36)
        SELECT TOP 1
       FirstName, LastName, grant
        FROM dbo.test_Users AS U
     WHERE
      U.userID = @UserID 
     AND CAST (U.userHashKey as varchar(36)) = @Session;
    UPDATE dbo.test_Users 
     SET grant = 0
    WHERE
    userID = @UserID
     ;

    END
     Go

现在我在班上打电话给这两个SP:

     public void permissions(string sess)
    {
        using (var ctx2 = new Tracker_Entities())
        {  
       ctx2.uspMarket_Users(new ObjectParameter("UserID", typeof(int)), sess);
       ctx2.uspMarket_list(new ObjectParameter("UserID",typeof(int)));
       ctx2.SaveChanges();
        }
      }

这就是我在控制器中调用的方式:

  public ActionResult Index(string sess)
    {

        var permissions = new Accept();
        permissions.permissions(sess);



        return View();
     }

因为两个SP只是选择记录我所做的是我将虚拟数据填充到我的user_name_apps表中。那么我如何在前端检查这个SP是否正常执行?我可以在我的sql server中执行它在那里工作正常。但我想确保我正确地调用我的SP。那我怎么能这样做呢?

0 个答案:

没有答案