Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Pass param to an inline-Table function n a funct.

Author  Topic 

MagicCity77
Starting Member

19 Posts

Posted - 2009-11-24 : 12:49:51
Hello everyone I have a quick question

This is my first project where I had the need to write functions. My reason for doing so was to minimize the number of times the same lines of code will be executed as they will be used in other storedprocedures.

Here is my Code:

fx_MaterialBuildQuantity
	-- Add the SELECT statement with parameter references here
SELECT dbo.fx_Material.EDCID
, dbo.fx_Material.MatNumber
, Sum(dbo.fx_Material.MaterialCost) AS Budget
FROM dbo.fx_Material

WHERE MaterialData.InitialDate BETWEEN '05/01/2005' and '05/31/2005' AND--@StartDate and @EndDate AND
MaterialDetailsXref.MatNumber = MaterialData.MatNumber AND
MaterialDetails.EDCID = MaterialDetailsXref.EDCID

GROUP BY MaterialDetails.MatNumber
, MaterialDetails.EDCID


Second Function fx_Material

    SELECT 
CAST(ROUND((SUM(dbo.DoorParts.Percentage * dbo.ScrewUsage.WeightID)
* (dbo.MatDetails.MatNumber)),2) AS decimal(15,2)) AS Cost

FROM Material INNER JOIN
DoorINNER JOIN
DoorParts ON dbo.Door.DoorID = dbo.DoorParts.DoorID ON
Material.MaterialID = dbo.DoorParts.MaterialID INNER JOIN
Weight ON dbo.Door.DoorID = dbo.Weight.DoorID INNER JOIN
DoorUsage INNER JOIN
MatDetails ON dbo.DoorUsage.EDCID = dbo.MatDetails.EDCID AND
MatUsage.MatNumber = dbo.MatDetails.MatNumber ON dbo.Weight.WeightID = dbo.DoorUsage.WeightID

WHERE dbo.MatDetails.MatNumber = @MatNumber

GROUP By MatDetails.MatNumber


My problem is I need to pass in a value to a function inside of a function so that the main function can build its table? Many of these functions will be used in formulas so I know that I will run into this again, but at least I will have a better understanding of how to resolve it.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-11-24 : 13:10:09
Is performance at all a concern here? Functions like these are notorious for being slow.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

MagicCity77
Starting Member

19 Posts

Posted - 2009-11-24 : 13:29:55
Yes performance is definitely an issue what would you recommend.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-11-24 : 14:15:54
Against my recommendation, we have a system that heavily uses functions like you've shown. That system's performance is so bad due to the functions that it's impossible to make any improvements. I have confirmed with Microsoft that their issue is due to the functions since the query optimizer is not able to pick a good exeuction plan. We see scans instead of seeks on our functions.

I don't really have a good solution for you except to avoid functions like this. I suppose if I had to come up with a solution it would involve putting the code into stored procedures, executing the code into a temp table, and then joining that temp table to whatever query would have needed the function. There could be a better way though, but that's just off the top of my head.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

MagicCity77
Starting Member

19 Posts

Posted - 2009-11-24 : 14:23:11
Ok thanks this will help me a great deal as this application will be hiting our servers here in the states but the users are in South America.

I'm going to see if I can come up with a few examples like you mentioned as this will be my first time trying something like this.

Thanks again!!!!
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-11-24 : 16:02:04
You're welcome.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page
   

- Advertisement -