Просто опубликовать это для использования другими.T-sql код ниже для создания новой таблицы в SAP B1 из таблицы ofpr для расширенного использования календаря.В моем случае для отчетности отсюда и название 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