简体   繁体   English

SQL Server查询以添加具有默认值的列

[英]SQL server query to add column with default value

I'm trying to change my SQL server database by adding another column to a table with 0 as a default value. 我正在尝试通过向表中添加另一列来更改我的SQL服务器数据库,其中0为默认值。

I thought this script worked in the past (for another table), but now I have an error when I try to execute the script 我认为这个脚本在过去(另一个表)工作,但现在我尝试执行脚本时出错了

The script 剧本

ALTER TABLE Whatever
ADD WhateverColumn tinyint NOT NULL 
DEFAULT(0)

The errors 错误

  • On hovering mouse over "NOT": Incorrect syntax near 'NOT'. 将鼠标悬停在“NOT”上:“NOT”附近的语法不正确。 Expecting 'TO' 期待'TO'
  • On hovering mouse over "0": Incorrect syntax near 0. Expecting '(', or SELECT 将鼠标悬停在“0”上:语法接近0时出错。期望'('或SELECT

Anyone knows what's wrong with this? 谁知道这有什么问题?

Try this: 尝试这个:

ALTER TABLE Whatever
ADD WhateverColumn tinyint NOT NULL DEFAULT 0

也许你使用的“Whatever”作为表名有未闭合的引号,甚至是“WhateverColumn”(你把它放在这里作为代币,我得到它)我有这个问题,甚至是“WhateverColumn”的实名是一个保留字?


@SammuelMiranda has just asked the same just now. @SammuelMiranda刚刚问了同样的问题。 It matters if you are using reserved keyword as table or column name also. 如果您还使用保留关键字作为表或列名称,则很重要。
you can check this link Add a column with a default value to an existing table in SQL Server 您可以检查此链接将具有默认值的列添加到SQL Server中的现有表

As I expected, updating my SQL Server Management Studio to version 17.8.1 solved my issue. 正如我所料,将我的SQL Server Management Studio更新到版本17.8.1解决了我的问题。
After updating, I was able to run this command without any problem. 更新后,我能够毫无问题地运行此命令。

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

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