2017-03-09 70 views
1

我已經嘗試過使用內部連接來實現一個存儲過程來顯示不同表中的結果,但是我的問題是select語句沒有返回任何結果,但它將一些值打印爲消息。存儲過程中的「選擇」語句不起作用

alter proc EmployeeReport(@empid int) 
as 
begin 
declare @inTime time(0) 
declare @outTime time(0) 
declare @fromDate date 
declare @toDate date 

set @inTime = (select CAST(InTime as time(0)) from Timelog where [email protected]) 
set @outTime = (select CAST(OutTime as time(0)) from Timelog where EmployeeId = @empid) 
set @fromDate = (select cast (InTime as date) from Timelog where EmployeeId= @empid) 
set @toDate = (select cast (outTime as date) from Timelog where EmployeeId= @empid) 

select @fromDate as FromDate 
     ,@toDate as ToDate 
     ,c.Name as Client 
     ,p.Name as Project 
     ,@inTime as InTime 
     ,@outTime as OutTime 
     ,t.TotalTime 
from Timelog t 
    inner join Employee e 
     on e.id = t.EmployeeId 
    inner join Project p 
     on p.Id = t.EmployeeProjectId 
    inner join Client c 
     on c.Id = p.ClientId 
where t.EmployeeId = @empid 

print @inTime 
print @outTime 
print @fromDate 
print @toDate 
end 

我附上輸出文件什麼我收到,請幫助我這個

內留言得到印刷:

沒有返回的值或選擇:

+1

檢查您的加入,請。另外,您的select語句中缺少where子句 –

+0

我嘗試使用where子句,但仍然無法正常工作 –

+1

您不需要所有這些變量,只需在查詢中選擇這些變量即可。如果你想保留變量,你應該在一個select語句中而不是4中執行它。 –

回答

4

您的初始聲明設置僅包含TimeLog表中的select數據,其中清楚地包含數據。因爲你從這裏到其他表,如果其他表沒有數據,則不會返回任何內容。

要麼確保您的EmployeeProjectClient表中都有數據或更改join s到left,而不是inner

select @fromDate as FromDate 
     ,@toDate as ToDate 
     ,c.Name as Client 
     ,p.Name as Project 
     ,@inTime as InTime 
     ,@outTime as OutTime 
     ,t.TotalTime 
from Timelog t 
    left join Employee e 
     on e.id = t.EmployeeId 
    left join Project p 
     on p.Id = t.EmployeeProjectId 
    left join Client c 
     on c.Id = p.ClientId 
+0

非常感謝你,它與左連接一起工作 –