• You haven't given us much to go on, so this is an educated guess.

    Create your tally table like Gail suggested above. Then you'll need to join it to your base table. Since you haven't provided one, I'll create one like this and insert your rows.

    create table #t (

    A integer,

    B integer)

    insert into #t(A, B)

    select 1, 22

    union all

    select 5, 6;

    Once you have that, you can join it to the tally table to produce the query results you want.

    select t.N A, isnull(#t.B, 0) B

    from dbo.Tally t

    left outer join #t on t.N = #t.A

    where t.N <= (select MAX(a) from #t)

    order by t.N;

    If you want much more than that, you're going to have to provide us with the table DDL.

    Edit: Removed database reference for my tally table.