TysonLPrice
Programmer
I have a stored procedure that currently brings back one row per policy number. A new table has been introduced that needs to be joined to that stored procedure. That table currently has six entries per policy but that can increase or decrease. This is sort of what I am looking at.
Although it is much more complex I think this will illustrate the issue:
Lets say this is the current stored procedure:
select * from #Policies p where policyno = 1
I need to join #NewTable to it:
select p.policyno,t.Newdata,t.akey from #Policies p
join #NewTable t on t.policyno = p.policyno
where p.policyno = 1
But I need the output to look like this:
PolicyNo NewData akey NewData akey NewData akey NewData akey
1 aaaa 1 bbbb 2 cccc 3 dddd 4
Any ideas assuming at this point I can't change the table structure. If the stored procdure always brought back one row I could flatten #NewTable first and join to that. The real issue is the stored procedure could bring back many policies and each one needs to be one row per policy with the #NewTable data flattened out per policy row.
Although it is much more complex I think this will illustrate the issue:
Code:
create table #Policies(PolicyNo int, SomeData Char(5))
insert into #Policies(policyno,somedata) values (1,'abcd')
create table #NewTable(PolicyNo int,NewData char(5),akey int)
insert into #NewTable(PolicyNo,NewData,akey) values(1, 'aaaa',1)
insert into #NewTable(PolicyNo,NewData,akey) values(1, 'bbbb',2)
insert into #NewTable(PolicyNo,NewData,akey) values(1, 'cccc',3)
insert into #NewTable(PolicyNo,NewData,akey) values(1, 'dddd',4)
Lets say this is the current stored procedure:
select * from #Policies p where policyno = 1
I need to join #NewTable to it:
select p.policyno,t.Newdata,t.akey from #Policies p
join #NewTable t on t.policyno = p.policyno
where p.policyno = 1
But I need the output to look like this:
PolicyNo NewData akey NewData akey NewData akey NewData akey
1 aaaa 1 bbbb 2 cccc 3 dddd 4
Any ideas assuming at this point I can't change the table structure. If the stored procdure always brought back one row I could flatten #NewTable first and join to that. The real issue is the stored procedure could bring back many policies and each one needs to be one row per policy with the #NewTable data flattened out per policy row.