Error in Stored Procedure

  • Hi All,

    Please check my stored procedure.I am getting errors

    Invalid column name 'EmpNo'.Invalid column name 'Totalhours'.

    I think there is error i closing quotations i tried it but unable to figure it out.

    FDiv is a function in SP

    ALTER PROCEDURE [dbo].[SAR_Sp_GetScheduledHours](@date1 DATETIME, @date2 DATETIME, @date3 DATETIME, @date4 DATETIME)

    AS

    DECLARE @query VARCHAR(MAX)

    BEGIN

    SET @query = 'SELECT '+ CHAR(39) + 'Hours' + CHAR(39) + ' AS HeadCount, [' + CONVERT(VARCHAR,@date1,101) + '], [' + CONVERT(VARCHAR,@date2,101)+ '], [' + CONVERT(VARCHAR,@date3,101) + '], [' + CONVERT(VARCHAR,@date4,101) + ']' +

    'FROM

    (SELECT convert(varchar,weekstartdate,110) as StartDate,'+

    convert(varchar,dbo.FDiv(Count(Case When Totalhours<='24:0' Then EmpNo end),2)*100)+'+% as Less,'

    +convert(varchar,dbo.FDiv(Count(Case when Totalhours>'24:0' and Totalhours<='48:0' Then EmpNo end),2)*100)+'+% as b/w,'

    +convert(varchar,dbo.FDiv(Count(Case when Totalhours>'48:0' Then EmpNo end),2)*100)+'+% as More

    FROM Test2

    AS SourceTable

    PIVOT

    (

    max(Less),max(b/w),max(More)

    FOR StartDate IN (' + '[' + CONVERT(VARCHAR,@date1,101) + '], [' + CONVERT(VARCHAR,@date2,101)+ '], [' + CONVERT(VARCHAR,@date3,101) + '], [' + CONVERT(VARCHAR,@date4,101) + ']' + ')

    ) AS PivotTable'

    EXEC(@query)

    END

  • My guess is that your table does not have the columns that is says it can't find.

    Invalid column name 'EmpNo'.

    Invalid column name 'Totalhours'.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply