SQL 从视图创建表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6694430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 11:20:11  来源:igfitidea点击:

Create Table from View

sqltsqlview

提问by tdjfdjdj

I have a view that I want to create a table from in SQL Enterprise Manager, but I always get an error when I run this query:

我有一个视图,我想从 SQL 企业管理器中创建一个表,但在运行此查询时总是出现错误:

CREATE TABLE A 
AS
(SELECT top 10 FROM dbo.myView)

So far the error is: "syntax error at 'as'"

到目前为止,错误是:“'as'处的语法错误”

View is too large. Is it possible to use a top 10?

视野过大。是否可以使用前 10 名?

回答by Quassnoi

SQL Serverdoes not support CREATE TABLE AS SELECT.

SQL Server不支持CREATE TABLE AS SELECT

Use this:

用这个:

SELECT  *
INTO    A
FROM    myview

or

或者

SELECT  TOP 10
        *
INTO    A
FROM    myview
ORDER BY
        id

回答by Pittsburgh DBA

If you just want to snag the schema and make an empty table out of it, use a false predicate, like so:

如果您只想获取模式并从中创建一个空表,请使用假谓词,如下所示:

SELECT * INTO myNewTable FROM myView WHERE 1=2

回答by Daniel Hilgarth

In SQL SERVER you do it like this:

在 SQL SERVER 中,您可以这样做:

SELECT *
INTO A
FROM dbo.myView

This will create a new table Awith the contents of your view.
See herefor more info.

这将创建一个A包含视图内容的新表。
请参阅此处了解更多信息。

回答by Keith

To create a table on the fly us this syntax:

要动态创建表,请使用以下语法:

SELECT *
INTO A
FROM dbo.myView

回答by Alex K.

If you want to create a new Ayou can use INTO;

如果你想创建一个新的,A你可以使用INTO;

select * into A from dbo.myView

回答by mikey

SELECT * INTO [table_a] FROM dbo.myView

回答by MatBailie

Looks a lot like Oracle, but that doesn't work on SQL Server.

看起来很像 Oracle,但在 SQL Server 上不起作用。

You can, instead, adopt the following syntax...

相反,您可以采用以下语法...

SELECT
  *
INTO
  new_table
FROM
  old_source(s)

回答by javal

Select 
    MonthEndDate MED,  
    SUM(GrossBalance/1000000) GrossBalance,
    PortfolioRename PR 
into 
    testDynamic 
from 
    Risk_PortfolioOverview  
    Group By MonthEndDate, PortfolioRename

回答by amstegraf

INSERT INTO table 2
SELECT * FROM table1/view1