pmadhavapeddi22 (1/21/2014)
Hi ,I have a view which contains a CTE and I need to pass the parameter inside the CTE.
our application is not accepting sps or functions. Please help me
You don't actually need to do anything special other than make sure your party_id is passed through untouched in the cte/view...
All you need to do is include party_id in the anchor and the union, you can alias it, but make sure it is present in both parts of the rCTE.
Here is your code again , with that change:
create view yourView
as
WITH PartyHierarchy( party_id,reporting_to_party_id,level,[highlight="#ffff11"]base_id[/highlight])AS
(
select party_id,reporting_to_party_id, 0[highlight="#ffff11"], party_id as base_id[/highlight]
from table_a
union all
select a.party_id,a.reporting_to_party_id,level+1[highlight="#ffff11"], b.base_id[/highlight]
from table_a a
inner join PartyHierarchy b ON b.party_id=a.reporting_to_party_id
)
select H.party_id
,H.reporting_to_party_id
,pp.last_name as 'SourceParty'
,ppb.last_name as businessEntity
,prt.code as RelationshipType
,ps.name as state
,ppt.ID as toPartyId
,ppt.last_name as destinationParty
,pstat.display_name as status
,ppr.effective_date as effectiveDate
--,level
--,party_id_from
--,party_id_to
[highlight="#ffff11"], base_id[/highlight]
from PartyHierarchy H
inner join table_a W ON H.party_id=W.party_id
INNER JOIN table_b ppr ON ppr.party_id_from=H.party_id
INNER JOIN table_c pp ON ppr.party_id_from=pp.id
INNER JOIN table_c ppb ON ppr.party_id_from=ppb.id
WHERE table_b.is_deleted=0
Now you can select from the View in your application like this:
SELECT * -- I don't want to write out the columns, but you should...
FROM yourView
WHERE base_id = @party_id
You should get the appropriate filtering on the base table now - the same as having the WHERE clause in the anchor.
edit: I had left the variable in the anchor which was very wrong and misleading!!!
MM
select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);