# Including User Written Functions in Query Model

**URL:** <https://forums.toadworld.com/t/including-user-written-functions-in-query-model/58933>\
**Category:** Toad Data Point & Toad Intelligence Central\
**Created:** [February 9, 2024, 1:34pm UTC](https://forums.toadworld.com/t/including-user-written-functions-in-query-model/58933 "2024-02-09T13:34:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![brinehart](https://sea1.discourse-cdn.com/quest/user_avatar/forums.toadworld.com/brinehart/32/16398_2.png) [@brinehart](https://forums.toadworld.com/u/brinehart)\
**Post date:** [February 9, 2024, 1:34pm UTC](https://forums.toadworld.com/t/including-user-written-functions-in-query-model/58933/1 "2024-02-09T13:34:01Z")

</div>

I've written several SQL Server user functions that simplify retrieval of data in complex queries/views. SQL Server allows functions to be referenced in a query. However, I can't drag them into the query diagram. I found an old knowledgebase article that describes how someone was doing that. I'm wondering if this feature was removed, broken or I just don't know how to do it?

---

<div class="post-metadata">

**Author:** ![Gary.Jerep](https://sea1.discourse-cdn.com/quest/user_avatar/forums.toadworld.com/gary.jerep/32/205_2.png) [@Gary.Jerep](https://forums.toadworld.com/u/Gary.Jerep)\
**Post date:** [February 9, 2024, 7:04pm UTC](https://forums.toadworld.com/t/including-user-written-functions-in-query-model/58933/2 "2024-02-09T19:04:13Z")

</div>

I would be very interested in seeing the KB article, if you have the link. Can you please share with the rest of the forum readers?

To my knowledge, TDP has never had the ability to drag a function (e.g. from the Code Snippets View panel) to any part of a query diagram in the Visual Query Builder.

However, you _can_ drag a code snippet/s into the SQL code that the Query Builder generates (via its Query tab), if that helps.

---

<div class="post-metadata">

**Author:** ![brinehart](https://sea1.discourse-cdn.com/quest/user_avatar/forums.toadworld.com/brinehart/32/16398_2.png) [@brinehart](https://forums.toadworld.com/u/brinehart)\
**Post date:** [February 9, 2024, 10:09pm UTC](https://forums.toadworld.com/t/including-user-written-functions-in-query-model/58933/3 "2024-02-09T22:09:11Z")

</div>

@Gary.Jerep it appears that you are correct. I thought that I reviewed a KB article that alluded to using a function in a query diagram. But now I cannot find it. So, I'll go with your answer, it's not a feature.

---

<div class="post-metadata">

**Author:** ![brinehart](https://sea1.discourse-cdn.com/quest/user_avatar/forums.toadworld.com/brinehart/32/16398_2.png) [@brinehart](https://forums.toadworld.com/u/brinehart)\
**Post date:** [February 9, 2024, 10:22pm UTC](https://forums.toadworld.com/t/including-user-written-functions-in-query-model/58933/4 "2024-02-09T22:22:28Z")

</div>

I see that I am able to drag the function to the Query tab. However, I'm not sure how to determine where to include it in the query. The function accepts an input parameter that is a unique identifier. The function performs some calculations and returns a one row table that contains four columns. One of those columns is named PRI which is the column that I would like to use in the query.

Currently the query performs the same calculation as the function and returns PRI as one of the columns in the SELECT ... PRI .... FROM..... Can you help me determine where and how to include the function?

SELECT MS.StandID,  
MS.StandPosition AS POS,  
MS.WtWarning AS Warning,  
RW\_UOM.UOMName AS UOM,  
RS.RollSet,  
RS.Section,  
RS.SetNum,  
RSS.StatusName AS Status,  
RSS.AutoSelect AS [Auto],  
RSS.InstallRollSet AS Inst,  
RS.InService,  
RS.OutService,  
SEL\_PRI.Effective,  
RSSPriority.Priority,  
RS.RS\_UID,  
MSR.MSRollSet\_UID  
FROM GSL\_PI\_DEV.PISystem.vRollSetsAll vRollSetsAll  
CROSS JOIN  
(  
(  
(  
(  
(  
GSL\_PI\_DEV.PISystem.MillStandsRollsets MSR  
INNER JOIN GSL\_PI\_DEV.PISystem.RollSets RS  
ON (MSR.RS\_UID = RS.RS\_UID))  
INNER JOIN  
GSL\_PI\_DEV.PISystem.RollSetSelPriority RSSPriority \<\<=== this is part of the logic that is similar to the function  
ON (RSSPriority.MSRollSet\_UID = MSR.MSRollSet\_UID))  
RIGHT OUTER JOIN GSL\_PI\_DEV.PISystem.MillStands MS  
ON (MSR.MS\_UID = MS.MS\_UID))  
INNER JOIN GSL\_PI\_DEV.PISystem.RolledWtUOM RW\_UOM  
ON (MS.UOM\_UID = RW\_UOM.UOM\_UID))  
INNER JOIN  
(SELECT RSSP.MSRollSet\_UID, MAX (RSSP\_AVAIL.EffDate) AS Effective  
FROM GSL\_PI\_DEV.PISystem.RollSetSelPriority RSSP  
INNER JOIN  
(SELECT RSSP\_CUR.RSSPRI\_UID, \<\<=== this is also some of the logic  
RSSP\_CUR.MSRollSet\_UID,  
RSSP\_CUR.EffDate,  
RSSP\_CUR.Priority  
FROM GSL\_PI\_DEV.PISystem.RollSetSelPriority RSSP\_CUR  
WHERE (RSSP\_CUR.EffDate \<= SYSDATETIME ())) RSSP\_AVAIL  
ON (RSSP.MSRollSet\_UID = RSSP\_AVAIL.MSRollSet\_UID)  
GROUP BY RSSP.MSRollSet\_UID) SEL\_PRI  
ON (MSR.MSRollSet\_UID = SEL\_PRI.MSRollSet\_UID)  
AND (SEL\_PRI.Effective = RSSPriority.EffDate))  
INNER JOIN GSL\_PI\_DEV.PISystem.RollSetStatus RSS  
ON (RS.RSStatus\_UID = RSS.RSStatus\_UID)

---

<div class="post-metadata">

**Author:** ![Gary.Jerep](https://sea1.discourse-cdn.com/quest/user_avatar/forums.toadworld.com/gary.jerep/32/205_2.png) [@Gary.Jerep](https://forums.toadworld.com/u/Gary.Jerep)\
**Post date:** [February 9, 2024, 11:34pm UTC](https://forums.toadworld.com/t/including-user-written-functions-in-query-model/58933/5 "2024-02-09T23:34:14Z")

</div>

Not sure if I understand what you're trying to do completely, and I'm personally not a SQL Server power expert, but if your table-valued function (sounds like) takes a parameter, then you'll need to either introduce a bind variable in the script executing your parent query, or embed the call/s in a procedure.

Might want to post your question on a forum like

> **[SQLServerCentral – The #1 SQL Server community](https://www.sqlservercentral.com/)**
>
> The #1 SQL Server community and education site, with articles, news, forums, scripts and FAQs.

or the like.

The other option possibly is to use the Visual Query Builder in TDP to join the underlying tables together, and then save the Query Builder file once the query logic tests ok so you don't have to keep building the query over and over.

---

<div class="post-metadata">

**Author:** ![brinehart](https://sea1.discourse-cdn.com/quest/user_avatar/forums.toadworld.com/brinehart/32/16398_2.png) [@brinehart](https://forums.toadworld.com/u/brinehart)\
**Post date:** [February 10, 2024, 10:45pm UTC](https://forums.toadworld.com/t/including-user-written-functions-in-query-model/58933/6 "2024-02-10T22:45:47Z")

</div>

Gary,

Thank you for the tips!
