使用存储过程从数据库中提取数据

时间:2017-11-30 16:56:28

标签: sql-server asp.net-mvc stored-procedures

我使用两个存储过程从数据库中提取数据。当我使用这个存储过程

select distinct a.Req_No,a.Req_Date,a.employee_id,a.department,
a.employee_name, 
b.item_no, b.expense_acc, c.ACTDESCR+'/'+c.ACTNUMBR_1+'-'+c.ACTNUMBR_2 [Expense_Acc], b.unit_of_measure,b.quantity_requested,b.description 
from Requisition a inner join Requisitionitem b
Inner Join [TWCL].dbo.GL00100 c
    on b.expense_acc = c.ACTINDX 
on a.Req_No =b.Req_No where status=-1 

它返回状态为-1

的请购单编号上的所有项目

但是,当我使用此sp时,它只返回状态为-1的请购单号,我理解为什么

SELECT distinct a.Req_No,Req_Date
FROM [dbo].[RequisitionItem] a Inner Join [dbo].[Requisition] b
ON a.Req_No = b.Req_No
Inner Join [TWCL].dbo.GL00100 c
ON a.expense_acc = c.ACTINDX
Inner Join  [TWCL].dbo.GL00100 d
On d.ACTINDX = a.expense_acc
Inner Join [TWCL].dbo.IV00101 e
on a.item_no = e.ITEMNMBR
WHERE b.status = -1

与此存储过程结合使用

    SELECT a.ITEMNMBR, a.ITEMDESC, ab.employee_id, ab.department,ab.employee_name,quantity_requested,b.expense_acc, c.ACTDESCR+'/'+c.ACTNUMBR_1+'-'+c.ACTNUMBR_2 [Expense_Acc],
     b.unit_of_measure
FROM [TWCL].[dbo].IV00101 a inner Join RequisitionItem b on a.ITEMNMBR = b.item_no
Inner Join Requisition ab
    on ab.Req_No = b.Req_No
Inner Join [TWCL].dbo.GL00100 c
    on b.expense_acc = c.ACTINDX 
where b.Req_No = @Req_No 

在网页上,数据与第一个存储过程重复,但不与第二个存储过程重复。所以第二个SP返回正确的数据。

当我使用这个控制器时; postobj,第一个SP返回计数4,如果它是5个项目,第二个返回计数2(这是正确的)

  public ActionResult RequisitionList(List<Requisition> postingObj)
    {

        IssueDAO dbObj = new IssueDAO(ConfigurationManager.ConnectionStrings["TWCL_OPERATIONSConnectionString"].ConnectionString);
        //TransactionIssues transissues = new TransactionIssues();
        //transissues.docNumber = string.Concat("IS", DateTime.Now.ToString("yymmddhhmmss"));
        List<string> reqNumbers = new List<string>();



        foreach (var item in postingObj)
        {
            if (item.postTrnx)
            {
                reqNumbers.Add(item.reqNumber);
            }
        }
        if (reqNumbers.Count == 0)
        {
            ViewBag.Message = "Please select at least one requested item";
            return View(dbObj.GetAllRequest());
        }

        dbObj.SetRequisitionStatus0(reqNumbers);
        ViewBag.Message = "Approval Successful!";
        return View(dbObj.GetAllRequest());
    }

我的问题是,当有5个项目以及我正在使用复选框时,为什么第一个返回4个计数,以便用户可以选择一个项目并批准它但我选择了一个项目它会抛出错误消息请选择至少一个请求的项目“因为reqNumbers的计数为零。

模型

 public List<Requisition> GetAllRequest()
    {
        using (var connection = new SqlConnection(connectionString))
        {
            using (var command = new SqlCommand("getallrequests", connection))
            {
                command.CommandType = CommandType.StoredProcedure;

                List<Requisition> request = new List<Requisition>();

                SqlDataReader rdrObj;

                connection.Open();
                rdrObj = command.ExecuteReader();
                while (rdrObj.Read())
                {
                    Requisition requisition = new Requisition();


                    requisition.reqNumber = rdrObj.GetString(0);
                    requisition.reqDate = rdrObj.GetDateTime(1);

                    requisition.items = getRequestItemByRquisition(rdrObj.GetString(0));
                    request.Add(requisition);
                }
                rdrObj.Close();


                return request;
            }
        }
    }

 public List<Item> getRequestItemByRquisition(string Req_No)
    {
        List<Item> items = new List<Item>();
        SqlConnection TWCLOPConnect = new SqlConnection(connectionString.ToString());
        SqlCommand itemscommand = new SqlCommand();
        SqlDataReader itemRdr;
        itemscommand.CommandText = "requisition_sp_getItemNum ";
        itemscommand.CommandType = CommandType.StoredProcedure;
        itemscommand.Connection = TWCLOPConnect;
        itemscommand.Parameters.Add("@Req_No", SqlDbType.VarChar).Value = Req_No;

        try
        {
            TWCLOPConnect.Open();
            itemRdr = itemscommand.ExecuteReader();

            while (itemRdr.Read())
            {


                Item item = new Item();
                //Requisition requisition = new Requisition();
                item.itemNumber = itemRdr.GetString(0);
                item.description = itemRdr.GetString(1);
                ////item.price = Convert.ToDouble(itemRdr[3]);
                item.employeeDetails.employeeNum = Convert.ToInt32(itemRdr.GetString(2));
                item.employeeDetails.department = itemRdr.GetString(3);
                item.employeeDetails.LoggedInUserName = itemRdr.GetString(4);
                item.quantity = Convert.ToDouble(itemRdr[5]);
                item.expense_account.index = itemRdr.GetInt32(6);
                item.expense_account.account_desc = itemRdr.GetString(7);
                ////item.lineNum = itemRdr.GetInt32(7);
               item.selecteduomtext = itemRdr.GetString(8);

                items.Add(item);
            }
            itemRdr.Close();
        }
        catch (Exception ex)
        {

            throw ex;
        }
        finally
        {
            TWCLOPConnect.Close();
        }

        return items;
    }

1 个答案:

答案 0 :(得分:0)

DISTINCT将消除所有选定字段相同的行。

  • 在第一个存储过程中,您引用了11列。
  • 在第二个存储过程中,您只引用了两列。

尝试在第二个存储过程中添加相同的列,您将获得与第一个存储过程相同的结果。