Home Forums SQL Server 2008 T-SQL (SS2K8) how to replace this cursor with set based solution? RE: how to replace this cursor with set based solution?

  • before I go there, I want to clarify. The parameter @benches is called out in the left query as well as the right.

    I tried this:

    created tvf

    CREATE FUNCTION GetBench (@benches varchar(20))

    RETURNS TABLE

    AS RETURN

    SELECT r.createdate

    FROM Sandbox.dbo.Runs r

    INNER JOIN Sandbox.dbo.runs_machines rm ON rm.runid = r.runid

    INNER JOIN Sandbox.dbo.Machines m ON m.machineid = rm.machineid

    WHERE m.NAME LIKE (@benches + '[0-9][0-9][0-9]')

    AND r.createdate > dateadd(M, - 3, getdate());

    incorporated in APPLY

    DECLARE @benches TABLE (Bench varchar(20));

    INSERT @benches

    values

    ('LabC1'),

    ('LabC2'),

    ('LabC3'),

    ('LabC4');

    INSERT INTO RunsByBench

    SELECT Bench AS BenchName,

    COUNT(CreateDate) AS NumberOfRunsOnBench,

    MAX(CreateDate) AS LastRun

    FROM @benches

    OUTER APPLY

    GetBench (@benches)

    group by Bench;

    select * from RunsByBench;

    got error:

    Msg 206, Level 16, State 2, Line 10

    Operand type clash: table is incompatible with varchar

    So, you say Jeff Moden's splitter article contains the answer, yes? I'm not ready for that article unless it addresses this problem. I currently can make no sense of it and it's long. If you can provide more pointed guidance to whether I should be able to encapsulate the right query into TVF, given that it relies on the same variable as the left side of query, I would appreciate.

    --Quote me