Click here to Skip to main content
16,019,618 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this is my query:
SQL
DECLARE @columns VARCHAR(8000)

SELECT @columns = COALESCE(@columns + ',[' + cast(Date as varchar) + ']',
'[' + cast(Date as varchar)+ ']')
FROM Absence
GROUP BY Date DECLARE @query VARCHAR(8000) SET @query = ' SELECT * FROM (SELECT Name , Code, Section, Date FROM Absence) up PIVOT (COUNT(Code) FOR Date IN (' + @columns + '))  AS p WHERE Section LIKE A ' EXECUTE(@query)


it gives "Invalid column name 'A'"
Posted
Updated 22-May-18 6:10am
v2

1 solution

You have delimit a single quote for LIKE:
SQL
GROUP BY Date DECLARE @query VARCHAR(8000) SET @query = ' SELECT * FROM (SELECT Name , Code, Section, Date FROM Absence) up PIVOT (COUNT() FOR Date IN (' + @columns + '))  AS p WHERE Section LIKE ''A'' ' EXECUTE(@query)
 
Share this answer
 
Comments
XRushdy 20-Sep-13 3:16am    
Thank you very much Mehdi Gholam,
It's working.
I'm trying to solve this error for days,but now it's solved.
Maciej Los 24-Sep-13 1:51am    
+5
Mehdi Gholam 24-Sep-13 11:34am    
Thanks Maciej!
Jamie888 18-Jun-14 2:48am    
sir, what if in the where clause is "WHERE Year(CreateDate) = ' +@YEAR+ '", it would say invalid column name CreateDate but i have checked it with nothing wrong.

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