• You're hard coding the value as 4. Within a script as you've outlined, you'd have to change the value to 5, 6 or 7 to get those values. If you wanted to make these into parameters, you'd need to change your script to being a stored procedure.

    For example:

    CREATE PROCEDURE MyProc

    (@InCo VARCHAR(MAX))

    AS

    Declare @ReportDateIN [datetime]=GETDATE(),

    @OutCo varchar(max) = 8,

    @IncludeDetailIN [int]= 1,

    @IncludeReleasedIN [int]= 1,

    @IncludeHoldingIN [int]= 1

    DECLARE @EndDate DATETIME

    SET @EndDate = DATEADD(dd, 1, @ReportDateIN)

    INSERT CustodyStatusTable

    SELECT C.CustomerID AS 'CustomerID',

    CASE

    WHEN CH.CoID IN (SELECT KeyValue FROM dbo.fnValuesText(@InCo)) THEN 1

    WHEN CH.CoID IN (SELECT KeyValue FROM dbo.fnValuesText(@OutCo)) THEN 0

    END AS 'InCounty',...

    Then you call the procedure and pass the value you want:

    EXEC MyProc @Inco = 5;

    --or

    EXEC MyProc @Inco = 6

    That will then execute the proc for 5 or 6, as needed.

    Now, questions could be raised about why a VARCHAR(MAX) for a single numeric value, what are you doing with the function, because those look like problematic approaches to data access.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning