Incorrect syntax near 'for' SQL Server

久未见 提交于 2019-12-11 00:28:50

问题


I want to add a new Column to an existing Table that has already Data in it. The Column should be NOT NULL. Because of that i want to set a Default Value. But when i do it throws the following exception: "Incorrect syntax near 'for'"

ALTER TABLE Semester ADD SIDNew uniqueidentifier NOT NULL 
CONSTRAINT DF_SIDNew DEFAULT '00000000-0000-0000-0000-000000000000' FOR SIDNew

I already had a look at How to set a default value for an existing column and Incorrect syntax near the keyword 'FOR' but none of them helped me.


回答1:


Simply lose the part FOR SIDNew. You are adding a new column with a default constraint. You are not adding a new default constraint to an existing column.




回答2:


The FOR keyword is only required if you're adding default values to Azure SQL Data Warehouse or Parallel Data Warehouse. If you're using normal relational SQL Server (on-premise or Azure) then you can just write it without the FOR

ALTER TABLE Semester ADD SIDNew uniqueidentifier NOT NULL 
CONSTRAINT DF_SIDNew DEFAULT '00000000-0000-0000-0000-000000000000';

https://msdn.microsoft.com/en-gb/library/ms190273.aspx



来源:https://stackoverflow.com/questions/42440431/incorrect-syntax-near-for-sql-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!