Click here to Skip to main content
15,886,858 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
There is an Invalid Object error while executing query.

Please help !
===============================
SQL
USE HR_Employee
GO
DECLARE   @SQLQuery AS NVARCHAR(MAX)
DECLARE   @PivotColumns AS NVARCHAR(MAX)

SELECT   @PivotColumns= COALESCE(@PivotColumns + ',','') + QUOTENAME(ADA_DATE)
FROM (SELECT DISTINCT  ADA_DATE FROM WrkDailyAttend_Web) AS PivotExample
 
SELECT   @PivotColumns

SET   @SQLQuery = N'SELECT EMP_ID, ' +   @PivotColumns + '
    FROM [HR_Employee].[dbo].[PivotExample] 
    PIVOT (MAX(TIME_IN) FOR ADA_DATE IN (' + @PivotColumns + ')) AS P'

SELECT   @SQLQuery

EXEC sp_executesql @SQLQuery

=======================================
Result :

(1 row(s) affected)

(1 row(s) affected)
Msg 208, Level 16, State 1, Line 1
Invalid object name 'HR_Employee.dbo.PivotExample'.
Posted
Updated 10-Jul-15 16:08pm
v2
Comments
ZurdoDev 10-Jul-15 16:27pm    
It does not know what HR_Employee.dbo.PivotExample is.
Afzaal Ahmad Zeeshan 10-Jul-15 17:01pm    
I believe the table doesn't exist. Re-check!
Suvendu Shekhar Giri 10-Jul-15 22:12pm    
Instead of writing full table name try just replacing it with "PivotExample"

1 solution

Is there a table called PivotExample in the database? If there is, check the writing and the owner and replace dbo with proper owner and correct possible differences in spelling.

If the PivotExample is just the alias you have used in the previous query, you cannot reference a query executed earlier. Executing a query does not create any permanent object into the database meaning that after the query is executed the aliases exit no more.

If the latter is the case, you need to embed a full, proper query inside the @SQLQuery variable.

It's a good idea to use PRINT[^] to see what's inside the variables so you can capture the statement and try running it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900