我正在为新年创建一些报告。我解除了此代码并尝试使其适用于临时表,因为我没有在我的数据库上创建表的权限。我注释掉了 PKey 部分,因为我认为我在临时表中不需要它。
我可以在 2099 年之前插入日期,在 BankHoliday 中插入“N”,HolidayName
在临时表的列中插入“Null”。但是,当我单独或作为一个长查询运行“元旦”假期的更新部分时,我得到了相同的语法错误。“''附近的语法不正确。” 就在我觉得我也知道自己在做什么的时候...
Declare @FirstDate as Date
Declare @LastDate as Date
Declare @WorkingDate as Date
set @FirstDate = '01-01-2010'
SET @LastDate = '12-31-2099'
-- create holiday table replace # with dbo for permanent table
begin
create table #CACFederalReserverHolidays
(
[Date] Date Not Null,
BankHoliday nvarchar(1) Null,
HolidayName nvarchar(50) Null,
) ON [Primary]
end
----add primary key replace # with dbo for permanent table
--begin
--alter table #CACFederalReserverHolidays add constraint
--PK_CACFederalReserverHolidays Primary Key Clustered
--(
--Date
--)
--With (Statistics_NoRecompute = off,
-- Ignore_Dup_Key = Off,
-- Allow_Row_Locks = On,
-- Allow_Page_Locks = On) On [Primary]
--end
-- insert the first date
Insert into #CACFederalReserverHolidays
([Date],[BankHoliday])
Values
(@FirstDate,'N')
-- insert the remaining dates by adding 1 to the last date
While (select MAX(DATE)
from #CACFederalReserverHolidays
) < @LastDate
begin
Set @WorkingDate = DATEADD (day,1,(select MAX(DATE) from #CACFederalReserverHolidays))
if @WorkingDate <= @LastDate
begin
insert into #CACFederalReserverHolidays
([Date], [BankHoliday])
Values
(@WorkingDate, 'N')
end
else
break
end
--ID Fed Holidays
begin
update #CACFederalReserverHolidays
set BankHoliday = 'Y',
HolidayName = 'New Year''s Day'
where DATEPART(day,Date) = 1
and DATEPART(month,Date) = 1
and DATEPART(Dw,Date) between 2 and 6
update #CACFederalReserverHolidays
set BankHoliday = 'Y',
HolidayName = 'New Year''s Day'
where DATEPART(day,Date) = 1
and DATEPART(month,Date) = 1
and DATEPART(Dw,Date) = 2
end
begin
-- MLK Day, 3rd Mon in January
update #CACFederalReserverHolidays
set BankHoliday = 'Y',
HolidayName = 'Martin Luther King Day'
where DATEPART(day,Date) between 15 and 21
and DATEPART(month,Date) = 1
and DATEPART(Dw,Date) = 2
end