表结果集的别名

man*_*ngh 1 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.Circle_Code=d2.Circle_Code
and d1.Call_Type_Code=d2.Call_Type_Code
and d1.DescId=d2.DescId
and d1.CustId=d2.custID
Run Code Online (Sandbox Code Playgroud)

小智 5

我在这里创建新的临时表,只显示如何将查询结果直接插入表....

--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
Run Code Online (Sandbox Code Playgroud)

当前面没有创建表时使用下面的方法,并且需要在将来自一个表的数据从另一个表插入到新创建的表中时创建.使用与所选列相同的数据类型创建新表.

                    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
Run Code Online (Sandbox Code Playgroud)