• well, the obvious fix is to use an Inline Table Valued function instead of a scalar function;

    i was able to convert it easily, but the example usage you pasted is a subset of the code...i have no idea where the @st value came from, but we'd want to use a column name instead of that variable to speed things up.

    CREATE FUNCTION [dbo].[CheckDayType_ITVF]

    (@Data SMALLDATETIME)

    RETURNS TABLE

    AS

    RETURN

    SELECT

    CASE

    WHEN Feriado = 0 AND DiaSemana = 1

    THEN 0

    WHEN Feriado = 0 AND DiaSemana = 0

    THEN 1

    WHEN Feriado = 1 AND DiaSemana = 1

    THEN 2

    WHEN Feriado = 1 AND DiaSemana = 0

    THEN 3

    END AS DayType

    FROM dbo.Calendar

    WHERE Data = @data

    GO

    if you can show the fuller code that defines the @st variable, we could help change the code to do a single set based fix

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!