只是发布这个供其他人使用。下面的 T-sql 代码用于从 ofpr 表在 SAP B1 中创建新表以扩展日历使用。在我报告的情况下,因此名称 ReportCalendar 使其独一无二。此代码用于三个 SAP B1 生产系统。随时发布设计改进和想法。注意我这里使用 sqlcmd 进行 sql 交互。
/* :r ReportCalendar.table.sql
Build ReportCalendar year month table. ofpr table is for finanical year only.
Confirmation
:out ReportCalendar.table.txt
select * from ReportCalendar;
go
:out stdout
*/
if exists (select name from dbo.sysobjects
where id = object_id(N'[dbo].[ReportCalendar]') and
objectproperty(id, N'IsUserTable')=1)
drop table [dbo].[ReportCalendar];
go
create table [dbo].[ReportCalendar]
(Year int not null,
Month int not null,
MonthName varchar(9) not null,
MonthShortName varchar(3) not null,
MonthFrom datetime not null,
MonthTo datetime not null,
Quarter int not null,
HalfYear int not null,
FinancialYear varchar(9) not null,
FinancialYearShort varchar(5) not null,
FinancialYearQuarter int not null,
FinancialHalfYear int not null
constraint pk_year_month primary key clustered (Year, Month));
go
-- Build yearly ReportCalendar from Posting Period table.
insert into [dbo].[ReportCalendar]
select
datename(Year, f_refdate) as [Year],
datepart(Month, f_refdate) as [Month],
datename(Month, f_refdate) as [MonthName],
substring(datename(Month, f_refdate), 1, 3) as [MonthShortName],
f_refdate as [MonthFrom],
t_refdate as [MonthTo],
datename(Quarter, f_refdate) as [Quarter],
case when datename(Quarter, f_refdate) > 2 then 2 else 1 end as [HalfYear],
case when datepart(Month, f_refdate) > 6
then (datename(Year, f_refdate) + '-' + datename(Year, dateadd(Year, 1, f_refdate)))
else (datename(Year, dateadd(Year, -1, f_refdate)) + '-' + datename(Year, f_refdate))
end as [FinancialYear], -- month > 6 year+(year+1) else (year-1)+year
case when datepart(Month, f_refdate) > 6
then (substring(datename(Year, f_refdate), 3, 2) + '-' + substring(datename(Year, dateadd(Year, 1, f_refdate)), 3, 2))
else (substring(datename(Year, dateadd(Year, -1, f_refdate)), 3, 2) + '-' + substring(datename(Year, f_refdate), 3, 2))
end as [FinancialYear],
case when datename(Quarter, f_refdate) > 2 then (datename(Quarter, f_refdate) + 2) - 4
else datename(Quarter, f_refdate) + 2
end as [Quarter], -- add 2 to Quarter
case when datename(Quarter, f_refdate) > 2 then 1 else 2 end as [FinancialHalfYear] -- opposite HalfYear
from [dbo].[ofpr] -- Posting Period
order by f_refdate;
go