• One thing you could try is building a single dynamic string, like this:

    create table #T (

    ID int identity primary key,

    Name varchar(100));

    insert into #T (Name)

    select 'Ink Inc' union all

    select 'ACME Co';

    declare @SQL varchar(max);

    select @SQL = coalesce(

    @SQL + ';' + 'select * from #T where id = ' + cast(id as varchar(10)),

    'select * from #T where id = ' + cast(id as varchar(10)))

    from #T;

    print @SQL;

    Result:

    select * from #T where id = 1;select * from #T where id = 2

    You can build complex queries that way from data in tables, and then instead of "print", used "exec()". That's going to be faster than a cursor on that part.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon