Click here to Skip to main content
15,906,296 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi i have the following stored procedure which will accept 2 params. The first is @CUSTNAME and the second is @YEAR. @CUSTNAME works fine but @YEAR have an error. Below are my codes:

SQL
DECLARE @SQL NVARCHAR(MAX)
DECLARE @params NVARCHAR(MAX)

SELECT @SQL = 'SELECT CustomerName,[0] as Pending, [1] as Save_As_Draft, [2] as Spec_Confirmed, [3] as Request_Revision, [4] as Withdraw, [5] as Pending_RND, [6] as Pending_QC, [7] as Manday_Confirmed, [0]+[1]+[2]+[3]+[4]+[5]+[6]+[7] as Total
FROM
(
    SELECT P.DocStatus,C.CustomerName,P.CustomerID
    FROM dbo.CRF_Project P
    INNER JOIN dbo.CustomerList C
    ON P.CustomerID = C.CustomerID
) p

PIVOT
(
    COUNT (CustomerID) FOR DocStatus IN ([0], [1], [2], [3], [4], [5], [6], [7])

) AS pvt '

SELECT @params = '@CUSTNAME VARCHAR(MAX),
                  @YEAR VARCHAR(MAX)'

IF(@CUSTNAME != '' AND @YEAR = '')
    SELECT @SQL = @SQL + ' WHERE CustomerName IN (' + @CUSTNAME + ')'
ELSE IF(@CUSTNAME = '' AND @YEAR != '')
    SELECT @SQL = @SQL + ' WHERE Year([CreateDate])= (' +@YEAR+ ')' --<--error here
ELSE
    SELECT @SQL = @SQL

EXEC sp_executesql @SQL, @params, @CUSTNAME, @YEAR
END


every time i execute the stored procedure ,it will give me the following error message:
Incorrect column name 'CreateDate'.

My input is for @CUSTNAME 'Osm' and @YEAR '2014'
Posted
Updated 17-Jun-14 21:10pm
v2
Comments
Kornfeld Eliyahu Peter 18-Jun-14 3:30am    
It as simple as it sounds, there is no such column CreateDate in your query!
You probably missed some data or picked up the wrong column name...
Jamie888 18-Jun-14 3:35am    
i have checked it many times, but there is CreateDate in my table. If i put the where clause inside the pivot after the ON P.CustomerID = C.CustomerID, it is ok but it is not what it should be.

1 solution

Quote:
but there is CreateDate in my table
In the table there is but the query hides the table. You use FROM (sub-query) from instead of FROM [table-name]. In this case the only columns in the sub-query accessible in the where clause and not all the columns in the table...
You may change it like this
SQL
FROM
(
    SELECT P.DocStatus,C.CustomerName,P.CustomerID, CreateDate
    FROM dbo.CRF_Project P
    INNER JOIN dbo.CustomerList C
    ON P.CustomerID = C.CustomerID
) p

UPDATE according OPs comments...
SQL
FROM
(
    SELECT P.DocStatus,C.CustomerName,P.CustomerID
    FROM dbo.CRF_Project P
    INNER JOIN dbo.CustomerList C
    ON P.CustomerID = C.CustomerID
    WHERE Year([CreateDate])= (' +@YEAR+ ')'
) p

And
SQL
IF(@CUSTNAME != '' AND @YEAR = '')
    SELECT @SQL = @SQL + ' WHERE CustomerName IN (' + @CUSTNAME + ')'
ELSE IF(@CUSTNAME = '' AND @YEAR != '')
    SELECT @SQL = @SQL + ' WHERE Year([CreateDate])= (' +@YEAR+ ')' --<--error here
ELSE
    SELECT @SQL = @SQL
 
Share this answer
 
v2
Comments
Jamie888 18-Jun-14 3:58am    
yes, it has solved the problem but the output has been changed from the original output that i want. Eg.
CustomerName Total
==================
ABC 10
Jamie888 18-Jun-14 3:59am    
into
CustomerName Total
==================
ABC 3
ABC 5
ABC 2

whereas all the same CustomerName should be grouped together
Kornfeld Eliyahu Peter 18-Jun-14 4:20am    
Probably you have 3 different entries of the same customer with different dates, so when date is part of the query PIVOT behaves differently...
The best solution is to add the where clause about the year to the sub-query (the one inside the from clause) and remove the date column...
Jamie888 18-Jun-14 4:25am    
thanks for your effort and solution, i have change the:
COUNT (CustomerID) FOR DocStatus IN ([0], [1], [2], [3], [4], [5], [6], [7])
into:
COUNT (createYear) FOR DocStatus IN ([0], [1], [2], [3], [4], [5], [6], [7]) and it works fine
Kornfeld Eliyahu Peter 18-Jun-14 4:28am    
Glad to help!

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