将 SQL 过程转换为函数
Convert SQL procedure to function
create function fiyathesapla(@StartDate datetime2(5),@EndDate datetime2(5),@kodu nvarchar(max))
returns table
as
return (
WITH theDates AS
(SELECT @StartDate as theDate
UNION ALL
SELECT DATEADD(day, 1, theDate)
FROM theDates
WHERE DATEADD(day, 1, theDate) <= @EndDate
)
SELECT SUM( v_period.fiyat-((v_period.fiyat/100)*v_period.indirim)) FROM theDates,v_period WHERE tarih1<=theDates.theDate and tarih2>=theDates.theDate and vkodu=@kodu
)
go
---错误代码---
Msg 4514, Level 16, State 1, Procedure fiyathesapla, Line 6 [Batch Start Line 0]
CREATE FUNCTION failed because a column name is not specified for column 1.
我在尝试创建函数时遇到此错误。
它有助于通过按天计算输入的两个日期来计算价格。
但是当我创建存储过程时它起作用了。
我想创建一个函数,我该如何编辑它。
---正常运行的过程代码---
USE [villapaket1]
GO
/****** Object: StoredProcedure [dbo].[FN_fiyat] Script Date: 30.09.2020 16:18:46 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[FN_fiyat] @StartDate datetime2(5),@EndDate datetime2(5),@kodu nvarchar(max)
AS
WITH theDates AS
(SELECT @StartDate as theDate
UNION ALL
SELECT DATEADD(day, 1, theDate)
FROM theDates
WHERE DATEADD(day, 1, theDate) <= @EndDate
)
SELECT SUM( v_period.fiyat-((v_period.fiyat/100)*v_period.indirim)) FROM theDates,v_period WHERE tarih1<=theDates.theDate and tarih2>=theDates.theDate and vkodu=@kodu
看起来数据库引擎告诉您 SUM( v_period.fiyat-((v_period.fiyat/100)*v_period.indirim))
(= 最后 select 的第一列)需要一个别名。这不是在函数外部显示结果所必需的,但是 return 具有有效列名的table 所必需的。
下面和 this fiddle.
中的小数据集和简单(但相似)函数的问题再现
示例数据
create table MyTable
(
id int
);
insert into MyTable (id) values
(100), (101), (102);
选择
with cte as
(
select id-100 as NewId from MyTable
)
select sum(cte.NewId) --> no column alias, works fine (result table has unnamed column)
from cte;
函数
create function MyFunction()
returns table
as return
(
with cte as
(
select id-100 as NewId from MyTable
)
select sum(cte.NewId) --> no column alias, does NOT work
from cte
);
CREATE FUNCTION failed because a column name is not specified for column 1.
已更新为工作函数:
create function MyFunction()
returns table
as return
(
with cte as
(
select id-100 as NewId from MyTable
)
select sum(cte.NewId) as IdSum --> with column alias
from cte
);
create function fiyathesapla(@StartDate datetime2(5),@EndDate datetime2(5),@kodu nvarchar(max))
returns table
as
return (
WITH theDates AS
(SELECT @StartDate as theDate
UNION ALL
SELECT DATEADD(day, 1, theDate)
FROM theDates
WHERE DATEADD(day, 1, theDate) <= @EndDate
)
SELECT SUM( v_period.fiyat-((v_period.fiyat/100)*v_period.indirim)) FROM theDates,v_period WHERE tarih1<=theDates.theDate and tarih2>=theDates.theDate and vkodu=@kodu
)
go
---错误代码---
Msg 4514, Level 16, State 1, Procedure fiyathesapla, Line 6 [Batch Start Line 0] CREATE FUNCTION failed because a column name is not specified for column 1.
我在尝试创建函数时遇到此错误。 它有助于通过按天计算输入的两个日期来计算价格。 但是当我创建存储过程时它起作用了。 我想创建一个函数,我该如何编辑它。
---正常运行的过程代码---
USE [villapaket1]
GO
/****** Object: StoredProcedure [dbo].[FN_fiyat] Script Date: 30.09.2020 16:18:46 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[FN_fiyat] @StartDate datetime2(5),@EndDate datetime2(5),@kodu nvarchar(max)
AS
WITH theDates AS
(SELECT @StartDate as theDate
UNION ALL
SELECT DATEADD(day, 1, theDate)
FROM theDates
WHERE DATEADD(day, 1, theDate) <= @EndDate
)
SELECT SUM( v_period.fiyat-((v_period.fiyat/100)*v_period.indirim)) FROM theDates,v_period WHERE tarih1<=theDates.theDate and tarih2>=theDates.theDate and vkodu=@kodu
看起来数据库引擎告诉您 SUM( v_period.fiyat-((v_period.fiyat/100)*v_period.indirim))
(= 最后 select 的第一列)需要一个别名。这不是在函数外部显示结果所必需的,但是 return 具有有效列名的table 所必需的。
下面和 this fiddle.
中的小数据集和简单(但相似)函数的问题再现示例数据
create table MyTable
(
id int
);
insert into MyTable (id) values
(100), (101), (102);
选择
with cte as
(
select id-100 as NewId from MyTable
)
select sum(cte.NewId) --> no column alias, works fine (result table has unnamed column)
from cte;
函数
create function MyFunction()
returns table
as return
(
with cte as
(
select id-100 as NewId from MyTable
)
select sum(cte.NewId) --> no column alias, does NOT work
from cte
);
CREATE FUNCTION failed because a column name is not specified for column 1.
已更新为工作函数:
create function MyFunction()
returns table
as return
(
with cte as
(
select id-100 as NewId from MyTable
)
select sum(cte.NewId) as IdSum --> with column alias
from cte
);