2010-07-28 100 views
0

有人可以告诉为什么这个查询不起作用吗?为什么@Table不适合我?

DECLARE @unwantedRows TABLE 
( 
    ProductId INT, 
    ProductName VARCHAR(50), 
    Description VARCHAR(50), 
    Category VARCHAR(50), 
    Repetitions VARCHAR(50) 

); 

Select * 
INTO @unwantedRows From 
(
Select a.*,Row_Number() Over(Partition By ProductId Order By ProductId) As [Repetitons] from tblProduct a 
) As A 

Where A.Repetitons > 1 

错误我得到的是

`消息102,级别15,状态1,行12 附近有语法错误@unwantedRows“。 Msg 156,Level 15,State 1,Line 15 关键字'As'附近的语法不正确。

编辑:

现在,它与Repetitions,并提供: -

INSERT 
INTO @unwantedRows 
Select a.*,Row_Number() Over(Partition By ProductId Order By ProductId) As [Repetitons] from tblProduct a 
Where a.Repetitons > 1 

`

Invalid column name 'Repetitons'.

+0

检查更新,插入查询 – 2010-07-28 11:03:41

+0

你拼写错误重复次数的查询,但没有在表中。 – HLGEM 2010-07-28 17:42:04

回答

1

一个错误,我发现它是不选择到选择..插入声明

以下查询没有错误做工精细上述查询即语法错误

DECLARE @unwantedRows TABLE 
( 
    ProductId INT, 
    ProductName VARCHAR(50), 
    Description VARCHAR(50), 
    Category VARCHAR(50), 
    Repetitions VARCHAR(50) 

); 

insert INTO @unwantedRows 
Select a.*,Row_Number() Over(Partition By ProductId Order By ProductId) As [Repetitons] from tblProduct 
Where A.Repetitons > 1 

insetead你也可以试试这个

insert INTO @unwantedRows 
select * from (
Select a.*,Row_Number() Over(Partition By ProductId Order By ProductId) As [Repetitons] from tblProduct) d 
Where d.Repetitons > 1 
+0

请阅读我编辑的帖子。顺便说一句,'凡A.Repetitons> 1'应该是'如果a.Repetitons> 1' – TCM 2010-07-28 11:00:18

+0

SQL是不区分大小写,因此这将工作 – 2010-07-28 11:01:53

+0

尼斯。但是这次肯定有一个错误。它应该是'tblProduct a'而不是'tblProduct'。感谢它的工作。投票 – TCM 2010-07-28 11:07:18

1

因为select into创建一个表,你想insert into @unwantedRows select * from ...


编辑

,然后你不允许在Where子句中使用窗口函数(如分区)。如果你必须这样做,换你选择到另一个选择:

select * from (select * from ...) as a where a.Partition > 1 
+0

请阅读我编辑的帖子。 – TCM 2010-07-28 10:59:50

+1

好的。请阅读我编辑的答案:) – GSerg 2010-07-28 11:07:21

1

您需要删除SELECT INTO,你需要指定列。

INSERT @unwantedRows 
SELECT a.ProductID, a.ProductName, a.Description, a.Category, a.Repetitions 
FROM (
    Select *, Row_Number() Over (Partition By ProductId Order By ProductId) As [Repetitons] 
    from tblProduct) As A 
Where A.Repetitons > 1; 
0
insert into @unwantedRows 
Select * from 
(
Select a.*,Row_Number() Over(Partition By ProductId Order By ProductId) As [Repetitons] from tblProduct a 
) As A 
Where A.Repetitons > 1 
+0

我觉得'选择'中缺少'from'关键字? – TCM 2010-07-28 11:10:49

+0

对比提示,我编辑了我的答案,在这里SO是如此急于回答第一,我没有时间双重检查 – adopilot 2010-07-28 11:28:34