为什么我不能使用简单的查询CREATE TABLE创建新表格?

huangapple go评论73阅读模式
英文:

Why I can't create a new table using simply query CREATE TABLE?

问题

MS SQL SERVER
当我执行查询

SELECT *
FROM dbo.transactions 

它会完美地列出所有行和列。

问题是当我尝试使用 CREATE TABLE 创建新表时

CREATE TABLE dbo.all_transactions 
SELECT *
FROM dbo.transactions 

或者

CREATE TABLE dbo.all_transactions AS
SELECT *
FROM dbo.transactions 

我总是得到一个奇怪的错误

Msg 156, Level 15, State 1, Line 2
关键字 'SELECT' 附近的语法不正确

只有 * 在 SELECT 旁边。它怎么可能是“语法不正确”?

我尝试更改表名或使用另一个表,但它不起作用。
我检查了列的名称是唯一的。它们是唯一的。

英文:

MS SQL SERVER
When I do query

SELECT *
FROM dbo.transactions 

it list all rows and colums perfectly.

Problem is when I'm trying to do a new table with CREATE TABLE

CREATE TABLE dbo.all_transactions 
SELECT *
FROM dbo.transactions 

or

CREATE TABLE dbo.all_transactions AS
SELECT *
FROM dbo.transactions 

Always I get strange error

Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'SELECT'

There is only * next to SELECT. How it could be an "Incorrect syntax"?

I tried changing the table name or using another table, but it doesn't work.
I checked the names of the columns are unique. They are unique.

答案1

得分: 2

SELECT * INTO dbo.all_transactions
FROM dbo.transactions;

英文:

Suppose you want to create table all_transactions copying the data from transactions
Try out this:

SELECT * INTO dbo.all_transactions
FROM dbo.transactions;

huangapple
  • 本文由 发表于 2023年3月31日 23:28:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75900251.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定