2017-03-03 27 views
0

以下是我在應用程序中編寫的過程。但我想列出我的LogSource列,但我無法從這個存儲過程中獲得它。如何在顯示列表時從存儲過程中獲取外鍵值。

CREATE PROCEDURE [dbo].[GetApplicationLogs] 
    -- Add the parameters for the stored procedure here 
    @Skip int, 
    @Pagesize int 
AS 
BEGIN 
    -- SET NOCOUNT ON added to prevent extra result sets from 
    -- interfering with SELECT statements. 
    SET NOCOUNT ON; 

    -- Insert statements for procedure here 
WITH TableDatawithRowNumber AS 
    (SELECT dbo.ApplicationLog.* ,ROW_NUMBER() OVER (ORDER BY LoggedDate DESC) AS RowNumber, 
     (SELECT COUNT(*) AS Expr1 
      FROM ApplicationLog) AS TotalRecords   
    from ApplicationLog 
)  
    SELECT * FROM TableDatawithRowNumber 
    WHERE RowNumber > @Skip AND RowNumber <= (@[email protected]) 
END 

此表doensn't包含LOGSOURCE列,但它具有其LogSourceID其在該表的外鍵和在LOGSOURCE表的主鍵。我想在列表中顯示,但我無法在視圖中看到它。我只能使用LogSourceId,但不能使用LogSource。所以請幫助我。

回答

0

我想,你只需要做一個左連接上LOGSOURCE表

WITH TableDatawithRowNumber AS 
    (SELECT dbo.ApplicationLog.* ,ROW_NUMBER() OVER (ORDER BY LoggedDate DESC) AS RowNumber, logSource.LogSource 
     (SELECT COUNT(*) AS Expr1 
      FROM ApplicationLog) AS TotalRecords   
    from ApplicationLog as appLog 
    left join LogSource as logSource on appLog.LogSourceId = logSource.LogSourceId 
)  
相關問題