【发布时间】:2016-06-30 03:09:42
【问题描述】:
您好,我正在尝试将查询结果附加到另一个表,但出现错误,这是我的项目代码
--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
【问题讨论】:
-
使用这种语法时,必须期望指定的值的数量必须与表中的列数匹配。要使其工作,请在应将值放入的位置显式声明列名,例如。
INSERT INTO tempweblogs (date1, users, utotal) SELECT distinct top 10 Xdate as date1, Xuser as users, count (Xuser) as utotal ......
标签: sql-server vb.net