2010-07-28 78 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