I hope this is formatted right. I’m having a problem with this procedure. I’ve stripped out the declaration and what you see is the main body. All I am trying to do is create a table variable (in SQL SERVER 2000) and pull some metrics from the data contained within.
DECLARE @InvoiceData TABLE
(
UserID INT
, FullName VARCHAR
, StatusId INT
, Status VARCHAR
, DateCreated DATETIME
)
--INSERT INTO @InvoiceData(UserID, FullName, StatusId, Status, DateCreated)
SELECT
tblInvoiceInstructions.AccountantUserId
, Users.Name + ', ' + Users.First_Name AS FullName
, tblInvoice.CurrentWorkflowStatusId AS StatusID
, dbo.ufnGetGenericCodeLongDescText(tblInvoice.CurrentWorkflowStatusId,GETDATE()) AS Status
, CONVERT(VARCHAR, tblInvoice.DateCreated, 101) AS DateCreated
FROM tblInvoiceInstructions
JOIN tblInvoice ON tblInvoice.Project_ID = tblInvoiceInstructions.ProjectID
LEFT JOIN dbo.Users ON tblInvoiceInstructions.AccountantUserId = Users.UserId
WHERE tblInvoice.DateCreated BETWEEN @StartDate AND @EndDate
SELECT FullName, InvoicesAssigned, InvoicesApproved, InvoicesRejected, InvoicesAssigned-InvoicesApproved-InvoicesRejected AS InvoicesRemaining
FROM
(
SELECT DISTINCT FullName
,ISNULL((SELECT COUNT(t2.StatusID) FROM @InvoiceData t2 WHERE t2.AccountantUserId = T1.AccountantUserId GROUP BY FullName),0) AS InvoicesAssigned
,ISNULL((SELECT COUNT(t2.StatusID) FROM @InvoiceData t2 WHERE t2.StatusID = 394 AND t2.AccountantUserId = T1.AccountantUserId GROUP BY FullName),0) AS InvoicesApproved
,ISNULL((SELECT COUNT(t2.StatusID) FROM @InvoiceData t2 WHERE t2.StatusID = 388 AND t2.AccountantUserId = T1.AccountantUserId GROUP BY FullName),0) AS InvoicesRejected
FROM @InvoiceData T1
) AS InnerTable
When I execute the stored procedure, it gives me the following error 6 times:
Msg 207, Level 16, State 3, Procedure uspReportInvoicesProcessedAndWaiting, Line 31
Invalid column name ‘AccountantUserId’.
Line 31 is the outer select where I am pulling values for FullName, InvoicesAssigned, InvoicesApproved. and so on. Am I aliasing the tables incorrectly?
Check this line in the second query.
NOTE: @InvoiceData Alias t2 has no AccountantUserId column defined.
t2.AccountantUserId does not exist.
Check the @InvoiceData table definition