2016-06-30 58 views
0

您好我試圖追加我的查詢結果到另一個表,但我得到一個錯誤,這裏是我的代碼爲項目錯誤MSG 213列名或提供的值的數量不匹配表定義

--create new table 
create table tempweblogs 
(
    date1 datetime 
    users nvarchar(50) 
    utotal int 
    date2 datetime 
    hostname nvarchar(50) 
    htotal int 
    date3 datetime 
    srcip nvarchar(50) 
    stotal int 
) 
--insert query result to tempweblogs table 

insert into tempweblogs 
SELECT distinct top 10 
     Xdate as date1, Xuser as users, count (Xuser) as utotal 
from weblogs 
where Xdate='2/16/2016' and Xuser is not null 
group by Xuser, Xdate order by utotal DESC  

SELECT distinct top 10 
     Xdate as date2, Xhostname as hostname, count(Xhostname) as htotal 
from weblogs  
where Xdate='2/16/2016' and xhostname is not null 
group by Xhostname, Xdate order by htotal DESC 

SELECT distinct top 10 
     Xdate as date3, Xsrcip as srcip, count (Xsrcip) as stotal 
from weblogs 
where Xdate='2/16/2016' and Xuser is not null 
group by Xsrcip, Xdate order by stotal DESC 
+1

當使用這種語法,必須預期數量指定的lues必須與表中的列數匹配。爲了使它工作,顯式聲明列名應放在哪裏,例如。 INSERT INTO tempweblogs(date1,users,utotal)SELECT distinct top 10 Xdate as date1,Xuser as users,count(Xuser)as utotal ......' –

回答

2

:你需要在INSERT語句

insert into tempweblogs (date1 , users , utotal) 
select .... 

還需要所有3個查詢的INSERT子句指定列列表

+0

Thanks Squirrel, This work out fine – Melvuen

相關問題