views:

30

answers:

4

Can anybody tell why is this query not working?

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

Error i get is

`Msg 102, Level 15, State 1, Line 12 Incorrect syntax near '@unwantedRows'. Msg 156, Level 15, State 1, Line 15 Incorrect syntax near the keyword 'As'.

Edit :

Now it's giving with 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'.

+1  A: 

Because select into creates a table, and you want insert into @unwantedRows select * from ...


EDIT

And then, you're not allowed to use a window function (such as Partition) in a Where clause. If you must do it, wrap your select into another select:

select * from (select * from ...) as a where a.Partition > 1
GSerg
Please read my edited post.
Ankit Rathod
Ok. Please read my edited answer :)
GSerg
+1  A: 

One error i found is its not select into its select .. insert into statement

Following query working fine without errors i.e syntax error

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 of above query you can also try this

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
Pranay Rana
Please read my edited post. Btw, `Where A.Repetitons > 1` should be `Where a.Repetitons > 1`
Ankit Rathod
sql is not case sensitive so this will work
Pranay Rana
Nice. But this time there is surely a mistake. It should be `tblProduct a` instead of `tblProduct`. Thanks it works. Voted
Ankit Rathod
+1  A: 

You need to remove the SELECT INTO and you need to specify your columns.

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;
Dave Markle
A: 
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
adopilot
I think `from` keyword is missing in `Select`?
Ankit Rathod
Yep thanx on tip, I edited my answer, Here on SO is so rush to answer first that I did not have time to duble check
adopilot

related questions