简体   繁体   English

SQL创建表在SQL Server Management Studio SSMS中不起作用

[英]SQL- Create table does not work in SQL Server Management Studio SSMS

For some reason I can't create a new table from an another in SSMS. 由于某种原因,我无法在SSMS中从另一个表创建新表。

use master;

create table bop as
select *
from dbo.Data$
where [2016] is not null and 
      [Series Name] = 'Imports of goods and services (% of GDP)' or 
      [Series Name] = 'Exports of goods and services (% of GDP)'
order by [Country Name] asc;

It outputs the error: 它输出错误:

Msg 156, Level 15, State 1, Line 4 消息156,第15层,状态1,第4行
Incorrect syntax near the keyword 'select'. 关键字“ select”附近的语法不正确。

Anybody experienced this problem before? 有人遇到过这个问题吗? I'm thinking its a bug 我在想它是一个错误

That is invalid SQL syntax for SQL Server : 这对于SQL Server是无效的SQL语法:

Instead you can do : 相反,您可以执行以下操作:

select d.* into bop
from dbo.Data$ d
where [2016] is not null and 
      [Series Name] in ('Imports of goods and services (% of GDP)', 'Exports of goods and services (% of GDP)')
order by [Country Name] asc;

Note : 注意 :

  • Use IN clause if you have a more than one constant values that is easier read & write. 如果您有多个常量值,这些常量值更易于读写,请使用IN子句。
  • OR evaluates constant values one by one with no particular order, while IN sorts the list. OR不按特定顺序逐一评估常量值,而IN对列表进行排序。 So, IN is faster in some circumstances. 因此,在某些情况下, IN速度更快。

  • OR will not include null s values if you have, but IN will include null . 如果有,则OR将不包含null的值,但IN将包含null So, choose one of them based on what actually you want. 因此,请根据实际需要选择其中之一。

Try below way 尝试以下方式

SELECT * INTO schema.newtable FROM schema.oldtable WHERE 1 = 0

From your query you have to remove 从查询中,您必须删除

create table bop as  ## this portion

and instead of it you have to add select * INTO bop 代替它,您必须添加select * INTO bop

So your query will be 所以您的查询将是

select * INTO bop
from dbo.Data$
where [2016] is not null and 
    (  [Series Name] = 'Imports of goods and services (% of GDP)' or 
      [Series Name] = 'Exports of goods and services (% of GDP)'
    )
order by [Country Name]

Instead of OR you can use in operator 可以使用in运算符代替OR

select * INTO bop
    from dbo.Data$
    where [2016] is not null and 
          [Series Name] in ('Imports of goods and services (% of GDP)',
           'Exports of goods and services (% of GDP)')
    order by [Country Name]

Your Error analysis 您的错误分析

  • Msg 156, Level 15, State 1, Line 4 Incorrect syntax near the keyword 'select'. 消息156,级别15,状态1,第4行关键字'select'附近的语法错误。

As you used wrong sql statement to create one table from others table thats why sqlserver thrown systax Error 当您使用错误的sql语句从其他表创建一个表时,这就是sqlserver引发systax错误的原因

You have several issues with your query. 您的查询有几个问题。 The correct syntax for what you intend is: 您想要的正确语法是:

select d.*
into bop
from dbo.Data$ d
where [2016] is not null and 
      [Series Name] in ('Imports of goods and services (% of GDP)', 'Exports of goods and services (% of GDP)');

Note the following: 请注意以下几点:

  • SQL Server uses select into , not create table as . SQL Server使用select into ,而不是create table as This is your basic syntax error. 这是您的基本语法错误。
  • Presumably, you want either series name and 20161 not to be NULL . 假设您希望系列名称 20161 not to be NULL . in does this. does this. AND has higher precedence than OR`, so your logic does something different. AND的has higher precedence than OR,因此您的逻辑有所不同。
  • ORDER BY is not appropriate, unless the destination table has an identity column. 除非目标表具有标识列,否则ORDER BY是不合适的。 SQL tables represent unordered sets. SQL表表示无序集。 The only ordering would be on the values in the identity column. 唯一的顺序是在identity列中的值上。

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

相关问题 从没有SQL Server Management Studio(SSMS)的MDF创建BAK - Create BAK from MDF without SQL Server Management Studio (SSMS) 如何在 SQL Server Management Studio (SSMS) 中评论 SQL 查询 - How to comment SQL queries in SQL Server Management Studio (SSMS) 在 SQL Server Management Studio(SSMS) 中评论和取消评论 SQL 查询的快捷方式 - Shortcuts to comment and uncomment SQL queries in SQL Server Management Studio(SSMS) 如何在SQL Server Management Studio(SSMS)中合并这些列 - How can I combine these columns in SQL Server Management Studio (SSMS) 有没有办法在 SQL Server Management Studio (SSMS) 中分隔查询结果? - Is there a way to separate query results in SQL Server Management Studio (SSMS)? 禁用 SQL Server Management Studio (SSMS) 中的空白文本副本 - Disabling copy of blank text in SQL Server Management Studio (SSMS) 在SQL Server Management Studio中修改表值函数时,为什么我的表值函数工作得更快? - Why does my table value function work faster, when i modify it in SQL server management studio? 将时间发送到SQL Server Management Studio表,但不显示吗? - Sending time to SQL Server Management Studio table but it does not display? SQL Server 2012别名在SQL Server Management Studio外部不起作用 - SQL Server 2012 alias does not work outside SQL Server Management Studio SQL在数据库SSMS中创建表 - SQL Create table in database ssms
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM