Sql 结果集的表别名

Sql 结果集的表别名,sql,sql-server-2008,Sql,Sql Server 2008,如何将给定查询的结果集存储在新表中 select d1.year,d1.month,d1.Circle_code,d1.Call_type_code,d1.DescId,d1.CustId, d1.call_logged,d2.Call_Cancel from dbo.Table_M_CALL_LOGGED as d1 join dbo.Table_M_CALL_CANCEL as d2 on d1.year=d2.year and d1.month=d2.month and d1.Ci

如何将给定查询的结果集存储在新表中

select   d1.year,d1.month,d1.Circle_code,d1.Call_type_code,d1.DescId,d1.CustId,
d1.call_logged,d2.Call_Cancel
from dbo.Table_M_CALL_LOGGED as d1 
join dbo.Table_M_CALL_CANCEL as d2 on

d1.year=d2.year
and d1.month=d2.month
and d1.Circle_Code=d2.Circle_Code
and d1.Call_Type_Code=d2.Call_Type_Code
and d1.DescId=d2.DescId
and d1.CustId=d2.custID

我在这里创建一个新的临时表,只是为了向您展示如何将查询结果直接插入表中

--Creating new TempTable
CREATE TABLE #tempTable(tempyear nvarchar(20),tempmonth nvarchar(20),Circle_code   nvarchar(20),Call_type_code nvarchar(20),
DescId nvarchar(20),CustId nvarchar(20),call_logged nvarchar(30),Call_Cancel nvarchar(20));

--Inserting the data into tempTable
INSERT INTO #tempTable(tempyear,tempmonth,Circle_code,Call_type_code,DescId,CustId,call_logged,Call_Cancel)
                    select   d1.year,d1.month,d1.Circle_code,d1.Call_type_code,d1.DescId,d1.CustId,
                    d1.call_logged,d2.Call_Cancel
                    from dbo.Table_M_CALL_LOGGED as d1 
                    join dbo.Table_M_CALL_CANCEL as d2 on
                    d1.year=d2.year
                    and d1.month=d2.month
                    and d1.Circle_Code=d2.Circle_Code
                    and d1.Call_Type_Code=d2.Call_Type_Code
                    and d1.DescId=d2.DescId
                    and d1.CustId=d2.custID
当一个表中的数据要插入到另一个表中新创建的表中时,需要创建该表,而之前未创建该表时,将使用下面的方法。新表是使用与选定列相同的数据类型创建的

                    SELECT   d1.year,d1.month,d1.Circle_code,d1.Call_type_code,d1.DescId,d1.CustId,
                    d1.call_logged,d2.Call_Cancel
                    INTO new_table   --Here inserting into new table
                    FROM dbo.Table_M_CALL_LOGGED AS d1 
                    join dbo.Table_M_CALL_CANCEL AS d2 ON
                    d1.year=d2.year
                    AND d1.month=d2.month
                    AND d1.Circle_Code=d2.Circle_Code
                    AND d1.Call_Type_Code=d2.Call_Type_Code
                    AND d1.DescId=d2.DescId
                    AND d1.CustId=d2.custID

我在这里创建一个新的临时表,只是为了向您展示如何将查询结果直接插入表中

--Creating new TempTable
CREATE TABLE #tempTable(tempyear nvarchar(20),tempmonth nvarchar(20),Circle_code   nvarchar(20),Call_type_code nvarchar(20),
DescId nvarchar(20),CustId nvarchar(20),call_logged nvarchar(30),Call_Cancel nvarchar(20));

--Inserting the data into tempTable
INSERT INTO #tempTable(tempyear,tempmonth,Circle_code,Call_type_code,DescId,CustId,call_logged,Call_Cancel)
                    select   d1.year,d1.month,d1.Circle_code,d1.Call_type_code,d1.DescId,d1.CustId,
                    d1.call_logged,d2.Call_Cancel
                    from dbo.Table_M_CALL_LOGGED as d1 
                    join dbo.Table_M_CALL_CANCEL as d2 on
                    d1.year=d2.year
                    and d1.month=d2.month
                    and d1.Circle_Code=d2.Circle_Code
                    and d1.Call_Type_Code=d2.Call_Type_Code
                    and d1.DescId=d2.DescId
                    and d1.CustId=d2.custID
当一个表中的数据要插入到另一个表中新创建的表中时,需要创建该表,而之前未创建该表时,将使用下面的方法。新表是使用与选定列相同的数据类型创建的

                    SELECT   d1.year,d1.month,d1.Circle_code,d1.Call_type_code,d1.DescId,d1.CustId,
                    d1.call_logged,d2.Call_Cancel
                    INTO new_table   --Here inserting into new table
                    FROM dbo.Table_M_CALL_LOGGED AS d1 
                    join dbo.Table_M_CALL_CANCEL AS d2 ON
                    d1.year=d2.year
                    AND d1.month=d2.month
                    AND d1.Circle_Code=d2.Circle_Code
                    AND d1.Call_Type_Code=d2.Call_Type_Code
                    AND d1.DescId=d2.DescId
                    AND d1.CustId=d2.custID

还有其他方法吗..?第一种方法的一个优点是,您可以指定查询需要满足的数据类型和约束。还有其他方法吗..?第一种方法的一个优点是,您可以指定查询需要满足的数据类型和约束。因为您似乎是SQL新手,一个好习惯是用更好的描述命名别名(d1,d2),例如d1=>loggedCall,d2=>CanceledCall。因为您只需输入一次,但要阅读多次,所以最好花一点时间进行详细说明。从长远来看,您会发现它工作得更好。因为您似乎是SQL新手,所以一个好习惯是使用更好的描述命名别名(d1,d2),例如d1=>loggedCall,d2=>CanceledCall。因为您只需输入一次,但要阅读多次,所以最好花一点时间进行详细说明。从长远来看,你会发现它工作得更好。