0

I have a stored procedure that when I run the SQL hard coding the parameter values it runs fine and returns records as expected.

However when I run it as

exec usp_ChangeFormReport 
        @CFType = -1,
        @RG = 11,
        @FromDate = '01/01/2017',
        @ToDate = '11/26/2018',
        @RC = 'Receive',
        @CreatedByUser = -1,
        @AssignedToUser = '-1',
        @CFStatus = 'All', 
        @NewToPMS = 'A',
        @execFromDate = NULL,
        @execToDate = NULL,
        @notifyMedPoint = 'All',
        @requireTraining = 'All'

It fails with an error:

Msg 245, Level 16, State 1, Procedure usp_ChangeFormReport, Line 117 [Batch Start Line 0]
Conversion failed when converting the varchar value 'No' to data type int.

Here's the stored procedure with the same parameters hard coded. I'm at a loss to understand let alone explain why it's failing in one case, yet runs just fine as a straight SQL script with the exact same parameters. I can't see anywhere I'm trying to pass a varchar as an int, and none of the records returned show anything unexpected.

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- ALTER PROCEDURE [dbo].[usp_ChangeFormReport]
--  (
Declare        @CFType   INT = -1
Declare        @RG     INT = 11
Declare        @FromDate   Varchar (30) = '01/01/2017'
Declare        @ToDate   Varchar (30) = '11/26/2018'
Declare        @RC     Varchar (10) = 'Receive'
Declare        @CreatedByUser  INT = -1
Declare        @AssignedToUser Varchar (20) = -1
Declare        @CFStatus   Varchar (20) = 'All'
Declare        @NewToPMS        Char(1) = 'A'
Declare        @execFromDate    Varchar (30) = NULL
Declare        @execToDate    Varchar (30) = NULL
Declare        @notifyMedPoint    Varchar (3) = 'ALL'
Declare        @requireTraining Varchar (3) = 'ALL'
--)
--AS
    -- validate date parameters
    If (isdate (@execFromDate) = 0)
        set @execFromDate = NULL

    If (isdate (@execToDate) = 0)
        set @execToDate = NULL

    IF isdate ( @FromDate ) = 0
        OR isdate ( @ToDate ) = 0
        OR cast ( @FromDate AS Datetime ) > cast ( @ToDate AS Datetime )
        OR cast ( @execFromDate AS Datetime ) > cast ( @execToDate AS Datetime )
    BEGIN
        SELECT    NULL                    AS changeformid,
                NULL                    AS isRush,
                NULL                    AS providerNotificationDate,
                NULL                    AS executedDate,
                NULL                    AS notifyMedPoint,
                NULL                    AS doesProviderRequireTraining,
                NULL                    AS displaycontractid,
                NULL                    AS contractentity,
                NULL                    AS changeformtype,
                NULL                    AS changeformstatus,
                'Date Parameter Error'    AS networklocname,
                NULL                    AS routinggroup,
                NULL                    AS receivedfromrg,
                NULL                    AS resolutiondate,
                NULL                    AS contractID,    
                NULL                    AS taxID,            
                NULL                    AS tat,
                NULL                    AS isBrandNewProv,
                NULL                    AS effectiveDate,
                NULL                    AS agreementType,
                NULL                    AS createdByUserName                
        RETURN
    END
    --create temp table to capture change forms that are completed per department
    IF object_id ( 'tempdb..#CFcompletedlist', 'U' ) IS NOT NULL
        DROP TABLE #cfcompletedlist

    CREATE TABLE #cfcompletedlist
    (
        changeformid  Int,
        isRush char(1),
        providerNotificationDate varchar(10),
        executedDate    varchar(10),
        notifyMedPoint    char(1),
        doesProviderRequireTraining char(1),
        displaycontractid Varchar (10),
        contractentity  Varchar (100),
        changeformtype  Varchar (100),
        changeformstatus  Varchar (20),
        networklocname  Varchar (1000),
        --resolutionname varchar(100),
        routinggroup  Varchar (100),
        --routinggroupuser varchar(100),
        receivedfromrg  Varchar (100),
        resolutiondate  Varchar (10),
        contractID    int,
        taxID varchar(20),
        createdByUserName varchar(100)
    )

    IF object_id ( 'tempdb..#CFnewassignment', 'U' ) IS NOT NULL
        DROP TABLE #cfnewassignment

    CREATE TABLE #cfnewassignment
    (
        changeformid  Int,
        isRush char(1),
        providerNotificationDate varchar(10),
        executedDate    varchar(10),
        notifyMedPoint    char(1),
        doesProviderRequireTraining char(1),
        displaycontractid Varchar (10),
        contractentity  Varchar (100),
        changeformtype  Varchar (100),
        changeformstatus  Varchar (20),
        networklocname  Varchar (1000),
        --resolutionname varchar(100),
        routinggroup  Varchar (100),
        --routinggroupuser varchar(100),
        receivedfromrg  Varchar (100),
        resolutiondate  Varchar (10),
        contractID    int,
        taxID varchar(20),
        createdByUserName varchar(100),
        tat    Int,
        isBrandNewProv char(1),
        -- SUK/Ticket#63828/2010-12-09: Added parameters below
        effectiveDate DATETIME,
        agreementType Varchar(100)
        -- end
     )
    IF @RC = 'Receive'
    BEGIN
        INSERT INTO #cfcompletedlist
        SELECT DISTINCT 
            cf.changeformid,
            IsNull(cf.isRush, 'N') AS isRush,
            CONVERT(varchar(10), cf.providerNotificationDate , 101) AS providerNotificationDate,
            CONVERT(varchar(10), cf.executedDate , 101) AS executedDate,
            IsNull(cf.notifyMedPoint, 'N') AS notifyMedPoint,
            IsNull(cf.doesProviderRequireTraining, 'N') AS doesProviderRequireTraining,
            dbo.fungetcontractdisplayid ( cf.contractid ) AS displaycontractid,
            contractentity,
            changeformtypename,
            cf.changeformstatus,
            dbo.fungetcontractnetworklocations ( cf.contractid ) AS networklocname,
            --ResolutionName,
            rg.routinggroupname            AS routinggroup,
            --tu.firstname+', '+tu.lastname AS RoutingGroupUser,
            rg2.routinggroupname           AS receivedfromrg,
            CONVERT(varchar(10), cfr.resolutiontime, 101) AS resolutiondate,
            cf.contractID,
            co.taxID,
            tu.firstname + ' ' + tu.lastname AS createdByUserName
      FROM tblchangeform cf
        LEFT JOIN tblchangeformresolution cfr
            ON cf.changeformid = cfr.changeformid
            And cfr.resolutiontime = (    Select max(resolutiontime) 
                                        From tblchangeformresolution 
                                        Where changeformid = cfr.changeformid 
                                        and resolutionValue = cfr.resolutionValue) 
        LEFT JOIN tblchangeformtype cft
            ON cft.changeformtypeid = cf.changeformtypeid
        LEFT JOIN tblroutinggroup rg
            ON rg.routinggroupid = cfr.resolutionvalue
        LEFT JOIN tblroutinggroup rg2
            ON rg2.routinggroupid = cfr.resolvedbygroupid
        LEFT JOIN tblresolution re
            ON re.resolutionid = cfr.resolutionid
        LEFT JOIN tblcontracts co
            ON co.contractid = cf.contractid
        LEFT JOIN tblUsers tu ON tu.userid=cf.createdByUserID
        WHERE 
        ( 
            cf.changeformstatus = @CFStatus
          OR @CFStatus = 'All' 
        )
        AND 
        (
            IsNull(cf.notifyMedPoint, 'N') = @notifyMedPoint
            OR @notifyMedPoint = 'All'
        )
        AND 
        (
            (
                @execFromDate IS NOT NULL AND 
                @execToDate IS NOT NULL AND 
                cf.executedDate BETWEEN cast ( @execFromDate AS Datetime ) 
                    AND cast ( @execToDate AS Datetime )
            )
            OR
            (
                @execFromDate IS NULL OR @execToDate IS NULL
            )
        )
        AND 
        (
            IsNull(cf.doesProviderRequireTraining, 'N') = @requireTraining
            OR @requireTraining = 'All'
        )
        AND cfr.resolutionid IN ( 18, 25, 27 )
        AND 
        ( 
            cft.changeformtypeid = @CFType
            OR @CFType = - 1 
        )
        AND cfr.resolutionvalue = @RG
        AND cfr.resolutiontime BETWEEN 
            cast ( @FromDate AS Datetime ) 
            AND dateadd ( DAY, 1, cast ( @ToDate AS Datetime ))
        GROUP BY 
            cf.changeformid, 
            cf.changeformstatus, 
            cf.isRush,
            cf.providerNotificationDate,
            cf.executedDate,
            cf.notifyMedPoint,
            cf.doesProviderRequireTraining,
            co.contractentity, 
            cft.changeformtypename, 
            cf.contractid,
            co.taxID, 
            rg.routinggroupname, 
            rg2.routinggroupname,
                tu.firstname,
                tu.lastname,
            cfr.resolutiontime
        ORDER BY cf.changeformid, cf.contractid

        INSERT INTO #cfnewassignment
        SELECT a.*, tat = '', b.isBrandNewProvider,
            -- SUK/Ticket#63828/2010-12-09: Added fields below
            co.EffectiveDate,
            at.Name as agreementType
        FROM #cfcompletedlist a
        LEFT JOIN tblchangeform b
            ON a.changeformid = b.changeformid
        -- SUK/Ticket#63828/2010-12-09: Added join below
        LEFT JOIN tblcontracts co
            ON co.contractid = b.contractid        
        LEFT JOIN dbo.tblAgreementTypes at
            ON co.AgreementTypeID = at.AgreementTypeID
        WHERE 
            ( 
                b.createdbyuserid = @CreatedByUser
                OR @CreatedByUser = - 1 
            ) --OR @CreatedByUser=-2)
            AND
            (
                b.isBrandNewProvider = @NewToPMS
                OR @NewToPMS = 'A'
            )
        ORDER BY a.changeformid

        SELECT distinct *
        FROM #cfnewassignment
        WHERE 
            @AssignedToUser = '-1'
          OR changeformid IN
            (
                SELECT DISTINCT a.changeformid --,resolutionvalue
                FROM #cfnewassignment a
                INNER JOIN tblchangeformresolution b
                    ON a.changeformid = b.changeformid
                WHERE resolutionid = 26
                 AND resolutionvalue = @AssignedToUser
            )
    END
    ELSE
        IF @RC = 'Create'
        BEGIN
            INSERT INTO #cfcompletedlist
            SELECT DISTINCT 
                cf.changeformid,
                IsNull(cf.isRush, 'N') AS isRush,
                CONVERT(varchar(10), cf.providerNotificationDate , 101) AS providerNotificationDate,
                CONVERT(varchar(10), cf.executedDate , 101) AS executedDate,
                IsNull(cf.notifyMedPoint, 'N') AS notifyMedPoint,
                IsNull(cf.doesProviderRequireTraining, 'N') AS doesProviderRequireTraining,
                dbo.fungetcontractdisplayid ( cf.contractid ) AS displaycontractid,
                contractentity,
                changeformtypename AS changeformtype,
                cf.changeformstatus,
                dbo.fungetcontractnetworklocations ( cf.contractid ) AS 
                    networklocname,
                --ResolutionName,
                rg.routinggroupname            AS routinggroup,
                --tu.firstname+', '+tu.lastname AS RoutingGroupUser,
                NULL                 AS receivedfromrg,
                min ( convert ( Varchar (10), cfr.resolutiontime, 101 )) AS resolutiondate, --, TAT =''
                cf.contractid,
                co.taxID,
                tu.firstname + ' ' + tu.lastname AS createdByUserName
          FROM tblchangeform cf
            LEFT JOIN tblchangeformresolution cfr
                ON cf.changeformid = cfr.changeformid
                And cfr.resolutiontime = (    Select max(resolutiontime) 
                                            From tblchangeformresolution 
                                            Where changeformid = cfr.changeformid 
                                            and resolutionValue = cfr.resolutionValue) 
            LEFT JOIN tblchangeformtype cft
                    ON cft.changeformtypeid = cf.changeformtypeid
            LEFT JOIN tblroutinggroup rg
                    ON rg.routinggroupid = cfr.resolvedbygroupid
            LEFT JOIN tblresolution re
                    ON re.resolutionid = cfr.resolutionid
            LEFT JOIN tblcontracts co
                    ON co.contractid = cf.contractid
            LEFT JOIN tblUsers tu ON tu.userid = cf.createdByUserID
            WHERE 
            ( 
                cf.changeformstatus = @CFStatus
                OR @CFStatus = 'All' 
            )
            AND 
            (
                IsNull(cf.notifyMedPoint, 'N') = @notifyMedPoint
                OR @notifyMedPoint = 'All'
            )
            AND 
            (
                (
                    @execFromDate IS NOT NULL AND 
                    @execToDate IS NOT NULL AND 
                    cf.executedDate BETWEEN cast ( @execFromDate AS Datetime ) 
                        AND cast ( @execToDate AS Datetime )
                )
                OR
                (
                    @execFromDate IS NULL OR @execToDate IS NULL
                )
            )
            AND 
            (
                IsNull(cf.doesProviderRequireTraining, 'N') = @requireTraining
                OR @requireTraining = 'All'
            )
          AND cfr.resolutionid = 27
          AND (cft.changeformtypeid = @CFType OR @CFType = - 1 )
          AND cfr.resolvedbygroupid = @RG
          AND cfr.resolutiontime BETWEEN cast ( @FromDate AS Datetime ) 
                AND dateadd ( DAY, 1, cast ( @ToDate AS Datetime ))
            GROUP BY 
                cf.changeformid, 
                cf.changeformstatus, 
                cf.isRush,
                cf.providerNotificationDate,
                cf.executedDate,
                cf.notifyMedPoint,
                cf.doesProviderRequireTraining,
                contractentity, 
                changeformtypename, 
                cf.contractid,
                co.taxID,
                rg.routinggroupname,
                tu.firstname,
                tu.lastname,
                cfr.resolutiontime
            ORDER BY cf.changeformid, cf.contractid

            SELECT distinct 
                a.*,
                tat = '', b.isBrandNewProvider,
                -- SUK/Ticket#63828/2010-12-09: Added fields below
                co.EffectiveDate,
                at.Name as agreementType
            FROM #cfcompletedlist a
            LEFT JOIN tblchangeform b
                ON a.changeformid = b.changeformid
            -- SUK/Ticket#63828/2010-12-09: Added join below
            LEFT JOIN tblcontracts co
                ON co.contractid = b.contractid        
            LEFT JOIN dbo.tblAgreementTypes at
                ON co.AgreementTypeID = at.AgreementTypeID
            WHERE 
            ( 
                b.createdbyuserid = @CreatedByUser
                OR @CreatedByUser = - 1 
            ) --OR @CreatedByUser=-2)
            AND
            (
                b.isBrandNewProvider = @NewToPMS
                OR @NewToPMS = 'A'
            )
            ORDER BY a.changeformid
        END

        ELSE
        -- completed
        BEGIN
            INSERT INTO #cfcompletedlist
            SELECT DISTINCT 
                cf.changeformid,
                IsNull(cf.isRush, 'N') AS isRush,
                CONVERT(varchar(10), cf.providerNotificationDate , 101) AS providerNotificationDate,
                CONVERT(varchar(10), cf.executedDate , 101) AS executedDate,
                IsNull(cf.notifyMedPoint, 'N') AS notifyMedPoint,
                IsNull(cf.doesProviderRequireTraining, 'N') AS doesProviderRequireTraining,
                dbo.fungetcontractdisplayid ( cf.contractid ) AS displaycontractid,
                contractentity,
                changeformtypename,
                cf.changeformstatus,
                dbo.fungetcontractnetworklocations ( cf.contractid ) AS networklocname,
                --ResolutionName,
                rg.routinggroupname            AS routinggroup,
                --tu.firstname+', '+tu.lastname AS RoutingGroupUser,
                NULL                 AS receivedfromrg,
                max ( convert ( Varchar (10), cfr.resolutiontime, 101 )) AS resolutiondate,
                cf.contractid,
                co.taxID,
                tu.firstname + ' ' + tu.lastname AS createdByUserName
          FROM tblchangeform cf
            LEFT JOIN tblchangeformresolution cfr
                ON cf.changeformid = cfr.changeformid
                And cfr.resolutiontime = (    Select max(resolutiontime) 
                                            From tblchangeformresolution 
                                            Where changeformid = cfr.changeformid 
                                            and (
                                                (resolutionValue = cfr.resolutionValue)
                                                    OR 
                                                (cfr.resolutionValue is null and cfr.resolutionid = 28) --completed
                                            )) 
            LEFT JOIN tblchangeformtype cft
                    ON cft.changeformtypeid = cf.changeformtypeid
            LEFT JOIN tblroutinggroup rg
                    ON rg.routinggroupid = cfr.resolvedbygroupid
            LEFT JOIN tblresolution re
                    ON re.resolutionid = cfr.resolutionid
            LEFT JOIN tblcontracts co
                    ON co.contractid = cf.contractid
            LEFT JOIN tblUsers tu ON tu.userid = cf.createdByUserID
            WHERE 
            ( 
                cf.changeformstatus = @CFStatus
                OR @CFStatus = 'All' 
            )
            AND 
            (
                IsNull(cf.notifyMedPoint, 'N') = @notifyMedPoint
                OR @notifyMedPoint = 'All'
            )
            AND 
            (
                (
                    @execFromDate IS NOT NULL AND 
                    @execToDate IS NOT NULL AND 
                    cf.executedDate BETWEEN cast ( @execFromDate AS Datetime ) 
                        AND cast ( @execToDate AS Datetime )
                )
                OR
                (
                    @execFromDate IS NULL OR @execToDate IS NULL
                )
            )
            AND 
            (
                IsNull(cf.doesProviderRequireTraining, 'N') = @requireTraining
                OR @requireTraining = 'All'
            )
          AND 
            ( 
                cfr.resolutionid IN ( 25, 28, 29 )
                OR 
                ( 
                    cfr.resolutionid = 18
                    AND cf.changeformstatus = 'Canceled' 
                )
            )
          AND 
            ( 
                cft.changeformtypeid = @CFType
                OR @CFType = - 1 
            )
          AND cfr.resolvedbygroupid = @RG
          AND cfr.resolutiontime BETWEEN cast ( @FromDate AS Datetime ) 
                AND dateadd ( DAY, 1, cast ( @ToDate AS Datetime ))
            GROUP BY 
                cf.changeformid, 
                cf.changeformstatus, 
                cf.isRush,
                cf.providerNotificationDate,
                cf.executedDate,
                cf.notifyMedPoint,
                cf.doesProviderRequireTraining,
                contractentity, 
                changeformtypename, 
                cf.contractid,
                co.taxId,
                rg.routinggroupname,
                tu.firstname,
                tu.lastname,
                cfr.resolutiontime
            ORDER BY cf.changeformid, cf.contractid

            IF object_id ( 'tempdb..#CFgroupbyRG', 'U' ) IS NOT NULL
                DROP TABLE #cfgroupbyrg

            CREATE TABLE #cfgroupbyrg
            (
                changeformid   Int,
                routinggroupname Varchar (100),
                tat      Int
            )

            INSERT INTO #cfgroupbyrg
            SELECT 
                changeformid,
                routinggroupname,
                sum ( isnull ( tad, 0 )) AS tad
            FROM tblchangeformresolution a
            INNER JOIN tblroutinggroup b
                ON a.resolvedbygroupid = b.routinggroupid
            --Where a.resolutionid IN ( 25, 27, 18 )
            GROUP BY changeformid, routinggroupname

            INSERT INTO #cfnewassignment
            SELECT 
                a.*,
                b.tat, 
                c.isBrandNewProvider,
                -- SUK/Ticket#63828/2010-12-09: Added fields below
                co.EffectiveDate,
                at.Name as agreementType
            FROM #cfcompletedlist a
            INNER JOIN #cfgroupbyrg b
                ON a.changeformid = b.changeformid
                AND b.routinggroupname = a.routinggroup
            LEFT JOIN tblchangeform c
                ON c.changeformid = a.changeformid
            -- SUK/Ticket#63828/2010-12-09: Added join below
            LEFT JOIN tblcontracts co
                ON co.contractid = c.contractid        
            LEFT JOIN dbo.tblAgreementTypes at
                ON co.AgreementTypeID = at.AgreementTypeID
            WHERE 
                ( 
                    c.createdbyuserid = @CreatedByUser
                    OR @CreatedByUser = - 1 
                ) -- OR @CreatedByUser=-2)
                AND
                (
                    c.isBrandNewProvider = @NewToPMS
                    OR @NewToPMS = 'A'
                )
            ORDER BY a.changeformid

            SELECT distinct *
            FROM #cfnewassignment
            WHERE 
                @AssignedToUser = '-1'
              OR changeformid IN
                    (
                        SELECT DISTINCT a.changeformid --,resolutionvalue
                          FROM #cfnewassignment a
                                 INNER JOIN tblchangeformresolution b
                                         ON a.changeformid = b.changeformid
                         WHERE resolutionid = 26
                         AND resolutionvalue = @AssignedToUser
                    )
        END
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
CraigBob
  • 147
  • 1
  • 5
  • 15
  • 2
    Something I do notice is that when passing values through parameters you assign `@AssignedToUser='-1'` but hardcoding you do `@AssignedToUser=-1` (ie- `VARCHAR` value vs `INT` value. – squillman Nov 28 '18 at 17:51
  • As @squillman pointed out, there is a difference although t-sql will implicitly cast in most cases. You proc does seem to mix'n'match a bit. It's worth cleaning all that up and retesting. – Alan Schofield Nov 28 '18 at 18:03
  • *"Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself."* You need to narrow down the problem to a [mcve]. A 300-line Stored Proc is not minimal, the lack of table and function definitions is not complete. Which part of those 300 lines is it coming from? Try removing some bits and see if it still happens. You will probably find it yourself before you get to a MCVE, but if not, come back and [edit] your question. – Richardissimo Nov 28 '18 at 20:25
  • 1
    Unrelated tips: Please do yourself a favour and stop passing dates around as varchars. Also consider using DATE rather than DateTime. And always [prefer DateTime2 to DateTime](https://stackoverflow.com/questions/1334143/datetime2-vs-datetime-in-sql-server). Beware [using between with datetime](https://sqlblog.org/2009/10/16/bad-habits-to-kick-mis-handling-date-range-queries). – Richardissimo Nov 28 '18 at 20:34
  • @AlanSchofield, You make a valid point, and I'd already tried that with no change in behavior.SQL still works, exec did not. Richard 'll make those changes and re-test. – CraigBob Nov 28 '18 at 21:39

0 Answers0