函数中的 SELECT 语句 - 别名、with、临时表...等

Posted

技术标签:

【中文标题】函数中的 SELECT 语句 - 别名、with、临时表...等【英文标题】:SELECT statements within a function - alias, with, temp tables...etc 【发布时间】:2013-03-22 17:38:57 【问题描述】:

我正在尝试创建一个函数,用户输入日期然后接收表格。 该函数返回显示每天(transdate)收到的邮件类型和数量的表格,然后生成特定于每种邮件类型的运行总计。它作为一个选择语句工作正常,但是当我尝试输入函数格式时,我收到错误: “函数中包含的 Select 语句不能将数据返回给客户端。”

CREATE FUNCTION DailyMailCount(@Date datetime)
RETURNS @Count TABLE 
                    (
   [ID] int identity (1,1),
   Transdate datetime, 
   Donation varchar (10), 
   DailyCount int,  
   RunningTotal int    
                     ) 

AS
BEGIN 

DECLARE @running_total TABLE (Transdate DATETIME,Donation varchar(30), countz INT,row int)
INSERT INTO @running_total

SELECT TransDate,Donation, COUNT(*)
,ROW_number() OVER (PARTITION BY Donation order by TransDate ASC) as row 
FROM RevInt.DetailDonation WHERE TransDate > '@Date' GROUP BY TransDate,Donation

SELECT 
r1.Transdate,
r1.Donation,
r1.countz,
SUM(r2.countz) as running_total
FROM @running_total r1
LEFT OUTER JOIN @running_total r2 on r2.donation = r1.Donation
                AND r2.row <= r1.row
GROUP BY 
r1.transdate,
r1.countz,
r1.donation,
r1.row
ORDER BY r1.Donation

       RETURN 
END 

【问题讨论】:

这看起来像一个存储过程,而不是一个函数。 我是函数和存储过程的新手。你能简单地向我解释一下为什么这应该是一个 SP 吗? 【参考方案1】:

正确的语法是:

CREATE FUNCTION DailyMailCount(@Date datetime)
RETURNS @Count TABLE (
   [ID] int identity (1,1),
   Transdate datetime, 
   Donation varchar (10), 
   DailyCount int,  
   RunningTotal int    
) 
as begin
    DECLARE @running_total TABLE (Transdate DATETIME,Donation varchar(30), countz INT,row int)
    INSERT INTO @running_total
        SELECT TransDate,Donation, COUNT(*),
               ROW_number() OVER (PARTITION BY Donation order by TransDate ASC) as row 
        FROM DetailDonation
        WHERE TransDate > '@Date'
        GROUP BY TransDate, Donation;

    insert into @Count
        SELECT r1.Transdate, r1.Donation, r1.countz,
               SUM(r2.countz) as running_total
        FROM @running_total r1
             LEFT OUTER JOIN @running_total r2
             on r2.donation = r1.Donation AND r2.row <= r1.row
        GROUP BY r1.transdate, r1.countz, r1.donation, r1.row
        ORDER BY r1.Donation;

    RETURN;
END;

您在返回表中缺少insert

您还可以使用单个查询创建内联函数(一条语句):

return with running_total as (
    SELECT TransDate,Donation, COUNT(*),
           ROW_number() OVER (PARTITION BY Donation order by TransDate ASC) as row 
    FROM RevInt.DetailDonation
    WHERE TransDate > '@Date'
    GROUP BY TransDate,Donation
)
SELECT r1.Transdate, r1.Donation, r1.countz, SUM(r2.countz) as running_total
FROM running_total r1 LEFT OUTER JOIN
     running_total r2
     on r2.donation = r1.Donation
                AND r2.row <= r1.row
GROUP BY r1.transdate, r1.countz, r1.donation, r1.row
ORDER BY r1.Donation

【讨论】:

谢谢,我试试看 我不确定我是否正确地关注了你。您介意复制并粘贴我发布的原始代码并告诉我您将如何更改它吗? @user2200270 。 . .我修好了它。我最初的答案只是方向错误。 完美运行!谢谢。

以上是关于函数中的 SELECT 语句 - 别名、with、临时表...等的主要内容,如果未能解决你的问题,请参考以下文章

SELECT 语句中的列别名不适用于 SQuirrel SQL + Firebird

在select 中的别名 能做 条件吗 ?

oracle--with as

mysql 递归函数with recursive的用法

根据 with 子句中的条件选择要使用的 select 语句

SQL语句中的别名为啥不能用啊