I think it can be done in one select.
select s.sortcode,
s.descrp, s.siteno,
sf.freqcode, sf.frequency, sf.firstfreq,
sf.numfirstloads, sf.checkscale
from samplefreqs sf join sortcodes s
on sf.sortcode = s.sortcode and sf.rcode = s.rcode
where sf.brandid = @brandid and sf.rcode = @rcode
union
select s.sortcode,
s.descrp, s.siteno,
sf.freqcode, sf.frequency, sf.firstfreq,
sf.numfirstloads, sf.checkscale
from samplefreqs sf join sortcodes s on sf.rcode = s.rcode
where sf.brandid = @brandid and sf.rcode = @rcode and sf.sortcode = '*'
and not exists (
select * from samplefreqs sf1
where sf1.sortcode = s.sortcode
and sf1.rcode = @rcode and sf.brandid = @brandid )
>Sergey
>
>I put some comments in the code.
>
>sampefreqs table - a table that says how often to pick a sample of a load based on the type of product (sortcode) that is being delived
>
>sortcodes table - a table of all the possible sortcodes available.
>
>/*
>This procedure retrieves all the possible sortcode combinations based on
>how the user has input the records in the samplefreqs table. If there is
>A "*" in a row in the sortcode column then all sortcodes in
>the sortcodes table are valid for that brand. Otherwise, only the
>sortcodes in the samplefreqs table are valid. The values declared
>below are used as the default value for all the rest of the sortcode
>records that are not in the samplefreqs table.
>
>*/
>
< snip >
--sb--