I want to use "Order By" in Last line of below Stored Procedure. But When I try to do so it shows me error.
The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions
SP is as below:
ALTER PROCEDURE [Invoice].[usp_tbl_Request_Query_MyInTray_Test]
@LoginUser nvarchar(50),
@Status tinyint = Null,
@SearchValue nvarchar(50) = Null ,
@ProcessInstanceID nvarchar(max),
@Role as nvarchar(50)
AS
BEGIN
SET NOCOUNT ON;
If (LTRIM(@SearchValue) = '')
Set @SearchValue = Null
Select Distinct
R.RequestID,
R.RequisitionNo,
R.BarcodeNo,
R.IsThreeWayMatch,
R.IsUrgent,
R.TotalInvoiceAmount,
X.BuyerNo,
W.CreatedDate,
W.Action,
CASE
WHEN (R.IsThreeWayMatch in (1))
THEN X.BuyerName
ELSE R.Company
END AS BuyerName,
X.SupplierNo,
X.SupplierName,
X.InvoiceNo,
S.RequestStatus,
R.[Status]
From Invoice.tbl_Request (NOLOCK) R
Left Join Invoice.tbl_Xml(NOLOCK) X On X.XmlID = R.XmlID
Left Join Invoice.tbl_WorkflowHistory(NOLOCK) W On W.RequestID = R.RequestID
Left Join Invoice.tbl_RequestStatus(NOLOCK) S On S.RequestStatusNo = R.[Status]
Where W.CreatedDate IN
(SELECT MAX(W.CreatedDate)
FROM
Invoice.tbl_Request (NOLOCK) R
Left Join Invoice.tbl_Xml(NOLOCK) X On X.XmlID = R.XmlID
Left Join Invoice.tbl_WorkflowHistory(NOLOCK) W On W.RequestID = R.RequestID
Left Join Invoice.tbl_RequestStatus(NOLOCK) S On S.RequestStatusNo = R.[Status]
WHERE
R.[Status] = IsNull(@Status, R.[Status])
And (
((R.FirstApprover = @LoginUser and R.[Status] = 3)
Or (R.SecondApprover = @LoginUser and R.[Status] = 4)) or
R.ProcessInstanceID In (Select * from [dbo].[SplitString] (@ProcessInstanceID, ','))
Or (R.CreatedBy = @LoginUser And [Status] In (0)) -- Return Draft Request for Submitter
)
And(R.[Status] <> 6 and (W.[Action]='Submit')) -- Dummy Vendor will not be returned
OR (R.[Status] = 24 and (@role ='Admin' or @Role='APAdmin') and (W.[Action]='Submit'))
OR (R.[Status] = 25 and (@role ='Admin' or @Role='APAdmin') and (W.[Action]='Submit'))
OR (R.[Status] = 7 and (@role ='AP' or @role ='Admin' or @Role='APAdmin')and (W.[Action]='Submit'))
And ((IsNull(R.RequisitionNo, '') Like '%' + Coalesce(@SearchValue, R.RequisitionNo, '') + '%')
Or (IsNull(R.BarcodeNo, '') Like '%' + Coalesce(@SearchValue, R.BarcodeNo, '') + '%')
Or (IsNull(R.Company, '') Like '%' + Coalesce(@SearchValue, R.Company, '') + '%')
Or (IsNull(X.SupplierName, '') Like '%' + Coalesce(@SearchValue, X.SupplierName, '') + '%')
Or (IsNull(X.InvoiceNo, '') Like '%' + Coalesce(@SearchValue, X.InvoiceNo, '') + '%'))
Group By R.RequisitionNo)
UNION ALL
Select Distinct
R.RequestID,
R.RequisitionNo,
R.BarcodeNo,
R.IsThreeWayMatch,
R.IsUrgent,
R.TotalInvoiceAmount,
X.BuyerNo,
W.CreatedDate,
W.Action,
CASE
WHEN (R.IsThreeWayMatch in (1))
THEN X.BuyerName
ELSE R.Company
END AS BuyerName,
X.SupplierNo,
X.SupplierName,
X.InvoiceNo,
S.RequestStatus,
R.[Status]
From Invoice.tbl_Request (NOLOCK) R
Left Join Invoice.tbl_Xml(NOLOCK) X On X.XmlID = R.XmlID
Left Join Invoice.tbl_WorkflowHistory(NOLOCK) W On W.RequestID = R.RequestID
Left Join Invoice.tbl_RequestStatus(NOLOCK) S On S.RequestStatusNo = R.[Status]
Where W.CreatedDate IN
(SELECT MAX(W.CreatedDate)
FROM
Invoice.tbl_Request (NOLOCK) R
Left Join Invoice.tbl_Xml(NOLOCK) X On X.XmlID = R.XmlID
Left Join Invoice.tbl_WorkflowHistory(NOLOCK) W On W.RequestID = R.RequestID
Left Join Invoice.tbl_RequestStatus(NOLOCK) S On S.RequestStatusNo = R.[Status]
WHERE
R.[Status] = IsNull(@Status, R.[Status])
And (
((R.FirstApprover = @LoginUser and R.[Status] = 3)
Or (R.SecondApprover = @LoginUser and R.[Status] = 4)) or
R.ProcessInstanceID In (Select * from [dbo].[SplitString] (@ProcessInstanceID, ','))
Or (R.CreatedBy = @LoginUser And [Status] In (0)) -- Return Draft Request for Submitter
)
And(R.[Status] <> 6 and (W.[Action] = 'Resubmit'))-- Dummy Vendor will not be returned
OR (R.[Status] = 24 and (@role ='Admin' or @Role='APAdmin') and (W.[Action] = 'Resubmit'))
OR (R.[Status] = 25 and (@role ='Admin' or @Role='APAdmin') and (W.[Action] = 'Resubmit'))
OR (R.[Status] = 7 and (@role ='AP' or @role ='Admin' or @Role='APAdmin') and (W.[Action] = 'Resubmit'))
And ((IsNull(R.RequisitionNo, '') Like '%' + Coalesce(@SearchValue, R.RequisitionNo, '') + '%')
Or (IsNull(R.BarcodeNo, '') Like '%' + Coalesce(@SearchValue, R.BarcodeNo, '') + '%')
Or (IsNull(R.Company, '') Like '%' + Coalesce(@SearchValue, R.Company, '') + '%')
Or (IsNull(X.SupplierName, '') Like '%' + Coalesce(@SearchValue, X.SupplierName, '') + '%')
Or (IsNull(X.InvoiceNo, '') Like '%' + Coalesce(@SearchValue, X.InvoiceNo, '') + '%'))
Group By R.RequisitionNo); // "Order By R.RequisitionNo Desc" not working
END
Due to this I am unable to fetch the Last inserted record. It gives me the 1st record but I want the Last inserted record (in 2nd query of SP)