简体   繁体   English

CREATE OR ALTER TABLE 因“关键字‘TABLE’附近的语法不正确”而失败

[英]CREATE OR ALTER TABLE is failing with "Incorrect syntax near the keyword 'TABLE'"

I am trying to make few tables in the database.我正在尝试在数据库中创建几个表。
& the tool I`m using is Azure Data Studio. & 我使用的工具是 Azure Data Studio。

Command that I`m using it:-命令我正在使用它:-

CREATE OR ALTER TABLE TABLE_NAME
(
    Date         datetime,
    Sequence         nvarchar(8),
    Code     nvarchar(3),
)
    GO

Error that I am getting:-我得到的错误:- 在此处输入图像描述

What I wanted to do is, create a table if not exist or if it does exist then alter it.我想做的是,如果不存在则创建一个表,或者如果它存在则更改它。 I`m unable to understand why its failing.我无法理解为什么它会失败。

We already have something available on Stackover Flow itself Incorrect Syntax near the keyword 'OR' in CREATE OR ALTER PROCEDURE我们已经在 Stackover Flow 本身上提供了一些可用信息Incorrect Syntax near the keyword 'OR' in CREATE OR ALTER PROCEDURE

We have create and alter for external tables here .我们在这里create and alter了外部表。

Why this isnt working?为什么这不起作用?

I found out there is no CREATE OR ALTER syntax for tables.我发现表没有 CREATE OR ALTER 语法。 It works only for views, triggers, functions and stored procedures.它仅适用于视图、触发器、函数和存储过程。

The corresponding MSSQL Tiger Team blog post explains the situation:- 相应的 MSSQL Tiger Team 博客文章解释了这种情况:-

CREATE OR ALTER can be used in programmability objects such as: CREATE OR ALTER 可用于可编程对象,例如:

  • STORED PROCEDURES (including natively compiled)存储过程(包括本机编译的)
  • FUNCTIONS (Transact-SQL, including natively compiled)函数(Transact-SQL,包括本机编译的)
  • TRIGGERS触发器
  • VIEWS观点

But cannot be used in: Objects that require storage (tables, indexes and indexed views)但不能用于:需要存储的对象(表、索引和索引视图)

  • CLR user-defined functions CLR 用户定义函数
  • Deprecated programmability objects (RULE and DEFAULT)已弃用的可编程性对象(RULE 和 DEFAULT)
  • Non-programmability objects (such as CREATE ASSEMBLY, CREATE TABLE or CREATE - SCHEMA).非可编程对象(例如 CREATE ASSEMBLY、CREATE TABLE 或 CREATE - SCHEMA)。 On these objects, the syntax for CREATE and ALTER is very different from a syntax and usability perspective.在这些对象上,CREATE 和 ALTER 的语法从语法和可用性的角度来看非常不同。

Instead, we can create table if it doesn't exist (eg like here ) or explicitly drop it first:相反,我们可以创建表(如果它不存在)(例如像这里一样)或者先显式删除它:

DROP TABLE IF EXISTS FOO;
CREATE TABLE FOO (...)

OR或者

DROP TABLE IF EXISTS FOO;
    GO
CREATE TABLE FOO (...)

Both will work fine.两者都可以正常工作。

I hope this will help the community.我希望这会对社区有所帮助。

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

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