[英]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 : 注意 :
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 您的错误分析
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: 请注意以下几点:
select into
, not create table as
. select into
,而不是create table as
。 This is your basic syntax error. 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. 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. identity
column. identity
列中的值上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.