简体   繁体   English

在 SQL Server Management Studio 中创建临时表时遇到问题

[英]Trouble creating temp table in SQL Server Management Studio

I'm trying to create a table from a join of 2 tables so I can switch between data.我正在尝试从 2 个表的连接中创建一个表,以便在数据之间切换。 But I'm getting a syntax error on "(" and if I remove I and only do "AS" I get a syntax error on the select command.但是我在“(”上遇到语法错误,如果我删除 I 并且只执行“AS”,我会在 select 命令上收到语法错误。

CREATE TABLE Neigh 
AS 
    (SELECT *
     FROM COR_NEIGBORHOODS_N1 A1 
     LEFT JOIN NEIG_RAMAT_GAN_SHCONOT B1 ON A1.Settle_Name = B1.City_Name 
                                         AND A1.NEIGHBORHOODs_AREA_FNAME = B1.name
     WHERE B1.City_Name IS Null AND A1.Settle_Code = 8600)

Does anyone know what's the problem?有谁知道是什么问题?

SQL Server does not support create table as . SQL 服务器不支持create table as . Instead, it uses the syntax into .相反,它使用语法into So:所以:

select *
into Neigh
from COR_NEIGBORHOODS_N1 n left join
     NEIG_RAMAT_GAN_SHCONOT nrg
     on n.Settle_Name = nrg.City_Name and
        n.NEIGHBORHOODs_AREA_FNAME = nrg.name
WHERE nrg.City_Name IS Null and n.Settle_Code = 8600;

The use of select * is dangerous -- because column names might be duplicated.使用select *很危险——因为列名可能重复。 I would recommend that you list the columns specifically.我建议您专门列出这些列。

Notice that I also changed the table aliases from meaningless letters to abbreviations for the table names.请注意,我还将表别名从无意义的字母更改为表名的缩写。

CREATE TABLE Neigh AS (SELECT Column_name AS Column1,Column_name AS Column2 FROM COR_NEIGBORHOODS_N1 CN LEFT JOIN NEIG_RAMAT_GAN_SHCONOT NRG ON CN.Settle_Name = NRG.City_Name AND CN.NEIGHBORHOODs_AREA_FNAME = NRG.name WHERE NRG.City_Name IS Null AND CN.Settle_Code = 8600)

Please use 'AS' for column aliases with the same tables as I have used in the above query.请使用“AS”作为我在上述查询中使用的相同表的列别名。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM