gpt4 book ai didi

在表值函数中使用#table/Temp 表的 SQL

转载 作者:行者123 更新时间:2023-12-05 04:09:39 25 4
gpt4 key购买 nike

我需要一个函数来获取给定日期范围内的所有周数。我必须将它与我的存储过程分开,因为它有一个漫长的过程。

但是当我在函数中使用#tables 时,它给出了以下错误

Msg 2772, Level 16, State 1, Procedure WeekNumbersWithinRange, Line 19
Cannot access temporary tables from within a function.

这是我的功能。

CREATE FUNCTION WeekNumbersWithinRange
(
@FromDate DATETIME,
@ToDate DATETIME
)
RETURNS TABLE
AS
BEGIN
IF OBJECT_ID (N'tempdb..#WeeksofRange', N'U') IS NOT NULL
BEGIN
DROP TABLE #WeeksofRange
END

CREATE TABLE #WeeksofRange (WeekNo INT);

-- Get all week numbers for the given delivery date range
IF OBJECT_ID (N'tempdb..#Calendar', N'U') IS NOT NULL
BEGIN
DROP TABLE #Calendar
END

CREATE TABLE #Calendar
(
CalendarDate DATE PRIMARY KEY,
IsWeekend BIT,
YearNo SMALLINT,
QuarterNo TINYINT,
MonthNo TINYINT,
DayOfYearNo SMALLINT,
DayNo TINYINT,
WeekNo TINYINT,
WeekDayNo TINYINT
)

DECLARE @beginDate DATE, @endDate DATE

SELECT
@beginDate = @FromDate, @endDate = @ToDate

WHILE @beginDate <= @endDate
BEGIN
INSERT INTO #Calendar (CalendarDate, IsWeekend, YearNo, QuarterNo, MonthNo, DayOfYearNo, DayNo, WeekNo, WeekDayNo)
SELECT
@beginDate As CalendarDate,
(CASE WHEN DATEPART(Weekday, @beginDate) IN (7, 1)
THEN 1 ELSE 0
END) AS IsWeekend,
DATEPART(Year, @beginDate) AS YearNo,
DATEPART(QUARTER, @beginDate) AS QuarterNo,
DATEPART(MONTH, @beginDate) AS MonthNo,
DATEPART(DayOfYear, @beginDate) AS DayOfYearNo,
DATEPART(Day, @beginDate) AS DayNo,
DATEPART(Week, @beginDate) AS WeekNo,
DATEPART(WEEKDAY, @beginDate) AS WeekDayNo
--,(Case When @beginDate < '02/03/2011' Then 0 Else DATEPART(Week, @beginDate) - 5 End) As mySpecificWeekNo

SET @beginDate = DATEADD(Day, 1, @beginDate)
END

INSERT INTO #WeeksofRange
SELECT DISTINCT WeekNo
FROM #Calendar;

-- End of Select all week numbers in the range

SELECT * FROM #WeeksofRange
GO

还有其他方法吗?或者我是否需要在我的存储过程中编写此逻辑?

最佳答案

您不能在 UDF 中使用临时表,因为

the object can't be created inside UDF.

不要使用临时表,而是使用表变量,这应该可以解决您的问题

CREATE FUNCTION WeekNumbersWithinRange
(
@FromDate DATETIME,
@ToDate DATETIME
)
RETURNS TABLE
AS
BEGIN

DECLARE @WeeksofRange TABLE
(
WeekNo INT
)

-- Get all week numbers for the given delivery date range

declare @Calendar table
(CalendarDate Date Primary key, IsWeekend Bit, YearNo SmallInt, QuarterNo TinyInt, MonthNo TinyInt, DayOfYearNo SmallInt, DayNo TinyInt, WeekNo TinyInt, WeekDayNo TinyInt )

Declare @beginDate Date, @endDate Date


Select @beginDate = @FromDate , @endDate = @ToDate


While @beginDate <= @endDate
Begin
Insert Into @Calendar (CalendarDate, IsWeekend, YearNo, QuarterNo, MonthNo, DayOfYearNo, DayNo, WeekNo, WeekDayNo)

Select
@beginDate As CalendarDate
,(Case When DATEPART(Weekday, @beginDate) In (7, 1) Then 1 Else 0 End) As IsWeekend
,DATEPART(Year, @beginDate) As YearNo
,DATEPART(QUARTER, @beginDate) As QuarterNo
,DATEPART(MONTH, @beginDate) As MonthNo
,DATEPART(DayOfYear, @beginDate) As DayOfYearNo
,DATEPART(Day, @beginDate) As DayNo
,DATEPART(Week, @beginDate) As WeekNo
,DATEPART(WEEKDAY, @beginDate) As WeekDayNo
--,(Case When @beginDate < '02/03/2011' Then 0 Else DATEPART(Week, @beginDate) - 5 End) As mySpecificWeekNo
Set @beginDate = DateAdd(Day, 1, @beginDate)
End

INSERT INTO @calendar
SELECT DISTINCT WeekNo FROM @Calendar;

-- End of Select all week numbers in the range


SELECT * FROM @WeeksofRange

GO

关于在表值函数中使用#table/Temp 表的 SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45605042/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com