子表中的insert语句

时间:2011-12-29 05:12:24

标签: sql sql-server-2008 sql-server-2005 tsql

我试图在子表中插入,从父表中获取[ACCNO] id及其计数值,然后在子表的FK中插入[ACCNO]并对每个[ACCNO]进行计数,但仍有一些错误那里

INSERT INTO [test1].[dbo].[star_schema]
           (
           [ACCNO],
           [book_frequency])
     VALUES
           (SELECT  [books_dimension] .ACCNO , count(books_dimension .[ACCNO]) as book_frequency
            FROM         [books_dimension]  
                         group by [ACCNO] having (COUNT(*)>1) order by book_frequency  desc)
GO

它在SELECT附近和最后一个括号中给出错误。 我还想在表[star_schema]中提到,id是star_int,它是identity -

1 个答案:

答案 0 :(得分:2)

从select中插入时,您不需要VALUESORDER BY关键字,请执行以下操作:

INSERT INTO [test1].[dbo].[star_schema]([ACCNO], [book_frequency])
SELECT  
  [books_dimension].ACCNO, 
  count(books_dimension.[ACCNO]) as book_frequency
FROM [books_dimension]  
group by [ACCNO] 
having COUNT(*)>1