选择查询不从表中检索数据?

时间:2012-02-29 10:53:13

标签: sql-server-2008 c#-4.0

我的数据库中有两个表名为...请求和余额跟踪器没有任何关系....但我想从两个表中选择数据并将其分为两个网格......

          EmpID   |EmpRqsts|EmpDescription|ApproverID
           1      |asdfsb  |sadbfsbdf     |1
           2      |asbfd   |sjkfbsd       |1

余额跟踪器

          EmpId|BalanceAmnt|LastUpdated
         | 1   |5000       |sdfbk
         | 2   |3000       |sjbfsh

员工表

      EmpId|EmpName
       1   |Anil
       2   |Raghu

现在Balance跟踪器具有EmployeeTable的ForeignKey列...我想要的是..想从[请求]中选择EmpTable [EmpRqsts]中的[EmpName]和从[Balance Tracker]中选择[BalanceAmnt] [LastUpdated]并绑定它到网格

这是我用来检索数据的存储过程

create procedure SP_GetEmployeeRequests
    (@ApproverName varchar (50))
as
begin
    select 
        EmployeeDetails.Emp_Username, 
        RequestDetails.Request_Amount,
        RequestDetails.Request_description,
        BalanceTracker.Balance_Amount,
        BalanceTracker.LastApproval,
        BalanceTracker.LastUdated
    from 
        EmployeeDetails, RequestDetails, BalanceTracker
    where 
        EmployeeDetails.Emp_ID = RequestDetails.Emp_ID
        and BalanceTracker.Emp_ID = RequestDetails.Emp_ID
        and RequestDetails.Approved_ID = (select Approved_ID 
                                          from ApprovalDetails  
                                          where Approved_By = @ApproverName)
end

此处一切正常,但此查询仅检索列名称。但不是表格的价值......任何人都可以帮我解决我的错误。

2 个答案:

答案 0 :(得分:0)

尝试以下方法:

    CREATE procedure SP_GetEmployeeRequests
    (
         @ApproverName varchar (50)
    )
    AS
    BEGIN

    SELECT ed.Emp_Username, rd.Request_Amount, rd.Request_description, bt.Balance_Amount, bt.LastApproval, bt.LastUpdated
    FROM EmployeeDetails ed JOIN RequestDetails rd ON ed.Emp_ID = rd.emp_ID 
    JOIN BalanceTracker bt ON bt.Emp_ID = rd.Emp_ID
    JOIN ApprovalDetails ad ON rd.Approved_ID  = ad.Approved_ID
    WHERE ad.Approved_By = @ApproverName 

    END
    GO

答案 1 :(得分:0)

我认为错误是您在架构“Approver_ID”时检查“Approved_ID”

create procedure SP_GetEmployeeRequests
    (
     @ApproverName varchar (50)
    )
    as
    begin
        select ed.Emp_Username
             , rd.Request_Amount
             , rd.Request_description
             , bt.Balance_Amount
             , bt.LastApproval
             , bt.LastUdated
          from RequestDetails rd
          join BalanceTracker bt
            on rd.Emp_ID = bt.Emp_ID
          join EmployeeDetails ed
            on rd.Emp_ID = ed.Emp_ID
          join ApprovalDetails ad
            on rd.Approver_ID = ad.Approver_ID
         where ad.Approver_By = @ApproverName)
    end
    go