pooja.sharma 54426 (1/15/2014)
Hi,Actually my function is returning a dynamic query .
Step 1 : Create a function returning property names in CSV format
Create FUNCTION dbo.fxnGetPropertyColsQuery
(
)
RETURNS NVARCHAR(MAX)
AS
BEGIN
declare @Return NVARCHAR(MAX)
select @Return = STUFF((SELECT distinct ',' + QUOTENAME(c.propName)
FROM PropertyDef c where objecttypeid = 3
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
RETURN @return
End
Step 2: Create a function accepting return value from dbo.fxnGetPropertyColsQuery as a parameter
create FUNCTION dbo.fxnGetPropertiesQuery
(
@cols NVARCHAR(MAX)
)
RETURNS NVARCHAR(MAX)
AS
BEGIN
declare @Return NVARCHAR(MAX)
select @Return =
'Select EquipmentNo,' + @cols + '
From
(
Select EquipmentNo,PropValue,PropName,PropertyDef.PropertyDefID
From PropertyDef Inner Join EPropertyData
ON PropertyDef.PropertyDefID=EPropertyData.PropertyDefID
INNER JOIN Equipment ON Equipment.EquipID=EPropertyData.ObjectID
WHERE ObjectTypeID In(3,4)
)AS SourceTable
PIVOT
(
max(PropValue)
For
PropName In (' + @cols + ')
)AS PivotTable;'
RETURN @return
End
I want to create a view executing function fxnGetPropertiesQuery returning table.
I would say that using either of those functions in a view is going to be rather slow performing. It appears to me that both could be converted into iTVFs along the pattern pietlinden provided. You would need to call them differently (e.g., with a CROSS APPLY).
Edit: Put the SQL within sql code tags.
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St