简体   繁体   English

更新临时表

[英]Update A Temp Table

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. 我注释掉了PKey部分,因为我认为我不需要它在临时表中。

I'm able to insert the Dates through 2099, "N" in BankHoliday and "Null" in HolidayName columns in the temp table. 我能够在2099年插入日期,在BankHoliday中插入“N”,在临时表的HolidayName列中插入“Null”。 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). 我认为您可能在SQL Server(或Management Studio)中发现了一个实际的错误。

These two UPDATE statements: 这两个UPDATE语句:

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

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. 但是,当我一次扫描一个字符时,我找不到任何导致此问题的Unicode值。 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. 但首先,我会请求您复制这些行并将它们带到Microsoft Connect并输入它们作为可能的错误。

(Actually, I'll be happy to enter this into MS Connect, if you want.) (实际上,如果你愿意,我很乐意将其输入MS Connect。)


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 . 我在此处将此作为SQL Server错误输入Microsoft Connect: 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. 随意去那里并对其进行upvote或评论和/或表明你是否可以自己复制它。


I just figured it out. 我刚想通了。 Here is my revised posting for Connect, which explains it pretty well: 这是我修改的Connect发布,它很好地解释了:

When I execute the following text verbatim in SSMS: 当我在SSMS中逐字执行以下文本时:

 update #CACFederalReserverHolidays set BankHoliday = 0 

It throws the error "Incorrect syntax near ' '." 它会抛出错误“''附近的语法不正确'。” As far as I can tell it should just say that the #temp table is undefined. 据我所知,它应该只是说#temp表是未定义的。 Note: Even if the #temp table is defined, it throws the same error. 注意:即使定义了#temp表,也会引发相同的错误。

I got this from a user's question on a support forum (here: Update A Temp Table ). 我从支持论坛上的用户问题得到了这个(这里: 更新临时表 )。 Cutting and pasting the user's text into my SSMS, I was able to narrow it down to these two lines. 将用户的文本剪切并粘贴到我的SSMS中,我能够将其缩小到这两行。 After a lot of editing and testing, I found that the problem would only go away if I removed the line-break and spaces between the first and second line and then re-entered them myself. 经过大量的编辑和测试后,我发现如果我删除第一行和第二行之间的换行符和空格然后自己重​​新输入它们,问题就会消失。 Suspecting some invisible/invalid character, I then quoted the text and examined it character by character with: 怀疑一些看不见/无效的字符,然后我引用了文本并逐字逐句地检查了它:

 select unicode(substring('update #CACFederalReserverHolidays set BankHolidayX = 0', 35,1)) 

But looking at characters 35, 36, and 37, only revealed the Unicode values 13, 10, and 63 (CR, LF and Space). 但是查看字符35,36和37,只显示了Unicode值13,10和63(CR,LF和Space)。

As far as I can tell, this must be a bug, either in Management Studio, or in the SQL Server Parser itself. 据我所知,这必须是一个错误,无论是在Management Studio中,还是在SQL Server Parser本身中。

Note that I have only reproduced this on SQL Server 2012 so far, but the original user reported it from SQL Server 2008. 请注意,到目前为止,我只在SQL Server 2012上重现了这一点,但原始用户从SQL Server 2008报告了它。


OK, I have just realized that my examination procedure above was flawed because I was converting it to ASCII before looking at the Unicode values. 好的,我刚刚意识到上面的检查程序存在缺陷,因为我在查看Unicode值之前将其转换为ASCII。 When I use the correct expression: 当我使用正确的表达式时:

 select unicode(substring(N'update #CACFederalReserverHolidays set BankHolidayX = 0', 37,1)) 

It reveals that character 37 is actually Unicode value 8200. I am unfamiliar with this but I assume that it is invalid. 它揭示了字符37实际上是Unicode值8200.我不熟悉这个但我认为它是无效的。


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 the update.. 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. 长话短说,似乎SQL代码中的一些空格(特别是update..后的set ..行前面的空格update..不是真正的空格(Unicode 63),但实际上是Unicode字符8200(“ PUNCTUATION SPACE“,U + 2008)。显然你需要用空格来代替它们。

What is the begin for after --ID Fed Holidays ? 之后--ID Fed Holidaysbegin是什么?

Those don't make sense there. 那些没有意义。 They only make sense for beginning and ending the while loop. 它们只对开始和结束while循环有意义。

IDK if that is the cause of your problem, but that's the first thing I noticed IDK,如果这是你的问题的原因,但这是我注意到的第一件事

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM