如何从SQL View创建表结构?

时间:2019-04-22 10:39:32

标签: sql-server

我需要从sql视图创建一个表结构。

我使用Azure SQL

CREATE TABLE ProvidersByNameOrLocation
    AS
    (
        SELECT STRING_AGG(CONCAT(C.name, ' ', Y.name, ' (', Y.max_length, ') ', IIF(C.is_nullable = 1, '', 'NOT '), 'NULL'), ', ')
        FROM SYS.all_columns C
        INNER JOIN SYS.all_objects T
        ON C.object_id = T.object_id
        INNER JOIN sys.types Y
        ON C.system_type_id = Y.system_type_id
        WHERE T.name = 'tableName'
    )

1 个答案:

答案 0 :(得分:1)

SQL Server不支持此Oracle语法。因此,您可能必须手动创建表,然后使用INSERT INTO ... SELECT填充它:

CREATE TABLE ProvidersByNameOrLocation (some_col varchar(200));
INSERT INTO ProvidersByNameOrLocation (some_col)
SELECT STRING_AGG(CONCAT(C.name, ' ', Y.name, ' (', Y.max_length, ') ', IIF(C.is_nullable = 1, '', 'NOT '), 'NULL'), ', ')
FROM SYS.all_columns C
INNER JOIN SYS.all_objects T
    ON C.object_id = T.object_id
INNER JOIN sys.types Y
    ON C.system_type_id = Y.system_type_id
WHERE T.name = 'tableName';