Search code examples
sql-serverstored-proceduressql-updatesql-server-2000temp-tables

"The column prefix '*' does not match with a table name or alias name used in the query." while using a temp table


In a SQL Server 2000 stored procedure, I'm trying to perform an update statement to a table variable. It is currently giving me the error: "The column prefix 'WST' does not match with a table name or alias name used in the query."

UPDATE WST
SET
    WST.QtySold = (SELECT SUM(II.QtyShipped)
    FROM #InvoiceItems II
        WHERE II.InvoiceDate >= WST.StartDate
            AND II.InvoiceDate <= WST.EndDate),
    WST.TotalSales = (SELECT SUM(ISNULL(II.QtyShipped, 0) * ISNULL(II.UnitPrice, 0))
        FROM #InvoiceItems II
        WHERE II.InvoiceDate >= WST.StartDate
            AND II.InvoiceDate <= WST.EndDate),
    WST.TotalCost = (SELECT SUM(ISNULL(II.QtyShipped, 0) * ISNULL(II.UnitCost, 0))
        FROM #InvoiceItems II
        WHERE II.InvoiceDate >= WST.StartDate
            AND II.InvoiceDate <= WST.EndDate)
FROM @WeeklySalesTrend WST
WHERE WST.WeekNo = 1

This error only appeared after I created the temp table #InvoiceItems and replaced the Inner Join of two tables with the temp table. Why would changing the two-table inner join out for a temp table cause this error and how do I fix/get around it?


Solution

  • The answer boils down to this, SQL Query Analyzer gave me the wrong location for the error. The error was actually because of an Insert statement 17 lines higher in the code where I was trying to use WST when I never aliased it up there. I don't know if this was a glitch/bug on Query Analyzer's side or if the stored procedure optimized to more lines and there its location was off. Either way, the problem has been fixed. Thanks for the suggestions and quick responses though and sorry for wasting your time.