I’m working on creating some reporting for the new year. I lifted this code and attempted to make it work for a temp table as I don’t have create table rights on my DB. I commented out the PKey portion as I didn’t think I needed it in a temp table.
I’m able to insert the Dates through 2099, “N” in BankHoliday and “Null” in HolidayName columns in the temp table. But, when I go to run the update portion for “New Years Day” holiday separately or as one long query I get the same syntax error. “Incorrect syntax near ‘ ‘.” Just when I think I know what I’m doing too…
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
I think that you may have found an actual bug in SQL Server (or in Management Studio).
These two UPDATE statements:
Appear to have some kind of invalid space characters and/or line breaks within the first two lines. However, when I scan it one character at a time, I can not find any Unicode values that should cause this. Nonetheless, when I edit out all of the spaces and line-breaks and then re-enter them by hand, the queries no longer get “Invalid Syntax” errors.
I suggest that you do the same thing. But first, I would request that you copy these lines and take them to Microsoft Connect and enter them as a possible bug.
(Actually, I’ll be happy to enter this into MS Connect, if you want.)
I have entered this into Microsoft Connect as a SQL Server bug here: https://connect.microsoft.com/SQLServer/feedback/details/775641/ssms-throws-spurious-incorrect-syntax-error. Feel free to go there and upvote or comment on it and/or indicate if you can reproduce it yourself.
I just figured it out. Here is my revised posting for Connect, which explains it pretty well:
So long story short, it appears that some of the spaces in the SQL code (specifically the spaces in front of the
set ..lines after theupdate..are not true spaces (Unicode 63), but are actually Unicode character 8200 (“PUNCTUATION SPACE”, U+2008). Obviously you’ll need to replace these with spaces.