用向导生成的原始报表只能包含两张关联的table,如果要显示多张关联的表的内容(三张以上),就得修改报表的DSMain数据源:
sample code
Code
DECLARE @SQL nvarchar(max)
SET @SQL = 'select account0.name as ''name'', account0.accountid as ''accountid'', opportunity1.name as ''opportunity1_name'', opportunity1.opportunityid as ''opportunity1_opportunityid'',Jade_purchasetable.Jade_purchaseordernum as ''test'',Jade_purchasetable.Jade_suppliernameName as ''supplier'',Jade_purchaseline.Jade_partsequencenumName as ''partnum''
from ('+ @CRM_FilteredAccount + ') as account0 left outer join (' + @CRM_FilteredOpportunity + ') as opportunity1 on (account0.accountid = opportunity1.customerid)
join Jade_purchasetable on (Jade_purchasetable.Jade_projectid = opportunity1.opportunityid)
join Jade_purchaseline on (Jade_purchaseline.Jade_purchaseorderid = Jade_purchasetable.Jade_purchasetableId)'
EXEC (@SQL)
说明:比如A,B,C,D四张表有关联,A.a1 = B.b1,B.b2 = C.c2,C.c3 = D.d3,则可以
select A.field1,B.field1,C.field1,D.field1 from A
[inner/left outer]join B
on A.a1 = B.b1
where A.field = "***" and B.field = "***"
[inner/left outer]join C
on B.b2 = C.c2
where ...
其中,第二个join是B和C的join而不是A和C的join(根据on B.b2 = C.c2),用join不用子查询的原因是子查询的效率比较低。
然后在数据源中就可以直接引用ABCD的字段,然后放到报表中去了。
如下代码实现将D、E中的数据放在一起,然后在body中显示(其中A->B->C->D,A->B->C->E):
Code
1DECLARE @SQL nvarchar(max)
2SET @SQL = 'select Table_1.*,Table_2.*,Table_3.*,Table_4.* from Table_1
3 join Table_2 on Table_1.id=Table_2.id
4 join Table_3 on Table_3.item = Table_2.item
5 left outer join Table_4 on Table_4.project = Table_2.project'
6EXEC(@SQL)