【问题标题】:SQL Table Valued Function is not workingSQL 表值函数不起作用
【发布时间】:2014-05-02 15:43:42
【问题描述】:

我有一个 sql 查询可以在 1 秒内检索大约 2500 行。当我把这个查询放在一个表值函数中时,它花了 20 分钟,只检索到 450-500 行,它仍在运行。

select g.AccountNumber,g.AccountName,g.ProductNumber,g.ProductName,g.SlsNumber,g.SlsName,g.GoalAmount,s.SalesAmount
from
    (
        select g.AccountNumber,g.AccountName,g.ProductNumber,g.ProductName,g.SlsNumber,g.SlsName,sum(g.GoalAmount) GoalAmount
        from RucoNetApiBase.dbo.Goals g with(nolock)
        where ((@month <> 0 and g.Month_=@month) or (@month=0 and 1=1)) and (g.Year_=year(getdate()))
        group by g.AccountNumber,g.AccountName,g.ProductNumber,g.ProductName,g.SlsNumber,g.SlsName
    ) g left outer join
    (
        select s.ClientCode,left(s.ItemCode,5) ItemCode,s.SalesmanCode,sum(s.Amount) SalesAmount
        from dbo.V_NetSalesBase s
        where ((@month <> 0 and s.Month_=@month) or (@month=0 and 1=1)) and (s.Year_=year(getdate())) and (s.FicheSpeCode='')
        group by s.ClientCode,left(s.ItemCode,5),s.SalesmanCode
    ) s on (g.AccountNumber=s.ClientCode) and (g.ProductNumber=s.ItemCode) and (g.SlsNumber=s.SalesmanCode)

以及功能;

ALTER FUNCTION [dbo].[F_GoalSummary]
(
   @month int
)
RETURNS TABLE 
AS
RETURN
(
select g.AccountNumber,g.AccountName,g.ProductNumber,g.ProductName,g.SlsNumber,g.SlsName,g.GoalAmount,s.SalesAmount
from
    (
        select g.AccountNumber,g.AccountName,g.ProductNumber,g.ProductName,g.SlsNumber,g.SlsName,sum(g.GoalAmount) GoalAmount
        from RucoNetApiBase.dbo.Goals g with(nolock)
        where ((@month <> 0 and g.Month_=@month) or (@month=0 and 1=1)) and (g.Year_=year(getdate()))
        group by g.AccountNumber,g.AccountName,g.ProductNumber,g.ProductName,g.SlsNumber,g.SlsName
    ) g left outer join
    (
        select s.ClientCode,left(s.ItemCode,5) ItemCode,s.SalesmanCode,sum(s.Amount) SalesAmount
        from dbo.V_NetSalesBase s
        where ((@month <> 0 and s.Month_=@month) or (@month=0 and 1=1)) and (s.Year_=year(getdate())) and (s.FicheSpeCode='')
        group by s.ClientCode,left(s.ItemCode,5),s.SalesmanCode
    ) s on (g.AccountNumber=s.ClientCode) and (g.ProductNumber=s.ItemCode) and (g.SlsNumber=s.SalesmanCode)
)

以及功能的使用;

SELECT [t0].[AccountNumber], [t0].[AccountName], [t0].[ProductNumber], [t0].[ProductName], [t0].[SlsNumber], [t0].[SlsName], [t0].[GoalAmount], [t0].[SalesAmount]
FROM [dbo].[F_GoalSummary](5) AS [t0]
ORDER BY [t0].[ProductName]

如果在管理工作室中执行查询本身,一切都很好(1 秒内 2500 行),但是当我将查询文本放入表值函数并在查询中使用它时,我在 20 分钟内只得到 450 行。它还在运行。该功能昨天运行良好。

我正在使用 sql server 2008 r2 Microsoft SQL Server 管理工作室 10.50.1600.1 Microsoft 分析服务客户端工具 10.50.1600.1 Microsoft 数据访问组件 (MDAC) 6.1.7600.16385 微软 MSXML 3.0 6.0 微软 Internet Explorer 8.0.7600.16385 微软 .NET 框架 2.0.50727.4963 操作系统 6.1.7600

一个简单的解决方法可以是,而不是使用函数。但是一个有更复杂的功能,我在其中使用了 clr 存储过程并且无法轻松复制粘贴。

我不知道这两者之间有什么区别。

请帮帮我!!

【问题讨论】:

    标签: sql-function


    【解决方案1】:

    您是否尝试将您尝试连接的两个查询的结果放入一个表变量中并连接这两个变量?

    如果您不订购结果会怎样?

    【讨论】:

      猜你喜欢
      • 2014-06-17
      • 1970-01-01
      • 2016-04-14
      • 2016-05-20
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 2012-04-15
      相关资源
      最近更新 更多