简体   繁体   English

如何在查询中正确调用函数

[英]How to call a function properly as part of a query

I'm new to the calling stored functions and stored procedures in queries. 我是查询中调用存储函数和存储过程的新手。 Can someone help me understand how to better call a stored function than I do below. 有人可以帮我理解如何比我在下面更好地调用存储函数。 It seems like there must be a more realistic, logical way so the code flows. 似乎必须有一种更现实,更合乎逻辑的方法,这样代码才能流动。 This seems a bit crazy. 这似乎有点疯狂。

SELECT        SUM(TotalAmt) AS [Total Amount]
FROM            dbo.fDataamount('1', @startdate, @enddate, '1', '1', 'Total', 'Total', 'Total', 'Total', '1', '1', '1', '1', '1') AS fDataamount_1
WHERE        (DataID = 55)

Is your query not working? 您的查询不起作用吗? Or are you looking for a better way of writing it. 还是您正在寻找一种更好的编写方式。

If it's not working, check to make sure it's a table-valued function. 如果它不起作用,请检查以确保它是一个表值函数。

I would also look at how often that function is being used. 我还将查看该功能的使用频率。 I prefer to use functions only if I can consolidate code that's being used across multiple stored procedures for easier maintaining. 仅当我可以合并跨多个存储过程使用的代码以便于维护时,我才更喜欢使用函数。

If the function is only used in this one stored procedure consider creating the data set in a temp table or CTE and reference that instead. 如果仅在此一个存储过程中使用该函数,请考虑在临时表或CTE中创建数据集,然后引用该数据集。

What I can gleam from your query, you're essentially taking the sum of a column out of a table that is returned from the function. 我可以从查询中看到的内容是,您实际上是从函数返回的表中取出列的总和。 Why bother using a function at all? 为什么要麻烦使用一个功能呢? You can take that logic out of the function and put it directly in the query. 您可以从函数中删除该逻辑,然后将其直接放入查询中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM